JQuery implements the example code (Common Code) for div drag at will, jquerydiv

Source: Internet
Author: User

JQuery implements the example code (Common Code) for div drag at will, jquerydiv

Pay attention to the js location, which should be placed close to it. If it is overwritten by others, it cannot be moved.

For example:

<Div id = "move"> movable DIV </div>

Introduce jquery. js, jquery-ui.js,

<script scr="http://code.jquery.com/jquery-1.10.2.js"></script><script scr="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

One sentence:

$("#move").draggable();

If you want to click it, the mouse changes to the hand shape:

$("#move").mousedown(function(){$(this).css("cursor","pointer");}).mouseup(function(){$(this).css("cursor","default");});

The following is a general-purpose code for jquery to implement the common method of dragging a div.

<Script type = "text/javascript"> <! -- $ (Document ). ready (function () {$ (". show "). mousedown (function (e) // e mouse event {cursor (this).css ("cursor", "move"); // change the shape of the mouse pointer var offset =$ (this ). offset (); // The Position of the DIV in the page var x = e. pageX-offset. left; // obtain the distance between the mouse pointer and the left border of the DIV element. var y = e. pageY-offset. top; // obtain the distance between the mouse pointer and the upper boundary of the DIV element $ (document ). bind ("mousemove", function (ev) // bind the move event of the mouse. Because the cursor must have an effect outside the DIV element, use the doucment event, instead of DIV element events {$ (". show "). stop (); // after adding this, var _ x = ev. pageX-x; // obtain the value var _ y = ev. pageY-y; // get the value that moves in the y axis direction $ (". show "). animate ({left: _ x + "px", top: _ y + "px"}, 10) ;}); $ (document ). mouseup (function () {$ (". show "example .css (" cursor "," default "); $ (this ). unbind ("mousemove") ;}) // --> </script>

Articles you may be interested in:
  • JQuery can drag the div to implement the Code help house correction version
  • Use JQuery + EasyDrag to bring up a drag-and-drop Div. At the same time, pass the value to the Div and return the selected value of the Div.
  • Jquery allows you to drag a DIV to customize an instance to save data.
  • Jquery div drag effect sample code
  • Implementation principle and example of jQuery drag div, move div, and pop-up layer
  • JQuery implements the DIV layer fade-in and fade-out drag effect method
  • JQuery implements Div drag + keyboard control
  • Jquery implements a DIV instance that can be dragged like JqueryUi
  • JQuery click to bring up the Div Layer window (you can close and drag it)

Related Article

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.