jquery uses drag effect to achieve free drag div_jquery

Source: Internet
Author: User
Tags event listener

Accidentally saw the previous do a simple div drag-and-drop effect, modified a little bit of comment, after testing the perfect through the firefox/chrome/ie6-11, now to share.

First of all, the implementation of the principles and points, the main three steps. The first step is to MouseDown the event, when the mouse MouseDown the mouse x and y axes and the left and top of the drag box, and assign true to the drag tag, which means the drag action is ready. The second step is the MouseMove event, where the x and y axes of the mouse are dynamically fetched and then calculated to drag the new left and top of the box and assign a value to drag and drop the effect. The third step is the MouseUp event, the mouse to play when the drag tag value false, drag and drop action completed.

The HTML code is as follows:

<div class= "Divwrap" id= "move1" style= "width:200px"; height:200px; Background:green; border:1px solid red; Position:absolute; top:100px; left:100px; Cursor:move; -moz-user-select:none; -webkit-user-select:none; " ></div>
<div class= "Divwrap" style= width:200px; height:200px Background:brown Red ; Position:absolute; top:300px; left:100px; " >
  

The

JS code is as follows:

(function ($) {$.fn.dragdiv = function (divwrap) {return This.each (function () {var $divMove = $ (this);/mouse can be dragged Drag region var $divWrap = divwrap?
      $divMove. Parents (Divwrap): $divMove//The entire mobile region var MX = 0, my = 0;//define mouse x axis y axis var DX = 0, DY = 0;//define div left, upper position var isdown = false;//mousedown Tag if (document.attachevent) {//ie event listener, stop selection when dragging Div, Firefox and Chrome have been set in CSS-moz-us Er-select:none;
        -webkit-user-select:none;
        $divMove [0].attachevent (' onselectstart '), function () {return false;
      });
        $divMove. MouseDown (function (event) {var event = Event | | window.event;
        MX = Event.clientx;
        my = Event.clienty;
        DX = $divWrap. Offset (). Left;
        DY = $divWrap. Offset (). Top;
      Isdown = true;//Mouse Drag start});
        $ (document). MouseMove (function (event) {var event = Event | | window.event;
 var x = x-axis var y when event.clientx;//mouse slide = event.clienty;//Y-axis if (Isdown) {         $divWrap. css ({"Left": X-mx + DX, "top": Y-my + DY});//div dynamic position Assignment}});
    $divMove. MouseUp (function () {Isdown = false;//mouse Drag end});
  });
};
}) (JQuery);

 $ (document). Ready (function () {$ (#move1). Dragdiv ()//Drag the entire div $ ("#move2"). Dragdiv (". Divwrap");//drag Div head});

Finally, to explain, before beginning to drag and drop action, to prohibit the selection of content, or affect the drag effect. Firefox and Chrome can be set via CSS: {-moz-user-select:none;-webkit-user-select:none;},ie could have written a onselectstart= directly in HTML. return false ", but it seems that the chrome has been a bit affected, so decided to cancel the writing, and then in JS for IE browser to write a onselectstart event.

This widget is simply a drag-and-drop effect, but the compatibility is good, which also used some knowledge points and skills. Of course, you can also use this plugin or the idea inside to continue to expand, write a more complete drag-and-drop plug-ins (such as: Draggable and droppable).

The above mentioned is the entire content of this article, I hope you can enjoy.

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.