js图片浮动碰浏览器边界反弹效果
js图片浮动,碰到浏览器编辑自动反弹向方向运动效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>图片浮动</title> <style> #img{ position:absolute; } </style> </head> <body> <img src="http://avatar.csdn.net/8/A/6/2_oyljerry.jpg" width=50 height=50 id="img"> <script> //获取图片的对象 var img=document.getElementById("img"); //设置图片的起始点坐标 var x=0,y=0; //设置图片的行进速度 var xSpeed=10; //横坐标的行进速度 var ySpeed=10; //纵坐标的行进速度 var imgHeight=50,imgWidth=50; //图片宽度和高度 //设置图片的最大移动范围 var w=Math.max(document.documentElement.clientWidth,document.body.clientWidth)-imgWidth; //横向移动的最大范围 var h=Math.max(document.documentElement.clientHeight,document.body.clientHeight)-imgHeight; //纵向移动的最大范围 window.onresize=function(){//窗体大小变化,更新可移动范围 w=Math.max(document.documentElement.clientWidth,document.body.clientWidth)-imgWidth; //横向移动的最大范围 h=Math.max(document.documentElement.clientHeight,document.body.clientHeight)-imgHeight; //纵向移动的最大范围 } function floatimg(){ //判断图片是否达到了边界 //1、如果达到了,那我们就改变图片的方向 //2、如果没有达到,设置坐标值为 起始坐标+速度 if(x>w||x<0){x=x<0?0:w;xSpeed=-xSpeed;}else x+=xSpeed; if(y>h||y<0){y=y<0?0:h;ySpeed=-ySpeed;}else y+=ySpeed; //使图片按规定坐标移动 img.style.left=x+"px"; img.style.top=y+"px"; //设置图片移动的时间间隔 setTimeout(floatimg,50); } floatimg(); </script> </body> </html>
加支付宝好友偷能量挖...
原创文章,转载请注明出处:js图片浮动碰浏览器边界反弹效果