Moveable/draggable

前端 未结 7 591
我在风中等你
我在风中等你 2020-11-28 02:31

This is my updated and modified script, it works completely, except I would like to universalize it... observe the **** how can I make it so that I don\'t have to do f

7条回答
  •  清酒与你
    2020-11-28 03:20

    I modified Shaedo's code a little bit, wraps it in a function and add a feature that you can drag an element by only parts of it or its children, say the title bar of a div. Note in this demo, you can only drag the red area to move the blue area.

    function makeDragable(dragHandle, dragTarget) {
      let dragObj = null; //object to be moved
      let xOffset = 0; //used to prevent dragged object jumping to mouse location
      let yOffset = 0;
    
      document.querySelector(dragHandle).addEventListener("mousedown", startDrag, true);
      document.querySelector(dragHandle).addEventListener("touchstart", startDrag, true);
    
      /*sets offset parameters and starts listening for mouse-move*/
      function startDrag(e) {
        e.preventDefault();
        e.stopPropagation();
        dragObj = document.querySelector(dragTarget);
        dragObj.style.position = "absolute";
        let rect = dragObj.getBoundingClientRect();
    
        if (e.type=="mousedown") {
          xOffset = e.clientX - rect.left; //clientX and getBoundingClientRect() both use viewable area adjusted when scrolling aka 'viewport'
          yOffset = e.clientY - rect.top;
          window.addEventListener('mousemove', dragObject, true);
        } else if(e.type=="touchstart") {
          xOffset = e.targetTouches[0].clientX - rect.left;
          yOffset = e.targetTouches[0].clientY - rect.top;
          window.addEventListener('touchmove', dragObject, true);
        }
      }
    
      /*Drag object*/
      function dragObject(e) {
        e.preventDefault();
        e.stopPropagation();
    
        if(dragObj == null) {
          return; // if there is no object being dragged then do nothing
        } else if(e.type=="mousemove") {
          dragObj.style.left = e.clientX-xOffset +"px"; // adjust location of dragged object so doesn't jump to mouse position
          dragObj.style.top = e.clientY-yOffset +"px";
        } else if(e.type=="touchmove") {
          dragObj.style.left = e.targetTouches[0].clientX-xOffset +"px"; // adjust location of dragged object so doesn't jump to mouse position
          dragObj.style.top = e.targetTouches[0].clientY-yOffset +"px";
        }
      }
    
      /*End dragging*/
      document.onmouseup = function(e) {
        if (dragObj) {
          dragObj = null;
          window.removeEventListener('mousemove', dragObject, true);
          window.removeEventListener('touchmove', dragObject, true);
        }
      }
    }
    
    makeDragable('#handle', '#moveable')
    #moveable {
        width: 100px;
        height: 100px;
        background: blue;
    }
    
    #handle {
        width: 50px;
        height: 50px;
        cursor: move;
        background: red;
    }

提交回复
热议问题