鼠标跟随测试.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>div移动</title>
  6. <style>
  7. div{
  8. background-color: #00b894;
  9. position: absolute;
  10. }
  11. </style>
  12. <script src="https://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
  13. <script>
  14. $(function(){
  15. var targetX;
  16. var targetY;
  17. $("#boxout").mouseenter(function(){
  18. $(this).mousemove(function(event){
  19. //$(this)就代表$(document),处理谷歌和火狐对滚动的兼容
  20. var event = event || window.event;
  21. var pageX = event.pageX || event.clientX + $(this).scrollLeft();
  22. var pageY = event.pageY || event.clientY + $(this).scrollTop();
  23. targetX = pageX - $(".b").width()/2;
  24. targetY = pageY - $(".b").height()/2;
  25. //注意:这里不能通过$("#box").offset().left来设置,因为它只能获取,设置使用如下方法
  26. if(targetX<945&&targetY<946){
  27. $(".b").show()
  28. $(".b").css({
  29. left:targetX+55,
  30. top:targetY+55
  31. });
  32. }else{
  33. $(".b").hide()
  34. }
  35. });
  36. })
  37. $("#boxout").mouseleave(function(){
  38. console.log("leave")
  39. })
  40. });
  41. </script>
  42. </head>
  43. <body style="width: 2000px;height: 2000px">
  44. <div id="boxout" style="width: 1000px; height: 1000px;">
  45. <div id="box" class="b" style="width: 100px; height: 100px;background-color: #ff0707;"></div>
  46. </div>
  47. <div id="continer" style="left: 1080px; width: 500px; height: 1000px;background-color: #07daff;">
  48. <table border="1px solid">
  49. <thead>
  50. <tr>
  51. <th>
  52. <input type="checkbox" id="allBtn"/>
  53. </th>
  54. <th>菜名</th>
  55. <th>饭店</th>
  56. </tr>
  57. </thead>
  58. <tbody id="j_tb">
  59. <tr>
  60. <td>
  61. <input type="checkbox" id="itemBox"/>
  62. </td>
  63. <td>红烧肉</td>
  64. <td>田老师</td>
  65. </tr>
  66. <tr>
  67. <td>
  68. <input type="checkbox" id="itemBox"/>
  69. </td>
  70. <td>西红柿鸡蛋</td>
  71. <td>田老师</td>
  72. </tr>
  73. <tr>
  74. <td>
  75. <input type="checkbox" id="itemBox"/>
  76. </td>
  77. <td>红烧狮子头</td>
  78. <td>田老师</td>
  79. </tr>
  80. <tr>
  81. <td>
  82. <input type="checkbox" id="itemBox"/>
  83. </td>
  84. <td>日式肥牛</td>
  85. <td>田老师</td>
  86. </tr>
  87. </tbody>
  88. </table>
  89. </div>
  90. </body>
  91. <script>
  92. $(function () {
  93. $("img").attr("src", "11111");
  94. //思路是,点击全选按钮后全选按钮状态是已选中,
  95. //那么代表之前全选按钮时未选中状态,所以其它
  96. //所有按钮设为选中状态,反之,其它所有按钮取消全选状态
  97. $("#allBtn").click(function(){
  98. var ischecked = $(this).prop('checked');
  99. if(ischecked == true){
  100. $('input').prop('checked',true);
  101. }else{
  102. $('input').prop('checked',false);
  103. }
  104. });
  105. $('input#itemBox').click(function(){
  106. //补充:如果我们手动选中了除了全选按钮外的全部按钮,那么全选按钮自动被选定
  107. //思路是,根据全部多选框的个数(排除一个总选按钮)和已选中多选框的个数对比,一致则总选按钮被自动选定
  108. var checkedLen = $('input#itemBox:checked').length;
  109. var allInput = $('input#itemBox').length;
  110. if(allInput == checkedLen){
  111. $("#allBtn").prop('checked',true);
  112. }else{
  113. $("#allBtn").prop('checked',false );
  114. }
  115. console.log($(this).parents().siblings("td").text())
  116. var txt = $(this).parents().siblings("td").text()
  117. var stat = $(this).is(":checked")
  118. if(stat==true){
  119. var ht = '<div id="box' + txt + '" class="b" style="width: 100px; height: 100px;background-color: #ff0707;">'+ txt +'</div>'
  120. $("body").append(ht)
  121. }
  122. else{
  123. $("#box"+txt).remove()
  124. }
  125. console.log($(this).is(":checked"))
  126. })
  127. });
  128. </script>
  129. </html>