Javascript to achieve ultra-simple mouse dragging effect _ javascript skills

Source: Internet
Author: User
This article mainly introduces JavaScript to achieve ultra-simple mouse drag effect, and involves related skills related to JavaScript response to mouse event dynamic page elements. it has some reference value, for more information about JS implementation, see the following example. We will share this with you for your reference. The details are as follows:

Use JavaScript code as short as possible to write a JS drag. the function itself contains 287 characters... It can be shorter if compatibility and variable encapsulation are not considered.

The running effect is as follows:

The online demo address is as follows:

Http://demo.jb51.net/js/2015/js-s-drug-demo/

The code is as follows:

Write a JS drag as short as possible

Script function dragable (id) {var d = document, o = d. getElementById (id), s = o. style, x, y, p = 'onmousemove '; o. onmousedown = function (e) {e = e | event; x = e. clientX-o.offsetLeft; y = e. clientY-o.offsetTop; d [p] = function (e) {e = e | event; s. left = e. clientX-x + 'px '; s. top = e. clientY-y + 'px '}; d. onmouseup = function () {d [p] = null }}dragable ("demo"); script

I hope this article will help you design JavaScript programs.

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.