当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery 实现鼠标画框并对框内数据选中的实例代码

jQuery 实现鼠标画框并对框内数据选中的实例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

jquery库:

jquery -1.10.2.min.js,jquery ui - v1.12.1。

jquery 代码

不多说了,之间上代码。不懂的地方看注释。

<script type="text/javascript">
 //鼠标按下时的x y坐标
 var mousedownx;
 var mousedowny;
 //鼠标按下时移动的x y 坐标
 var mousemovex;
 var mousemovey;
 //移动的状态
 var ismove = false;
 /*初始化 选择框 */
 function init() {
  $("#selected").css("display", "none");
  $("#selected").css("top", "0");
  $("#selected").css("left", "0");
  $("#selected").css("width", "0");
  $("#selected").css("height", "0");
 }
 $(document).ready(function() {
  init();
  var selectedtd = new array();//创建被选中表格数组
  var td = $("td");//获取所有表格信息
  for ( var i = 0; i < td.length; i++) {
   selectedtd.push(td[i]);
  }
  $("#tablediv").mousedown(function(event) {
   mousedownx = event.clientx - $(this).offset().left;;
   mousedowny = event.clienty - $(this).offset().top;
   console.log("mousedownx=" + mousedownx +" mousedowny=" + mousedowny );
   if(event.target.id.match(/selected/)) {
    ismove = true;
   }
   //鼠标按下并移动时进行判断(拖拽 or 画框)
   $("#tablediv").mousemove(function(event) {
    mousemovex = event.clientx - $(this).offset().left;
    mousemovey = event.clienty - $(this).offset().top;
    var selectdiv = document.getelementbyid("selected");
    if (ismove) {
     //拖拽的代码,因为实在不想算 xy 了,所以使用了jquery ui
     $("#selected").draggable();
     //这部分是负责画框的时候,实时把框住的表格变色的,(代码和下面的代码重复了)
     var left = selectdiv.offsetleft, top = selectdiv.offsettop; width = selectdiv.offsetwidth, height = selectdiv.offsetheight;
     for ( var i = 0; i < selectedtd.length; i++) {
      var sl = selectedtd[i].offsetwidth + selectedtd[i].offsetleft;
      var st = selectedtd[i].offsetheight + selectedtd[i].offsettop;
      if (sl > left && st > top && selectedtd[i].offsetleft < left + width && selectedtd[i].offsettop < top + height) {
       if (selectedtd[i].classname.indexof("selected") == -1) {
        selectedtd[i].classname = "selected";
       }
      } else { 
       if (selectedtd[i].classname.indexof("selected") != -1) { 
        selectedtd[i].classname = "td"; 
       }
      }
     }
    } else {
     //重复的代码,完了再把它抽取出来
     var left = selectdiv.offsetleft, top = selectdiv.offsettop; width = selectdiv.offsetwidth, height = selectdiv.offsetheight;
     for ( var i = 0; i < selectedtd.length; i++) {
      var sl = selectedtd[i].offsetwidth + selectedtd[i].offsetleft;
      var st = selectedtd[i].offsetheight + selectedtd[i].offsettop;
      if (sl > left && st > top && selectedtd[i].offsetleft < left + width && selectedtd[i].offsettop < top + height) {
       if (selectedtd[i].classname.indexof("selected") == -1) {
        selectedtd[i].classname = "selected";
       }
      } else {
       if (selectedtd[i].classname.indexof("selected") != -1) { 
        selectedtd[i].classname = "td"; 
       }
      }
     }
     //鼠标抬起结束画框,和拖动
     $("#tablediv").mouseup(function() {
      console.log("mouseupx=" + mousemovex + " mouseupy=" + mousemovex);
      ismove = false;
      $(this).unbind('mousemove');
     })
     //画框
     displayselected(mousedowny, mousedownx, mousemovex, mousemovey);
    }
   })
  })
  //当鼠标在已经画好的框上时,改变鼠标指针样式,就是十字形了
  $("#selected").mouseenter(function() {
   $("#selected").css("cursor", "move");
  });
 });
 function displayselected(mousedowny, mousedownx, mouseupx, mouseupy) {
  $("#selected").css("display", "block");
  $("#selected").css("top", mousedowny);
  $("#selected").css("left", mousedownx);
  var movex = mousemovex - mousedownx;
  var movey = mousemovey - mousedowny;
  if (movey < 0) {
    $("#selected").css("top", event.clienty - $("#table").offset().top);
  }
  if (movex < 0) {
   $("#selected").css("left", event.clientx - $("#table").offset().left);
  }
  $("#selected").css("width", math.abs(movex));
  $("#selected").css("height", math.abs(movey));
 }
 </script>

测试用的html

使用table进行的测试:

<div id="tablediv" style="width: 1500px; height: 1500px;top: 100px; left:100px; position: absolute;">
  <div id="selected" style="border:5px dotted rgb(239, 37, 17);position: absolute;display: none;"></div>
  <table border="1" style=" width: 1500px; height: 1500px;" id="table">
  <tr>
   <td id="1" class="td"></td>
   <td id="2" class="td"></td>
   <td id="3" class="td"></td>
   <td id="4" class="td"></td>
   <td id="5" class="td"></td>
   <td id="6" class="td"></td>
  </tr>
  <tr>
   <td id="7" class="td"></td>
   <td id="8" class="td"></td>
   <td id="9" class="td"></td>
   <td id="10" class="td"></td>
   <td id="11" class="td"></td>
   <td id="12" class="td"></td>
  </tr>
  <tr>
   <td id="1" class="td"></td>
   <td id="2" class="td"></td>
   <td id="3" class="td"></td>
   <td id="4" class="td"></td>
   <td id="5" class="td"></td>
   <td id="6" class="td"></td>
  </tr>
  </table>
 <!--表格代码太多所以...-->
 </div>

效果图

这里写图片描述

以上所述是小编给大家介绍的jquery 实现鼠标画框并对框内数据选中的实例代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网