cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console