使用純JS實現(xiàn)checkbox的框選效果(鼠標拖拽多選)

主要思路
用一個盒子作為選區(qū),通過定位讓其固定在左上角,由于沒有給定選區(qū)元素的寬高所以默認不顯示,在 onmousemove 中動態(tài)獲取選區(qū)定位的top left bottom right四個屬性,同時將鼠標拖拽的距離作為選區(qū)的寬高,由于給選區(qū)元素的css設(shè)置了border就呈現(xiàn)出如圖所示的框選效果。(注意:要想自己手動勾選復(fù)選框,要給選區(qū)元素的css設(shè)置pointer-events: none;否則點擊復(fù)選框的事件會被選區(qū)遮擋)然后獲取每個小復(fù)選框的位置和此時選區(qū)的位置進行比較來判斷是否在選區(qū)內(nèi),如果滿足條件就把復(fù)選框的checked屬性設(shè)置為true。
css 代碼如下
* {
user-select: none;
}
#ul {
position: relative;
width: 300px;
height: auto;
margin: 0 auto;
padding: 100px;
}
li {
display: inline-block;
margin: 5px;
}
input {
width: 30px;
height: 30px;
}
#moveSelected {
position: fixed;
top: 0;
left: 0;
border: 1px dashed #2783F5;
pointer-events: none;
}html結(jié)構(gòu)如下
<ul id="box">
<li><input type="checkbox" id="check1"></li>
<li><input type="checkbox" id="check2"></li>
<li><input type="checkbox" id="check3"></li>
<li><input type="checkbox" id="check4"></li>
<li><input type="checkbox" id="check5"></li>
<li><input type="checkbox" id="check6"></li>
<li><input type="checkbox" id="check7"></li>
<li><input type="checkbox" id="check8"></li>
<li><input type="checkbox" id="check9"></li>
<li><input type="checkbox" id="check10"></li>
<li><input type="checkbox" id="check11"></li>
<li><input type="checkbox" id="check12"></li>
<li><input type="checkbox" id="check13"></li>
<li><input type="checkbox" id="check14"></li>
<li><input type="checkbox" id="check15"></li>
<li><input type="checkbox" id="check16"></li>
<li><input type="checkbox" id="check17"></li>
<li><input type="checkbox" id="check18"></li>
<li><input type="checkbox" id="check19"></li>
<li><input type="checkbox" id="check20"></li>
<!-- 選區(qū) -->
<li>
<div id="moveSelected"></div>
</li>
</ul>js主要邏輯如下
window.onload = function() {
let flag = false;//是否開啟拖拽
let oldLeft = 0; //鼠標按下時的位置
let oldTop = 0;
let box = document.getElementById('box') //操作區(qū)
let moveSelected = document.getElementById("moveSelected");//選區(qū)
let checkboxs = box.getElementsByTagName("input"); //復(fù)選框
// 鼠標按下時開啟拖拽,給選區(qū)設(shè)置定位
box.onmousedown = function(e) {
flag = true;
moveSelected.style.top = e.pageY + 'px';
moveSelected.style.left = e.pageX + 'px';
oldLeft = e.pageX;
oldTop = e.pageY;
}
// 鼠標移動時計算選區(qū)的位置和大小
box.onmousemove = function(e) {
if (!flag) return;
if (e.pageX < oldLeft) { //表示左移
moveSelected.style.left = e.pageX + 'px';
moveSelected.style.width = (oldLeft - e.pageX) + 'px'; //向左移動的距離作為選區(qū)的寬
} else {
moveSelected.style.width = (event.pageX - oldLeft) + 'px';
}
if (e.pageY < oldTop) { //向上移動
moveSelected.style.top = e.pageY + 'px';
moveSelected.style.height = (oldTop - e.pageY) + 'px';
} else {
moveSelected.style.height = (e.pageY - oldTop) + 'px';
}
//通過得到的left和top加上元素自身的寬高來計算選區(qū)的right和bottom
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';
//找出選中的區(qū)域并激活
for (let i = 0; i < checkboxs.length; i++) {
//計算每個checkbox的位置信息
let left = checkboxs[i].offsetLeft + box.offsetLeft;
let right = checkboxs[i].offsetWidth + left;
let top = checkboxs[i].offsetTop + box.offsetTop;
let bottom = checkboxs[i].offsetHeight + top;
//判斷是否在選擇區(qū)
let leftCover = moveSelected.style.left.split('px')[0] <= left && left <= moveSelected.style.right.split('px')[0];
let rightCover = moveSelected.style.left.split('px')[0] <= right && right <= moveSelected.style.right.split('px')[0];
let topCover = moveSelected.style.top.split('px')[0] <= top && top <= moveSelected.style.bottom.split('px')[0];
let bottomCover = moveSelected.style.top.split('px')[0] <= bottom && bottom <= moveSelected.style.bottom.split('px')[0];
if ((leftCover || rightCover) && (topCover || bottomCover)) {
checkboxs[i].checked = true;//激活復(fù)選框
}
}
}
//鼠標抬起時清空選區(qū)數(shù)據(jù)
box.onmouseup = function(e) {
if (!flag) return;
flag = false;
moveSelected.style.width = 0;
moveSelected.style.height = 0;
moveSelected.style.top = 0;
moveSelected.style.left = 0;
moveSelected.style.bottom = 0;
moveSelected.style.right = 0;
}
// 鼠標超出ul選區(qū)失效
box.onmouseleave = function(e) {
flag = false;
moveSelected.style.width = 0;
moveSelected.style.height = 0;
moveSelected.style.top = 0;
moveSelected.style.left = 0;
}
}總結(jié)
到此這篇關(guān)于使用純JS實現(xiàn)checkbox的框選效果的文章就介紹到這了,更多相關(guān)JS實現(xiàn)checkbox鼠標拖拽多選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中的altKey 和 Event屬性大全
本文給大家介紹javascript中的altkey和event屬性大全,涉及到altkey和event屬性語法定義及用法,本文介紹的非常詳細,感興趣的朋友一起看看吧2015-11-11
20個常見的JavaScript數(shù)組操作總結(jié)
JavaScript中的Array對象與其他編程語言中的數(shù)組一樣,是一組數(shù)據(jù)的集合。在JavaScript中,數(shù)組里面的數(shù)據(jù)可以是不同類型的,并具有用于執(zhí)行數(shù)組常見操作的方法,本文整理了一些常用的,需要的可以參考一下2022-09-09
arctext.js實現(xiàn)文字平滑彎曲弧形效果的插件
這篇文章主要介紹了arctext.js實現(xiàn)文字平滑彎曲弧形效果的插件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
java與javascript之間json格式數(shù)據(jù)互轉(zhuǎn)介紹
對象轉(zhuǎn)為字符串:通過JSON.encode方法,這個是json.js里面的方法,引入到當前文件就可以了,下面整理的比較詳細一點,感興趣的朋友不要錯過2013-10-10
js?通過Object.defineProperty()?定義和控制對象屬性
這篇文章主要介紹了js?通過Object.defineProperty()?定義和控制對象屬性,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08
在Javascript里訪問SharePoint列表數(shù)據(jù)的實現(xiàn)方法
在進行SharePoint訂制的時候經(jīng)常會遇到開發(fā)手段受限制的問題,比如通常公司都會限制服務(wù)器的訪問以及部署,很多開發(fā)都只能夠在客戶端來進行2011-05-05
Jquery+javascript實現(xiàn)支付網(wǎng)頁數(shù)字鍵盤
這篇文章主要為大家詳細介紹了Jquery+javascript實現(xiàn)支付網(wǎng)頁數(shù)字鍵盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12

