js漂浮图片

非原创  来源于其它作者

第一种

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <style type="text/css">
 9         #demo {
10             width: 100px;
11             height: 100px;
12             position:absolute;
13             border-radius:50px;
14         }
15     </style>
16  
17     <script type="text/javascript">
18         window.onload = function(){
19             var demo = document.getElementById('demo');
20             var sx = sy = 10;
21             var x = y = 0;
22  
23             function move(){
24                 if(document.documentElement.clientWidth - demo.offsetWidth-10 < x || x < 0){
25                     sx = -sx;
26                 }
27  
28                 if(document.documentElement.clientHeight - demo.offsetHeight-10 < y || y < 0){
29                     sy = -sy;
30                 }
31  
32                 x = demo.offsetLeft + sx;
33                 y = demo.offsetTop + sy;
34  
35                 demo.style.left = x + 'px';
36                 demo.style.top = y + 'px';
37             }
38  
39             var timer = setInterval(move, 100);
40  
41             demo.onmouseover = function(){
42                 clearInterval(timer);
43             }
44  
45             demo.onmouseout = function(){
46                 timer = setInterval(move, 100);
47             }
48         }
49     </script>
50  
51     <img src="http://d.hiphotos.baidu.com/image/w%3D2048/sign=48fd3c26f01fbe091c5ec4145f580d33/64380cd7912397dd92729b545b82b2b7d0a28752.jpg" id="demo" />
52     
53 </body>
54 </html>

第二种

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <script>
 7         var directX=1;//x轴的方向
 8         var directY=1;//y轴的方向
 9         var moveX=0;//图片的坐标
10         var moveY=0;
11         var speed=2;//移动的速度
12         function move(){
13             moveX+=directX*speed;
14             moveY+=directY*speed;
15             var nr=document.getElementById("moveDiv");
16             nr.style.top=moveY+"px";//改变div 的top
17             nr.style.left=moveX+"px";//改变div的left
18             if (moveX+nr.offsetWidth>=document.body.clientWidth||moveX<=0){
19                 directX=-directX;//当碰触到边界的时候 改变方向
20             }
21             if (moveY+nr.offsetHeight>=document.documentElement.clientHeight||moveY<=0){
22                 directY=-directY;
23             }
24         }
25         setInterval(move,20);
26 
27     </script>
28 </head>
29 <body>
30 <div id="moveDiv" style=" position: absolute;">
31     <img src="http://img.mp.sohu.com/upload/20170813/60c7210c02ed4404ba29bfc149dffec3.png">
32 </div>
33 </body>
34 </html>

 

转载于:https://www.cnblogs.com/zzq-229/p/9577337.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值