跟随鼠标的DIV和一连串跟随鼠标的DIV
1. 跟随鼠标的DIV
1 window.onmousemove = function(ev) {
2 //浏览器兼容
3 var oEvent = ev || event;
4 var oDiv = document.getElementById("div1");
5
6 //页面滚动的距离
7 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
8 var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
9
10 oDiv.style.top = scrollTop + oEvent.clientY + "px";
11 oDiv.style.left = scrollLeft + oEvent.clientX + "px";
12 };
2. 一连串跟随鼠标的DIV
1 window.onload = function() {
2 var divs = document.getElementsByTagName("div");
3
4 window.onmousemove = function(ev) {
5 var oEvent = ev || event;
6
7 for (var i = divs.length - 1; i > 0; i--) {
8 divs[i].style.left = divs[i - 1].style.left;
9 divs[i].style.top = divs[i - 1].style.top;
10 }
11
12 divs[0].style.left = oEvent.clientX + "px";
13 divs[0].style.top = oEvent.clientY + "px";
14 }
15 };