码迷,mamicode.com
首页 > 其他好文 > 详细

跟随鼠标的DIV和一连串跟随鼠标的DIV

时间:2015-12-14 01:20:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

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     };

 

跟随鼠标的DIV和一连串跟随鼠标的DIV

标签:

原文地址:http://www.cnblogs.com/huoteng/p/5043958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!