Home >Web Front-end >JS Tutorial >JS implements super simple mouse dragging effect_javascript skills

JS implements super simple mouse dragging effect_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:34:181220browse

The example in this article describes how to implement a super simple mouse drag effect with JS. Share it with everyone for your reference, the details are as follows:

Here, use the shortest possible JavaScript code to write a JS drag. The function itself is 287 characters. . . If compatibility and variable encapsulation are not considered, it can be shorter.

The screenshot of the running effect is as follows:

The online demo address is as follows:

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

The specific code is as follows:

<title>尽可能短的写一个JS拖动</title>
<body>
<div id="demo" style="width:100px; height:100px; position:absolute; background-color:silver;"></div>
<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 be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn