首页 > web前端 > js教程 > javascript DIV跟随鼠标移动_javascript技巧

javascript DIV跟随鼠标移动_javascript技巧

WBOY
发布: 2016-05-16 15:16:26
原创
1181 人浏览过

首先介绍的是一个div跟随鼠标移动的效果,具体代码如下

javascript div跟随鼠标移动

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <script type="text/javascript">
    window.onload = function () {
      var div1 = document.getElementById('div1');
      document.onmousemove = function (ev) {
        //IE支持event,firefox不支持
        var oEvent = ev || event;
        //
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        div1.style.top = oEvent.clientY + scrollTop + 'px'; //Y
        div1.style.left = oEvent.clientX + scrollLeft + 'px';//X
      };
    };

  </script>
</head>
<body>
  <div id="div1" style="width: 10px; height: 10px; background-color: blue; position: absolute;"></div>
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
</body>
</html>

登录后复制

现在为大家分享的是javascript 一串DIV跟随鼠标移动

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title></title>
  <style type="text/css">
    div {
      width: 2px;
      height: 2px;
      background-color: red;
      position: absolute;
    }
  </style>
  <script type="text/javascript">
    window.onload = function () {
      var oDivs = document.getElementsByTagName('div');
      document.onmousemove = function (ev) {
        var oEvent = ev || event;
        document.getElementsByTagName('span')[0].innerHTML = oEvent.clientY + '|' + oEvent.clientX;
        for (var i = oDivs.length-1; i > 0; i--) {
            oDivs[i].style.top = oDivs[i - 1].style.top;
            oDivs[i].style.left = oDivs[i - 1].style.left;
        }
        oDivs[0].style.top = oEvent.clientY+'px';
        oDivs[0].style.left = oEvent.clientX + 'px';
      };

    };
  </script>
</head>
<body>
  <span></span>
  <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>
  <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>
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板