Make Div Draggable using CSS

前端 未结 9 741
死守一世寂寞
死守一世寂寞 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:20

    After going down the rabbit-hole of trying to do this myself by copy-pasting various code-snippets from Stack Overflow, I would highly recommend just using the InteractJS library, which allows you to create a draggable and resizable div (somewhat) easily.

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

    This is the best you can do without JavaScript:

    [draggable=true] {
      cursor: move;
    }
    
    .resizable {
      overflow: scroll;
      resize: both;
      max-width: 300px;
      max-height: 460px;
      border: 1px solid black;
      min-width: 50px;
      min-height: 50px;
      background-color: skyblue;
    }
    <div draggable="true" class="resizable"></div>

    Demo

    • draggable attribute on HTML5 Rocks
    • CSS resize property on MDN
    0 讨论(0)
  • 2020-12-07 23:27

    You can take a look at HTML 5, but I don't think you can restrict the area within you can drag it, just the destination:

    http://www.w3schools.com/html/html5_draganddrop.asp

    And if you don't mind using some great library, I would encourage you to try Dragula.

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