Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

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


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

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.


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


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


                // 主函数
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;
    } = x + 'px'; = y + 'px';
  // 当鼠标弹起时判断是否移除 mousemove 事件
  document.addEventListener('mouseup', () => {
    if (isDraging) {
      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, {
    document.addEventListener('mousemove', dragWithParams);

document.addEventListener('DOMContentLoaded', main);