Make Div Draggable using CSS

前端 未结 9 740
死守一世寂寞
死守一世寂寞 2020-12-07 22:26

I want to make my div tag with id \"drag_me\" to be draggable up to the length of 300px from left and 460px from top, only using CSS.

I also want to make it resizabl

相关标签:
9条回答
  • 2020-12-07 23:04

    CSS is designed to describe the presentation of documents. It has a few features for changing that presentation in reaction to user interaction (primarily :hover for indicating that you are now pointing at something interactive).

    Making something draggable isn't a simple matter of presentation. It is firmly in the territory of interactivity logic, which is handled by JavaScript.

    What you want is not achievable.

    0 讨论(0)
  • 2020-12-07 23:04

    You can do it now by using the CSS property -webkit-user-drag:

    #drag_me {
      -webkit-user-drag: element;
    }
    <div draggable="true" id="drag_me">
      Your draggable content here
    </div>

    This property is only supported by webkit browsers, such as Safari or Chrome, but it is a nice approach to get it working using only CSS.

    The HTML5 draggable attribute is only set to ensure dragging works for other browsers.

    You can find more information here: http://help.dottoro.com/lcbixvwm.php

    0 讨论(0)
  • 2020-12-07 23:07
    $('#dialog').draggable({ handle: "#tblOverlay" , scroll: false });
    
        // Pop up Window 
              <div id="dialog">
                            <table  id="tblOverlay">
                                <tr><td></td></tr>
                             <table>
               </div>
    

    Options:

    1. handle : Avoids the sticky scroll bar issue. Sometimes your mouse pointer will stick to the popup window while dragging.
    2. scroll : Prevent popup window to go beyond parent page or out of current screen.
    0 讨论(0)
  • 2020-12-07 23:10

    Only using css techniques this does not seem possible to me. But you could use jqueryui draggable:

    $('#drag_me').draggable();
    
    0 讨论(0)
  • 2020-12-07 23:12

    Draggable div not possible only with CSS, if you want draggable div you must need to use javascript.

    http://jqueryui.com/draggable/

    0 讨论(0)
  • 2020-12-07 23:19

    I found this is really helpful:

    // Make the DIV element draggable:
    dragElement(document.getElementById("mydiv"));
    
    function dragElement(elmnt) {
      var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
      if (document.getElementById(elmnt.id + "header")) {
        // if present, the header is where you move the DIV from:
        document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
      } else {
        // otherwise, move the DIV from anywhere inside the DIV:
        elmnt.onmousedown = dragMouseDown;
      }
    
      function dragMouseDown(e) {
        e = e || window.event;
        e.preventDefault();
        // get the mouse cursor position at startup:
        pos3 = e.clientX;
        pos4 = e.clientY;
        document.onmouseup = closeDragElement;
        // call a function whenever the cursor moves:
        document.onmousemove = elementDrag;
      }
    
      function elementDrag(e) {
        e = e || window.event;
        e.preventDefault();
        // calculate the new cursor position:
        pos1 = pos3 - e.clientX;
        pos2 = pos4 - e.clientY;
        pos3 = e.clientX;
        pos4 = e.clientY;
        // set the element's new position:
        elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
        elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
      }
    
      function closeDragElement() {
        // stop moving when mouse button is released:
        document.onmouseup = null;
        document.onmousemove = null;
      }
    }
    #mydiv {
      position: absolute;
      z-index: 9;
      background-color: #f1f1f1;
      border: 1px solid #d3d3d3;
      text-align: center;
    }
    
    #mydivheader {
      padding: 10px;
      cursor: move;
      z-index: 10;
      background-color: #2196F3;
      color: #fff;
    }
     <!-- Draggable DIV -->
    <div id="mydiv">
      <!-- Include a header DIV with the same name as the draggable DIV, followed by "header" -->
      <div id="mydivheader">Click here to move</div>
      <p>Move</p>
      <p>this</p>
      <p>DIV</p>
    </div> 

    I hope you can use it to!

    0 讨论(0)
提交回复
热议问题