JS Implementation Picture Drag Change Order drawings _javascript Tips

Source: Internet
Author: User
In a Web page, you need to change the position of multiple elements, which can be implemented by dragging elements. A global property draggable is added to the HTML5 to control whether the element can be dragged by setting the True/false.

Drag the picture below as an example, using jquery to achieve: The page has more than one picture, drag a picture to the middle of the other two pictures, you can insert the picture's position between the two figure.
Copy Code code as follows:

<! DOCTYPE html>
<style>
. img-div img {
width:200px;
height:200px;
Float:left;
}
. img-div {
Float:left;
}
. drop-left,.drop-right {
width:50px;
height:200px;
Float:left;
}
</style>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" ></script>
<script>
$ (document). Ready (function () {

The parent div of the picture being dragged
var $srcImgDiv = null;

Start dragging
$ (". Img-div img"). Bind ("DragStart", function () {
$srcImgDiv = $ (this). parent ();
});

The event that is triggered when you drag over the. drop-left,.drop-right
$ (". Drop-left,.drop-right"). Bind ("DragOver", function (event) {

You must set the Allow drag-and-drop by Event.preventdefault ()
Event.preventdefault ();
});

End drag to release mouse event
$ (". Drop-left"). Bind ("Drop", function (event) {
Event.preventdefault ();
if ($srcImgDiv [0]!= $ (this). Parent () [0]) {
$ (this). Parent (). before ($SRCIMGDIV);
}
});
$ (". Drop-right"). Bind ("Drop", function (event) {
Event.preventdefault ();
if ($srcImgDiv [0]!= $ (this). Parent () [0]) {
$ (this). Parent (), after ($SRCIMGDIV);
}
});

});
</script>
<body>
<div class= "Img-div" >
<div class= "Drop-left" ></div>

<div class= "Drop-right" ></div>
</div>
<div class= "Img-div" >
<div class= "Drop-left" ></div>

<div class= "Drop-right" ></div>
</div>
<div class= "Img-div" >
<div class= "Drop-left" ></div>

<div class= "Drop-right" ></div>
</div>
<div class= "Img-div" >
<div class= "Drop-left" ></div>

<div class= "Drop-right" ></div>
</div>
</body>

DragStart is the event that starts dragging the element, DragOver is the event that is dragged above the element, and the drop is the event that the mouse is dragged over to release.

Draggable= "true" means that the IMG element can be dragged, but in fact the IMG default is drag, so this property can also be removed, and if you want to drag the div element then you need to set draggable= "true".

Class Drop-left and Drop-right div elements are placed on the left right side of the picture to receive other pictures dragged to this position.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.