cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container" id="container">
  <div class="box" id="movable">
    <div class="face" id="face"></div>
    <p id="message">plz move me</p>
  </div>
</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=PT+Mono');

$elmBg: #474ec8;
$containerBg: #767df9;
$fontColor: #000;
$faintedBg: rgba(#fff, 0.3);

body,
html,
.container {
  margin: 0;
  padding: 0;
  border: 0;
  position: relative;
  background-color: $containerBg;
  width: 100%;
  height: 100%;
}

.face {
  width: 125px;
  height: 70px;
  font-size: 35px;
  line-height: 70px;
  
  &:after {
    content: '◉_◉';
    text-align: center;
  }
}

.box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 70px;
  left: 100px;
  box-sizing: border-box;
  border: 2px solid currentColor;
  color: $fontColor;
  background: $elmBg;
  width: 150px;
  height: 150px;
  font-family: 'PT Mono', monospace;
  font-size: 16px;
  cursor: pointer;
  user-select: none; 
  text-align: center;
  
  &:hover {
    background-color: darken($elmBg, 2%);
  }
  
  &:active,
  &:focus {
    background-color: darken($elmBg, 10%);
    cursor: move;
    cursor: grab;

    .face {
      &:after {
        content: ' ͡ ͜ ͡ ';
        position: relative;
        left: -8px;
      }
    }
    &.pained {

      .face {
        &:after {
          content: 'ಥ﹏ಥ';
          left: 0;
        }
      }
    }

    &.fainted {
      background-color: $faintedBg;
      .face {
        &:after {
          content: 'X ~ X';
          left: 0;
        }
      }
    }
  }
}
            
          
!
            
              (function(){
  const moveMe = document.getElementById('movable'),
        face = document.getElementById('face'),
        message = document.getElementById('message');
  
  let diffY,
      diffX,
      elmHeight,
      elmWidth,
      containerHeight,
      containerWidth,
      outOfBoundsCounter,
      isMouseDown = false;

  function setOutOfBoundsFace() {
    if (!moveMe.classList.contains('pained') && !moveMe.classList.contains('fainted')) {
      outOfBoundsCounter++;
      if (outOfBoundsCounter < 5) {
        moveMe.classList.add('pained');    
        message.innerHTML = 'ouch!';
      } else {
        moveMe.classList.add('fainted');
        message.innerHTML = 'zzz';
        setTimeout(() => {
          outOfBoundsCounter = 0;
        }, 3000);
      }
    }
  }

  function resetFace() {
    if (outOfBoundsCounter < 5) {
      moveMe.classList.remove('pained', 'fainted');
      message.innerHTML = 'yay!';
    }
  }

  function mouseDown(e) {
    isMouseDown = true;
    outOfBoundsCounter = 0;
    // get initial mousedown coordinated
    const mouseY = e.clientY;
    const mouseX = e.clientX;
    
    // get element top and left positions
    const elm = moveMe;
    const elmY = elm.offsetTop;
    const elmX = elm.offsetLeft;
    
    // get elm dimensions
    elmWidth = elm.offsetWidth;
    elmHeight = elm.offsetHeight;
    
    // get container dimensions
    const container = elm.offsetParent;
    containerWidth = container.offsetWidth;
    containerHeight = container.offsetHeight;
    
    // get diff from (0,0) to mousedown point
    diffY = mouseY - elmY;
    diffX = mouseX - elmX;
  }
  

  function mouseMove(e) {
    if (!isMouseDown) return;
    const elm = moveMe;
    // get new mouse coordinates
    const newMouseY = e.clientY;
    const newMouseX = e.clientX;
    
    // calc new top, left pos of elm
    let newElmTop = newMouseY - diffY,
        newElmLeft = newMouseX - diffX;
    
    // calc new bottom, right pos of elm
    let newElmBottom = newElmTop + elmHeight,
        newElmRight = newElmLeft + elmWidth;
    
    if ((newElmTop < 0) || (newElmLeft < 0) || (newElmTop + elmHeight > containerHeight) || (newElmLeft + elmWidth > containerWidth)) {
      // if elm is being dragged off top of the container...
      if (newElmTop < 0) {
        newElmTop = 0;
      }
      
      // if elm is being dragged off left of the container...
      if (newElmLeft < 0) {
        newElmLeft = 0;
      }

      // if elm is being dragged off bottom of the container...
      if (newElmBottom > containerHeight) {
        newElmTop = containerHeight - elmHeight;
      }

      // if elm is being dragged off right of the container...
      if (newElmRight > containerWidth) {
        newElmLeft = containerWidth - elmWidth;
      }
      setOutOfBoundsFace();
    } else {
      resetFace();
    }

    moveElm(elm, newElmTop, newElmLeft);
  }

  // move elm
  function moveElm(elm, yPos, xPos) {
    elm.style.top = yPos + 'px';
    elm.style.left = xPos + 'px';
  }

  function mouseUp() {
    isMouseDown = false;
    message.innerHTML = 'plz move me';
  }

  moveMe.addEventListener('mousedown', mouseDown);
  document.addEventListener('mousemove', mouseMove);
  document.addEventListener('mouseup', mouseUp);
})();
            
          
!
999px
Loading ..................

Console