js實(shí)現(xiàn)鼠標(biāo)拖拽多選功能示例
最近做了一個(gè)用js實(shí)現(xiàn)鼠標(biāo)拖拽多選的功能,于是整理了一下思路,寫了一個(gè)小demo:
遮罩出現(xiàn):

被遮罩蓋住的,即為選中的塊(背景色為粉色)

下面是具體代碼,注釋已在文中,與大家交流。
<!DOCTYPE html>
<html>
<head>
<title>鼠標(biāo)拖拽多選功能</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;
/*需設(shè)置定位*/
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>
<!-- 鼠標(biāo)拖拽出的遮罩 (定位為 position:absolute)-->
<!-- 遮罩最好是在綁定了mouseover事件的元素內(nèi)部,并且不要阻止遮罩的冒泡事件。這樣鼠標(biāo)移到了遮罩上面,依然可以利用冒泡執(zhí)行父元素的mouseover事件,就不會(huì)出現(xiàn)遮罩只能擴(kuò)大,不能縮小的情況了(親自試過) -->
<div id="moveSelected"></div>
</ul>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
let moveSelected=$('#moveSelected')[0];
let flag=false;//是搜開啟拖拽的標(biāo)志
let oldLeft=0;//鼠標(biāo)按下時(shí)的left,top
let oldTop=0;
let selectedList=[];//拖拽多選選中的塊集合
// 鼠標(biāo)按下時(shí)開啟拖拽多選,將遮罩定位并展現(xiàn)
$(".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(); // 阻止默認(rèn)行為
event.stopPropagation(); // 阻止事件冒泡
});
// 鼠標(biāo)移動(dòng)時(shí)計(jì)算遮罩的位置,寬 高
$(".list").mousemove(function(event) {
if(!flag) return;//只有開啟了拖拽,才進(jìn)行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(); // 阻止默認(rèn)行為
event.stopPropagation(); // 阻止事件冒泡
});
//鼠標(biāo)抬起時(shí)計(jì)算遮罩的right 和 bottom,找出遮罩覆蓋的塊,關(guān)閉拖拽選中開關(guān),清除遮罩?jǐn)?shù)據(jù)
$(".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(); // 阻止默認(rèn)行為
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(); // 阻止默認(rèn)行為
event.stopPropagation(); // 阻止事件冒泡
});
function findSelected(){
let blockList=$('.list').find('li');
for(let i=0;i<blockList.length;i++){
//計(jì)算每個(gè)塊的定位信息
let left=$(blockList[i]).offset().left;
let right=$(blockList[i]).width()+left;
let top=$(blockList[i]).offset().top;
let bottom=$(blockList[i]).height()+top;
//判斷每個(gè)塊是否被遮罩蓋?。催x中)
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用javaScript實(shí)現(xiàn)鼠標(biāo)拖拽事件
- 鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn)
- js實(shí)現(xiàn)使用鼠標(biāo)拖拽切換圖片的方法
- JS鼠標(biāo)拖拽實(shí)例分析
- jsMind通過鼠標(biāo)拖拽的方式調(diào)整節(jié)點(diǎn)位置
- js實(shí)現(xiàn)鼠標(biāo)拖拽div左右滑動(dòng)
- JavaScript鼠標(biāo)拖拽事件詳解
- 超酷的鼠標(biāo)拖拽翻頁(分頁)效果實(shí)現(xiàn)javascript代碼
- js實(shí)現(xiàn)鼠標(biāo)拖拽縮放div實(shí)例代碼
- JavaScript實(shí)現(xiàn)div的鼠標(biāo)拖拽效果
相關(guān)文章
基于JS實(shí)現(xiàn)動(dòng)態(tài)跟隨特效的示例代碼
這篇文章主要介紹了如何利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)跟隨特效,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)JS有一定的幫助,感興趣的小伙伴可以了解一下2022-06-06
npm?install報(bào)錯(cuò)Error:EPERM:operation?not?permitted,rename解決
這篇文章主要給大家介紹了關(guān)于npm?install報(bào)錯(cuò)Error:EPERM:operation?not?permitted,rename的解決辦法,文中介紹了可能遇到的多種原因以及解決辦法,需要的朋友可以參考下2024-01-01
js實(shí)現(xiàn)一個(gè)省市區(qū)三級(jí)聯(lián)動(dòng)選擇框代碼分享
省市區(qū)三級(jí)聯(lián)動(dòng)在填寫表單時(shí)有關(guān)地址這一塊顯得尤為重要,直接提高了用戶的填寫速度與準(zhǔn)確度,接下來本文使用js代碼實(shí)現(xiàn)一個(gè),感興趣的你可以參考下希望可以幫助到你2013-03-03
js 只能輸入數(shù)字和小數(shù)點(diǎn)的文本框改進(jìn)版
以前的版本不能輸入退格鍵等功能。2009-04-04
JavaScript DOM 編程藝術(shù)(第2版)讀書筆記(JavaScript的最佳實(shí)踐)
閱讀了本書第五章關(guān)于使用JavaScript的最佳實(shí)踐,大部分的建議之前都有耳聞,不過閱讀之后有更深的體會(huì)2013-10-10
用innerHTML &符號(hào)副值給文本框后會(huì)變成&amp;的方法
用innerHTML?。Ψ?hào)副值給文本框后會(huì)變成&amp;的方法...2007-07-07
webpack中Loader和Plugin的區(qū)別小結(jié)
本文主要介紹了webpack中Loader和Plugin的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06

