js realizes the special effect of clicking and dropping. Let’s take a look at the effect picture first Without further ado, let’s get started. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> <script src="jquery.js"></script> <script> window.onload = function () { var str = ''; var len = 20; var aDiv = document.getElementsByTagName('div'); var timer = null; var num = 0; for ( var i=0; i<len; i++ ) { str += '<div style="width:50px; height:50px; background:red; position:absolute; top:0px; left:'+ i*60 +'px;"></div>'; } document.body.innerHTML = str; document.onclick = function () { clearInterval( timer ); timer = setInterval( function () { DM( aDiv[num], 'top', 23, 500 ); num++; if ( num === len ) { clearInterval( timer ); } }, 100 ); }; }; </script> </head> <body> </body> </html> I quoted the encapsulation method here function DM( obj, attr, dir, target, endFn ) { dir = parseInt(getStyle( obj, attr )) < target ? dir : -dir; clearInterval( obj.timer ); obj.timer = setInterval(function () { var speed = parseInt(getStyle( obj, attr )) + dir; // step length if ( speed > target && dir > 0 || speed < target && dir < 0 ) { speed = target; } obj.style[attr] = speed + 'px'; if ( speed == target ) { clearInterval( obj.timer ); /* if ( endFn ) { endFn(); } */ endFn && endFn(); } }, 30); } This is the end of this article about how to implement click-and-drop effects with JS. For more relevant js click-and-drop content, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to implement Docker container self-start
>>: Solution to mysql failure to start due to insufficient disk space in ubuntu
This article shares the specific code for the WeC...
This article example shares the specific code of ...
Table of contents What is a listener in vue Usage...
Preface We already know that MySQL uses the SQL S...
Document mode has the following two functions: 1. ...
We better start paying attention, because HTML Po...
1. Overview The image in Docker is designed in la...
Copy code The code is as follows: <!DOCTYPE ht...
In Linux systems, especially server systems, it i...
This article mainly introduces the Vue project. O...
The excellence of Linux lies in its multi-user, m...
1. Installation of Docker under CentOS8 curl http...
This article describes how to compile and install...
MySQL software installation and database basics a...
Table of contents Preface JS Magic Number Storing...