多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 鼠标事件 - 鼠标位置 - 可视区位置:`clientX`、`clientY` - 例子:跟随鼠标的 `Div` - 消除滚动条的影响:**可视区**与**页面顶部**的距离 - 代码: ```js // 封装鼠标当前坐标函数 function getPos(ev) { var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; var scrollLeft = document.documentElement.scollLeft||document.body.scrollLeft; return {x: ev.clientX+scrollLeft, y: ev.clientY+scrollTop}; } ``` - 滚轮事件:`onmousewheel` 滚轮数据:`event.wheelDelta` - 代码:鼠标滚轮控制 `div` 移动 ```HTML <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>事件绑定与鼠标滚轮</title> <link rel="stylesheet" href="../reset.css"> <style> #div1 { width: 200px; height: 200px; position: absolute; background-color: rgb(255, 0, 0); } </style> <script type="text/javascript"> var iDis = 0; var scrollFunc = function (e) { var oDiv1 = document.getElementById('div1'); e = e || window.event; if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件 if (e.wheelDelta > 0) { //当滑轮向上滚动时 iDis += -e.wheelDelta; oDiv1.style.top = iDis/12 + 'px'; console.log("滑轮向上滚动",e.wheelDelta, iDis, oDiv1.offsetTop); } if (e.wheelDelta < 0) { //当滑轮向下滚动时 iDis += -e.wheelDelta; oDiv1.style.top = iDis/12 + 'px'; console.log("滑轮向上滚动",e.wheelDelta, iDis, oDiv1.offsetTop); } } else if (e.detail) { //Firefox滑轮事件 if (e.detail> 0) { //当滑轮向上滚动时 console.log("滑轮向上滚动",e.wheelDelta); } if (e.detail< 0) { //当滑轮向下滚动时 console.log("滑轮向下滚动",e.wheelDelta); } } } //给页面绑定滑轮滚动事件 if (document.addEventListener) {//firefox document.addEventListener('DOMMouseScroll', scrollFunc, false); } //滚动滑轮触发scrollFunc方法 //ie 谷歌 window.onmousewheel = document.onmousewheel = scrollFunc; </script> </head> <body> <div id="div1"></div> </body> </html> ``` - 获取鼠标在页面的绝对位置 - 封装函数 - 例子:一串跟随鼠标的 `Div` - 代码: ```HTML <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>跟随鼠标的一串div</title> <link rel="stylesheet" href="../reset.css"> <style> div { width: 10px; height: 10px; position: absolute; background-color: rgb(255, 0, 0); } </style> <script> window.onload = function () { // 封装鼠标当前坐标函数 function getPos(ev) { var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; var scrollLeft = document.documentElement.scollLeft||document.body.scrollLeft; return {x: ev.clientX+scrollLeft, y: ev.clientY+scrollTop}; } var oDiv = document.getElementsByTagName('div'); document.onmousemove = function () { var ev = ev || event; // 后一个元素跟随前一个元素 for ( var i = oDiv.length-1; i > 0; i--) { console.log(oDiv[i].style.left,oDiv[i-1].offsetLeft) oDiv[i].style.left = oDiv[i-1].offsetLeft + 'px'; oDiv[i].style.top = oDiv[i-1].offsetTop + 'px'; } // 当前多变赋值给第一个元素, var pos = getPos(ev); oDiv[0].style.left = pos.x + 'px'; oDiv[0].style.top = pos.y + 'px'; } } </script> </head> <body> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div> </body> </html> ```