今天應(yīng)同學(xué)要求唱捣,需要寫一個(gè)像Excel那樣框選高亮,并且實(shí)現(xiàn)框選區(qū)域?qū)崿F(xiàn)反選功能概说。要我用原生js寫碧注,由于沒什么經(jīng)驗(yàn)翻閱了很多資料,第一次寫文章希望各位指出不足L桥狻萍丐!
上來先建表
<div class="table-container" >
<table class="table" id="dataGrid" align="center">
<tr id="title">
<th> </th>
<th>水果</th>
<th>蔬菜</th>
</tr>
<tr id="tb1" class="fileDiv">
<td><input type="checkbox" name="idarray" value="1"/></td>
<td>蘋果</td>
<td>菠菜</td>
</tr>
<tr id="tb2" class="fileDiv">
<td><input type="checkbox" name="idarray" value="2"/></td>
<td>梨</td>
<td>白菜</td>
</tr>
<tr id="tb3" class="fileDiv">
<td><input type="checkbox" name="idarray" value="3"/></td>
<td>葡萄</td>
<td>蘿卜</td>
</tr>
<tr id="tb4" class="fileDiv">
<td><input type="checkbox" name="idarray" value="4"/></td>
<td>桃子</td>
<td>土豆</td>
</tr>
<tr id="tb5" class="fileDiv">
<td><input type="checkbox" name="idarray" value="5"/></td>
<td>蘋果</td>
<td>菠菜</td>
</tr>
以下省略十行……
</table>
</div>
</body>
樣式
<style>
body { padding: 0; margin: 0; overflow-y: hidden; }
.table { width: 990px; height: 850px; }
.table tr { height: 50px; overflow: scroll }
.table-container {
position: absolute;
top: 0;
left: 200px;
height: 750px;
width: 990px;
overflow-y: scroll;
overflow-x: hidden;
}
#title { background-color: #BDE4F4; }
.fileDiv { background-color:#FEFF89; }
.seled { border: 1px solid red; background-color: #D6DFF7; }
</style>
效果
重頭戲js部分開始
<script type="text/javascript">
var allpro2 = [];
var index = 0;
window.onload = function () {
document.onmousedown = function () {
var evt = window.event || arguments[0];
var startX = (evt.x || evt.clientX);
var startY = (evt.y || evt.clientY);
if (startX > 220 && startX < 1210) {
var selList = [];
var fileNodes = document.getElementsByTagName("tr");
for (var i = 0; i < fileNodes.length; i++) {
if (fileNodes[i].className.indexOf("fileDiv") != -1) {
fileNodes[i].className = "fileDiv";
selList.push(fileNodes[i]);
}
}
allpro2 = []; //選錯(cuò)區(qū)域后重選則清空數(shù)組
var yheight = document.getElementById("tablewrapper");
if (yheight.scrollTop > 0) { //判斷初始滾動(dòng)條是否為0 為后面是否加滾動(dòng)條高度設(shè)置判斷值
var istrue = true;
}
paint(yheight, selList, startX, startY, istrue);
}
}
};
function paint(yheight, selList, startX, startY, istrue) {
var isSelect = true;
var selDiv = document.createElement("div");
selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
selDiv.id = "selectDiv";
document.body.appendChild(selDiv);
selDiv.style.left = startX + "px";
selDiv.style.top = startY + "px";
var _x = null;
var _y = null;
document.onmousemove = function () {
evt = window.event || arguments[0];
if (isSelect) {
if (selDiv.style.display == "none") {
selDiv.style.display = "";
}
_x = (evt.x || evt.clientX);
_y = (evt.y || evt.clientY);
selDiv.style.left = Math.min(_x, startX) + "px";
selDiv.style.top = Math.min(_y, startY) + "px";
selDiv.style.width = Math.abs(_x - startX) + "px";
selDiv.style.height = Math.abs(_y - startY) + "px";
}
};
document.onmouseup = function () {
if (selDiv) {
if (istrue) {
selDiv.style.height = Math.abs(_y - startY) + "px";
selDiv.style.top = Math.min(_y, startY) + yheight.scrollTop + "px";
} else {
selDiv.style.height = Math.abs(_y - startY) + yheight.scrollTop + "px";
}
var _l = selDiv.offsetLeft, _t = selDiv.offsetTop;
var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;
for (var i = 0; i < selList.length; i++) {
var sl = selList[i].offsetWidth + selList[i].offsetParent.offsetParent.offsetLeft; //獲取父元素table距離左邊距離
var st = selList[i].offsetHeight + selList[i].offsetTop;
if (sl > _l && st > _t && selList[i].offsetLeft < _l + _w && selList[i].offsetTop < _t + _h) {
if (selList[i].className.indexOf("seled") == -1) {
selList[i].className = selList[i].className + " seled";
}
} else {
if (selList[i].className.indexOf("seled") != -1) {
selList[i].className = "fileDiv";
}
}
}
}
isSelect = false;
if (selDiv) {
document.body.removeChild(selDiv);
showSelDiv(selList);
}
revs1();
selList = null, _x = null, _y = null, selDiv = null, startX = null, startY = null, evt = null;
}
}
function showSelDiv(arr) { //確定選中范圍更新數(shù)組allpro2
for (var i = 0; i < arr.length; i++) {
if (arr[i].className.indexOf("seled") != -1) {
var allpro1 = document.getElementById(arr[i].id).getElementsByTagName("td")[0].getElementsByTagName("input")[0];
allpro2.push(allpro1);
}
}
}
function revs1() { //反選函數(shù)
if (allpro2.length > 0) {
for (var i = 0; i < allpro2.length; i++) {
if (allpro2[i].type == "checkbox" && allpro2[i].checked == true) {
allpro2[i].checked = false;
} else if (allpro2[i].type == "checkbox" && allpro2[i].checked == false) {
allpro2[i].checked = true;
}
}
}
allpro2 = [];
}
</script>