一个挺漂亮的鼠标跟随效果

 
 
  1. <html> 
  2. <head> 
  3. <title>follow mouse</title> 
  4. <style type="text/css"> 
  5. html { 
  6. overflow: hidden; 
  7. body { 
  8. position: absolute; 
  9. height: 100%; 
  10. width: 100%; 
  11. margin:0; 
  12. padding:0; 
  13. #screen { 
  14. background:#000; 
  15. position: absolute; 
  16. width: 100%; 
  17. height: 100%; 
  18. #screen span { 
  19. background: #fff; 
  20. font-size: 0; 
  21. overflow: hidden; 
  22. width: 2px; 
  23. height: 2px; 
  24. </style> 
  25.   <script type="text/javascript">  
  26. var Follow = function () { 
  27. var $ = function (i) {return document.getElementById(i)}, 
  28. addEvent = function (o, e, f) {o.addEventListener ? o.addEventListener(e, f, false) : o.attachEvent('on'+e, function(){f.call(o)})}, 
  29. OBJ = [], sp, rs, N = 0, m; 
  30. var init = function (id, config) { 
  31. this.config = config || {}; 
  32. this.obj = $(id); 
  33. sp = this.config.speed || 4; 
  34. rs = this.config.animR || 1; 
  35. m = {x: $(id).offsetWidth * .5, y: $(id).offsetHeight * .5}; 
  36. this.setXY(); 
  37. this.start(); 
  38. init.prototype = { 
  39. setXY : function () { 
  40. var _this = this; 
  41. addEvent(this.obj, 'mousemove', function (e) { 
  42. ee = e || window.event; 
  43. m.x = e.clientX; 
  44. m.y = e.clientY; 
  45. }) 
  46. }, 
  47. start : function () { 
  48. var k = 180 / Math.PI, OO, o, _this = this, fn = this.config.fn; 
  49. OBJ[N++] = OO = new CObj(null, 0, 0); 
  50. for(var i=0;i<360;i+=20){ 
  51. var O = OO
  52. for(var j=10; j<35; j+=1){ 
  53. var x = fn(i, j).x, 
  54. y = fn(i, j).y; 
  55. OBJ[N++] = o = new CObj(O , x, y); 
  56. O = o
  57. setInterval(function() { 
  58. for (var i = 0; i < N; i++) OBJ[i].run(); 
  59. }, 16); 
  60. var CObj = function (p, cx, cy) { 
  61. var obj = document.createElement("span"); 
  62. this.css = obj.style; 
  63. this.css.position = "absolute"
  64. this.css.left = "-1000px"
  65. this.css.zIndex = 1000 - N; 
  66. document.getElementById("screen").appendChild(obj); 
  67. this.ddx = 0
  68. this.ddy = 0
  69. this.PX = 0
  70. this.PY = 0
  71. this.x = 0
  72. this.y = 0
  73. this.x0 = 0; 
  74. this.y0 = 0; 
  75. this.cx = cx; 
  76. this.cy = cy; 
  77. this.parent = p; 
  78. CObj.prototype.run = function () { 
  79. if (!this.parent) { 
  80. this.x0 = m.x; 
  81. this.y0 = m.y; 
  82. } else { 
  83. thisthis.x0 = this.parent.x; 
  84. thisthis.y0 = this.parent.y; 
  85. thisthis.x = this.PX += (this.ddx += ((this.x0 - this.PX - this.ddx) + this.cx) / rs) / sp; 
  86. thisthis.y = this.PY += (this.ddy += ((this.y0 - this.PY - this.ddy) + this.cy) / rs) / sp; 
  87. this.css.left = Math.round(this.x) + 'px'; 
  88. this.css.top = Math.round(this.y) + 'px'; 
  89. return init; 
  90. }(); 
  91. </script></head> 
  92. <body> 
  93. <div id="screen"></div> 
  94. <script type="text/javascript"> 
  95. new Follow('screen', {speed: 4, 
  96. animR : 2, 
  97. fn : function (i, j) { 
  98. return { 
  99. x : j/4*Math.cos(i), 
  100. y : j/4*Math.sin(i) 
  101. }}) 
  102. </script></body> 
  103. </html> 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值