


Detailed explanation of JavaScript code to implement mouse drag and drop multi-selection function
This article mainly introduces the example of implementing the mouse drag and drop multi-selection function in js. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Recently I made a function of using mouse drag and drop to multiple-select, so I sorted out my ideas and wrote a small demo:
The mask appears:
The block covered by the mask is the selected block (the background color is pink)
The following is the specific code, and the comments are in the text for communication with everyone.
<!DOCTYPE html> <html> <head> <title>鼠标拖拽多选功能</title> <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> <style type="text/css"> *{ box-sizing:border-box; } ul{ width:500px; height:auto; margin:0; padding:20px; font-size: 0; /*需设置定位*/ position:relative; } li{ width:70px; height:70px; margin:10px; padding:0; display:inline-block; vertical-align: top; font-size: 13px; border:1px solid #d9d9d9; } #moveSelected{ position:absolute; background-color: blue; opacity:0.3; border:1px dashed #d9d9d9; top:0; left:0; } .selected{ background-color: pink; } </style> </head> <body> <ul class="list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> <li>14</li> <li>15</li> <li>16</li> <li>17</li> <li>18</li> <li>19</li> <li>20</li> <li>21</li> <li>22</li> <!-- 鼠标拖拽出的遮罩 (定位为 position:absolute)--> <!-- 遮罩最好是在绑定了mouseover事件的元素内部,并且不要阻止遮罩的冒泡事件。这样鼠标移到了遮罩上面,依然可以利用冒泡执行父元素的mouseover事件,就不会出现遮罩只能扩大,不能缩小的情况了(亲自试过) --> <p id="moveSelected"></p> </ul> </body> </html> <script type="text/javascript"> $(document).ready(function(){ let moveSelected=$('#moveSelected')[0]; let flag=false;//是搜开启拖拽的标志 let oldLeft=0;//鼠标按下时的left,top let oldTop=0; let selectedList=[];//拖拽多选选中的块集合 // 鼠标按下时开启拖拽多选,将遮罩定位并展现 $(".list").mousedown(function(event) { flag=true; moveSelected.style.top=event.pageY+'px'; moveSelected.style.left=event.pageX+'px'; oldLeft=event.pageX; oldTop=event.pageY; event.preventDefault(); // 阻止默认行为 event.stopPropagation(); // 阻止事件冒泡 }); // 鼠标移动时计算遮罩的位置,宽 高 $(".list").mousemove(function(event) { if(!flag) return;//只有开启了拖拽,才进行mouseover操作 if(event.pageX<oldLeft){//向左拖 moveSelected.style.left=event.pageX+'px'; moveSelected.style.width=(oldLeft-event.pageX)+'px'; }else{ moveSelected.style.width=(event.pageX-oldLeft)+'px'; } if(event.pageY<oldTop){//向上 moveSelected.style.top=event.pageY+'px'; moveSelected.style.height=(oldTop-event.pageY)+'px'; }else{ moveSelected.style.height=(event.pageY-oldTop)+'px'; } event.preventDefault(); // 阻止默认行为 event.stopPropagation(); // 阻止事件冒泡 }); //鼠标抬起时计算遮罩的right 和 bottom,找出遮罩覆盖的块,关闭拖拽选中开关,清除遮罩数据 $(".list").mouseup(function(event) { moveSelected.style.bottom=Number(moveSelected.style.top.split('px')[0])+Number(moveSelected.style.height.split('px')[0]) + 'px'; moveSelected.style.right=Number(moveSelected.style.left.split('px')[0])+Number(moveSelected.style.width.split('px')[0])+'px'; findSelected(); flag=false; clearDragData(); event.preventDefault(); // 阻止默认行为 event.stopPropagation(); // 阻止事件冒泡 }); $(".list").mouseleave(function(event) { flag=false; moveSelected.style.width=0; moveSelected.style.height=0; moveSelected.style.top=0; moveSelected.style.left=0; event.preventDefault(); // 阻止默认行为 event.stopPropagation(); // 阻止事件冒泡 }); function findSelected(){ let blockList=$('.list').find('li'); for(let i=0;i<blockList.length;i++){ //计算每个块的定位信息 let left=$(blockList[i]).offset().left; let right=$(blockList[i]).width()+left; let top=$(blockList[i]).offset().top; let bottom=$(blockList[i]).height()+top; //判断每个块是否被遮罩盖住(即选中) let leftFlag=moveSelected.style.left.split('px')[0]<=left && left<=moveSelected.style.right.split('px')[0]; let rightFlag=moveSelected.style.left.split('px')[0]<=right && right<=moveSelected.style.right.split('px')[0]; let topFlag=moveSelected.style.top.split('px')[0]<=top && top<=moveSelected.style.bottom.split('px')[0]; let bottomFlag=moveSelected.style.top.split('px')[0]<=bottom && bottom<=moveSelected.style.bottom.split('px')[0]; if((leftFlag || rightFlag) && (topFlag || bottomFlag)){ selectedList.push(blockList[i]); $(blockList[i]).addClass('selected'); } } console.log(selectedList); } function clearDragData(){ moveSelected.style.width=0; moveSelected.style.height=0; moveSelected.style.top=0; moveSelected.style.left=0; moveSelected.style.bottom=0; moveSelected.style.right=0; } }); </script>
The above is the detailed content of Detailed explanation of JavaScript code to implement mouse drag and drop multi-selection function. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools