How to implement a drag-and-drop div from scratch with JavaScript?

前端 未结 8 729
孤独总比滥情好
孤独总比滥情好 2020-12-08 05:07

It should be a combination of CSS and JavaScript. The steps to do should be:

  1. Make it on top of all other elements (which property to specify?)
  2. Catch t
8条回答
  •  渐次进展
    2020-12-08 05:24

    The jQuery Way:

    Check out the jQueryUI addons draggable and droppable.

    Literally hundreds of hours have been invested into the jQuery framework to make complicated tasks like this almost trivial. Take advantage of the jQuery team's efforts to make programming rich cross-browser applications easier on us all ;)

    Chuck Norris' Way:

    If you insist on trying this with raw javascript. You'll want to do a few things. One, programmatically set all draggable items to a relative/absolute positioning. If you click a particular item, cause it's top/left values in CSS to reflect the changes made by the x,y axis of the mouse until the click is released. Additionally, you'll want to update the z-index of each draggable when it's clicked to bring it into view.

    Tutorial: How to Drag and Drop with Javascript

提交回复
热议问题