How to use JavaScript to create a drag div Layer

Source: Internet
Author: User

The drag layer is useful in Web design. For example, in some applications that require custom layout, the control needs to be dragged. The following describes how to meet your needs, by the way, you can learn how to implement the drag layer.

The following is a demo of the effect:

This DIV can be moved. You can test it. JavaScript code
<Script type = "text/javascript"> // <! [CDATA [var $ j = function (id) {return document. getElementById (id) ;}; var getMouseP = function (e) {// to obtain the mouse coordinate, pass the evnet parameter e = e | window. event; var m = (e. pageX | e. pageY )? {X: e. pageX, y: e. pageY }:{ x: e. clientX + document. body. scrollLeft-document. body. clientLeft, y: e. clientY + document. body. scrollTop-document. body. clientTop}; return m ;}; move = function (o, t) {o = $ j (o); t = $ j (t); o. onmousedown = function (ev) {var mxy = getMouseP (ev); // obtain the current mouse coordinate var by = {x: mxy. x-(t. offsetLeft), y: mxy. y-(t. offsetTop)}; o. style. cursor = "move"; document. onmousemove = function (ev) {var mxy = getMouseP (ev); t . Style. left = mxy. x-by.x + "px"; t. style. top = mxy. y-by.y + "px" ;}; document. onmouseup = function () {window. getSelection? Window. getSelection (). removeAllRanges (): document. selection. empty (); this. onmousemove = null ;}} move ("jelle_test_div", "jelle_test_div"); //]> </script>

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.