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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

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

Packages

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.

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 class="container">
  <div class="dialog">
    dialog content
    <div class="drag-bar-left"></div>
    <div class="drag-bar-right"></div>
    <div class="drag-bar-top"></div>
    <div class="drag-bar-bottom"></div>
    <div class="mask-drag"></div>
  </div>
</div>


              
            
!

CSS

              
                *{
  padding:0;
  margin:0;
}
.container{
  width:100%;
  height:100%;
  position:relative;
}
.dialog{
  width:150px;
  height:150px;
  position:absolute;
  left:50px;
  top:50px;
  background:gray;
}

.drag-bar-left{
  background:transparent;
  position: absolute;
  left:0;
  top:0;
  bottom:0;
  width:10px;
  cursor: move;
  display: block;
}
.drag-bar-right{
  background:transparent;
  position: absolute;
  right:0;
  top:0;
  bottom:0;
  width:10px;
  cursor: move;
  display: block;
}
.drag-bar-top{
  background:transparent;
  position: absolute;
  left:0;
  top:0;
  right:0;
  height:10px;
  cursor: move;
  display: block;
}
.drag-bar-bottom{
  background:transparent;
  position: absolute;
  left:0;
  right:0;
  bottom:0;
  height:10px;
  cursor: move;
  display: block;
}

/*  拖动时遮住整个弹框放置鼠标触发了里面的按钮 */
.mask-drag{
  position: absolute;
  cursor: move;
  user-select: none;
  left:0;
  top:0;
}
              
            
!

JS

              
                  
// carried out
initDragDialog();

/*
    * DOM selector
    * @param selector {String} css selector for element
    * @param context {Element} root element
    * usage: $$('head')
    * result: You will get <head> element
    */
function $$(selector, context) {
    context = context || document
    const elements = context.querySelectorAll(selector)
    return elements.length == 1
    ? Array.prototype.slice.call(elements)[0]
    : Array.prototype.slice.call(elements)
}
/**
 * The pop-up window supports dragging
 */
function initDragDialog(){

    // Drag and drop function (mainly to trigger three events: onpointerdown\onpointermove\onpointerup)
    const drag = $$('.dialog');
    const divMask = $$('.mask-drag');

    const dragBarTop = $$('.drag-bar-top')
    const dragBarBottom = $$('.drag-bar-bottom')
    const dragBarLeft = $$('.drag-bar-left')
    const dragBarRight = $$('.drag-bar-right')

    dragBarTop.addEventListener('pointerdown',pointerDownLisenter)
    dragBarBottom.addEventListener('pointerdown',pointerDownLisenter)
    dragBarLeft.addEventListener('pointerdown',pointerDownLisenter)
    dragBarRight.addEventListener('pointerdown',pointerDownLisenter)

    // When you click on an object, you can use the drag object. Move and up are the global area, that is, the entire document is common. The document object should be used instead of the drag object (otherwise, when the drag object is used, the object can only move to the right or down )

    function pointerDownLisenter (e) {
    e = e || window.event // Compatible with IE browser
    const diffX = e.clientX-drag.offsetLeft // The distance from the moment the mouse clicks on the object relative to the left border of the object = the distance of the position when clicked relative to the leftmost left of the browser - the distance of the left border of the object relative to the leftmost left of the browser
    const diffY = e.clientY-drag.offsetTop

    /* Low version IE bug: When an object is dragged out of the browser’s visual window, a scroll bar will appear,
    The solution is to use two unique methods setCapture()\releaseCapture() in IE browser, these two methods,
    You can let the mouse slide outside the browser to capture the event, and our bug is when the mouse moves out of the browser,
    The function that the limit is exceeded becomes invalid. In this way, this problem can be solved. Note: These two methods are used in onpointerdown and onpointerup */
    if (typeof drag.setCapture !=='undefined') {
        drag.setCapture()
    }
    
    // Cover the pop-up window
    divMask.style.width = '100%';
    divMask.style.height = '100%';
    
    // Prevent the selected event from being triggered when the dragging speed is too fast
    document.body.style.userSelect ='none'
    
    document.onpointermove = function (e) {
        e = e || window.event // Compatible with IE browser
        let left = e.clientX-diffX
        let top = e.clientY-diffY

        // Control the range of dragged objects can only be in the browser window, and scroll bars are not allowed
        // if (left <0) {
        //     left = 0
        // } else if (left> window.innerWidth-drag.offsetWidth) {
        //     left = window.innerWidth-drag.offsetWidth
        // }
        // if (top <0) {
        //     top = 0
        // } else if (top> window.innerHeight-drag.offsetHeight) {
        //     top = window.innerHeight-drag.offsetHeight
        // }
        
        if (left < -drag.offsetWidth + 100) {
          left = -drag.offsetWidth + 100
        } else if (left > window.innerWidth  - 100) {
          left = window.innerWidth - 100
        }
        if (top < -drag.offsetHeight + 100) {
          top = -drag.offsetHeight + 100
        } else if (top > window.innerHeight - 100) {
          top = window.innerHeight - 100
        }

        // Regain the distance of the object when moving, and solve the phenomenon of shaking when dragging
        drag.style.left = left +'px'
        drag.style.top = top +'px'
        drag.style.transform =''
    }
    document.onpointerup = function (e) {// no longer move when the mouse pops up
        this.onpointermove = null
        this.onpointerup = null // Prevent the mouse from looping after it pops up (that is, prevent the mouse from moving when it is put on)

        // Fix low version ie bug
        if (typeof drag.releaseCapture !=='undefined') {
        drag.releaseCapture()
        }
        
        // Restore the hidden mask layer
        divMask.style.width = '0';
        divMask.style.height = '0';
        document.body.style.userSelect =''
    }
    }

}
              
            
!
999px

Console