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

主要思路
用一個(gè)盒子作為選區(qū),通過(guò)定位讓其固定在左上角,由于沒有給定選區(qū)元素的寬高所以默認(rèn)不顯示,在 onmousemove 中動(dòng)態(tài)獲取選區(qū)定位的top left bottom right四個(gè)屬性,同時(shí)將鼠標(biāo)拖拽的距離作為選區(qū)的寬高,由于給選區(qū)元素的css設(shè)置了border就呈現(xiàn)出如圖所示的框選效果。(注意:要想自己手動(dòng)勾選復(fù)選框,要給選區(qū)元素的css設(shè)置pointer-events: none;否則點(diǎn)擊復(fù)選框的事件會(huì)被選區(qū)遮擋)然后獲取每個(gè)小復(fù)選框的位置和此時(shí)選區(qū)的位置進(jìn)行比較來(lái)判斷是否在選區(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; //鼠標(biāo)按下時(shí)的位置
let oldTop = 0;
let box = document.getElementById('box') //操作區(qū)
let moveSelected = document.getElementById("moveSelected");//選區(qū)
let checkboxs = box.getElementsByTagName("input"); //復(fù)選框
// 鼠標(biāo)按下時(shí)開啟拖拽,給選區(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;
}
// 鼠標(biāo)移動(dòng)時(shí)計(jì)算選區(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'; //向左移動(dòng)的距離作為選區(qū)的寬
} else {
moveSelected.style.width = (event.pageX - oldLeft) + 'px';
}
if (e.pageY < oldTop) { //向上移動(dòng)
moveSelected.style.top = e.pageY + 'px';
moveSelected.style.height = (oldTop - e.pageY) + 'px';
} else {
moveSelected.style.height = (e.pageY - oldTop) + 'px';
}
//通過(guò)得到的left和top加上元素自身的寬高來(lái)計(jì)算選區(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++) {
//計(jì)算每個(gè)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ù)選框
}
}
}
//鼠標(biāo)抬起時(shí)清空選區(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;
}
// 鼠標(biāo)超出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實(shí)現(xiàn)checkbox的框選效果的文章就介紹到這了,更多相關(guān)JS實(shí)現(xiàn)checkbox鼠標(biāo)拖拽多選內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
點(diǎn)選TOP后并不是直接跳到頁(yè)頂?shù)?,而是滾動(dòng)上去
滾動(dòng)至頁(yè)頂,比以前的直接跳到頁(yè)頂效果好很多,正的很不錯(cuò)2008-09-09
JavaScript實(shí)現(xiàn)六種網(wǎng)頁(yè)圖片輪播效果詳解
在網(wǎng)頁(yè)中,我們經(jīng)常會(huì)看到各種輪播圖的效果,它們到底是怎樣實(shí)現(xiàn)的呢?本文將為大家詳細(xì)介紹一下六種不同的輪播效果的實(shí)現(xiàn),需要的可以參考一下2021-12-12
Js判斷參數(shù)(String,Array,Object)是否為undefined或者值為空
在一些前端控件要提交數(shù)據(jù)到服務(wù)器端的數(shù)據(jù)驗(yàn)證過(guò)程中,需要判斷提交的數(shù)據(jù)是否為空。如果是普通表單的字符串?dāng)?shù)據(jù),只需要在 trim 后判斷 length 即可,而這里需要的數(shù)據(jù)可以是各種不同的類型,通過(guò) JSON.stringify(data) 進(jìn)行序列化后再傳遞2013-11-11
25個(gè)讓你眼前一亮的JavaScript代碼技巧分享
學(xué)習(xí)強(qiáng)大的JavaScript一行代碼,能夠節(jié)省你的時(shí)間和代碼量,所以本文為大家整理了25個(gè)JavaScript實(shí)用代碼技巧,感興趣的小伙伴可以了解一下2023-07-07
chrome下img加載對(duì)height()的影響示例探討
這篇文章主要介紹了chrome下img加載對(duì)height()的影響,需要的朋友可以參考下2014-05-05
使用egg.js實(shí)現(xiàn)手機(jī)、驗(yàn)證碼注冊(cè)的項(xiàng)目實(shí)踐
本文主要介紹了使用egg.js實(shí)現(xiàn)手機(jī)、驗(yàn)證碼注冊(cè)的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
js jquery ajax的幾種用法總結(jié)(及優(yōu)缺點(diǎn)介紹)
本篇文章只要介紹了js jquery ajax的幾種用法總結(jié)(及優(yōu)缺點(diǎn)介紹),需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
BootStrap modal模態(tài)彈窗使用小結(jié)
這篇文章主要為大家詳細(xì)介紹了BootStrap modal模態(tài)彈窗使用小結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Javascript實(shí)現(xiàn)鼠標(biāo)框選操作 不是點(diǎn)擊選取
這篇文章主要介紹了Javascript實(shí)現(xiàn)鼠標(biāo)框選操作,不是點(diǎn)擊選取,利用鼠標(biāo)進(jìn)行框選,感興趣的小伙伴們可以參考一下2016-04-04

