Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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.

+ add another resource

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='ball'></div>
              
            
!

CSS

              
                #ball {
  cursor: pointer;
  border: 1px solid #000;
  height: 50px;
  width: 50px;
  border-radius: 25px;
  background: #67f0b6;
  position: fixed;
  top: 50%;
  left: 50%;
}
              
            
!

JS

              
                // 主函数
const main = () => {
  const ball = document.getElementById('ball');
  
  let isDraging = false; // 记录是否在拖动中
  
  let dragWithParams; // 绑定参数的拖动方法
  
  const drag = (params, e) => {
    let x = e.clientX - params.offsetLeft;
    let y = e.clientY - params.offsetTop;
    
    // 避免小球移出视口
    if (x < 0) {
      x = 0;
    } else if (x > document.documentElement.clientWidth - ball.offsetWidth) {
      x = document.documentElement.clientWidth - ball.offsetWidth
    }
    if (y < 0) {
      y = 0;
    } else if (y > document.documentElement.clientHeight - ball.offsetHeight) {
      y = document.documentElement.clientHeight - ball.offsetHeight;
    }
    
    ball.style.left = x + 'px';
    ball.style.top = y + 'px';
  }
  
  // 当鼠标弹起时判断是否移除 mousemove 事件
  document.addEventListener('mouseup', () => {
    if (isDraging) {
      console.log('up');
      document.removeEventListener('mousemove', dragWithParams);
      isDraging = false;
    }
  })
  
  // 点击球时为 document 绑定 mousemove 事件
  ball.addEventListener('mousedown', e => {
    const offsetLeft = e.clientX - ball.offsetLeft;
    const offsetTop = e.clientY - ball.offsetTop;
    
    isDraging = true;
    
    // 绑定参数
    dragWithParams = drag.bind(null, {
      offsetLeft,
      offsetTop,
    })
    
    document.addEventListener('mousemove', dragWithParams);
   });
  
}

document.addEventListener('DOMContentLoaded', main);
              
            
!
999px

Console