js实现4个方向滚动的球
程序员文章站
2022-06-21 08:02:57
效果图:
代码如下:
效果图:
代码如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ margin: 0; padding: 0; } #wrap{ width: 800px; height: 500px; border: 1px solid deeppink; margin-left: 10px; margin-top: 5px; float: left; } #input1{ width: 80px; margin: 5px auto 5px 10px; font-size: 0; float: left; } #div1{ width: 100px; height: 100px; background: hotpink; position: absolute; top: 20px; left: 30px; border-radius: 100px; box-shadow: 0px 5px 5px rgba(0,0,0,.5); } input{ width: 100px; height: 40px; line-height: 40px; text-align: center; font-size: 18px; display: block; background: palegreen; margin-bottom: 5px; } </style> </head> <body> <div id="wrap"> <div id="div1"></div> </div> <div id="input1"> <input type="button" value="向左" id="btn2" /> <input type="button" value="向右" id="btn1"/> <input type="button" value="向上" id="btn3" /> <input type="button" value="向下" id="btn4"/> </div> <script> var obtn=document.getelementbyid('btn1'); var odiv=document.getelementbyid('div1'); var obtn2=document.getelementbyid('btn2'); var obtn3=document.getelementbyid('btn3'); var obtn4=document.getelementbyid('btn4'); obtn4.onclick=function(){ move(odiv,10,380,'0px 5px 5px rgba(0,0,0,.5)','top'); } obtn3.onclick=function(){ move(odiv,-10,30,'0px -5px 5px rgba(0,0,0,.5)','top'); } obtn2.onclick=function(){ move(odiv,-10,40,'-5px 5px 5px rgba(0,0,0,.5)','left'); } obtn.onclick=function(){ move(odiv,10,680,'5px 5px 5px rgba(0,0,0,.5)','left'); } function move(obj,val,target,bs,dir){ obj.style.boxshadow=bs; clearinterval(obj.timer); obj.timer=setinterval(function(){ var speed=parseint(getstyle(obj,dir))+val; if(speed>=target&&val>0){ speed=target; } if(speed<=target&&val<0){ speed=target } obj.style[dir]=speed+'px'; if(speed==target){ clearinterval(obj.timer); } },30); } function getstyle(obj,sty){ return obj.currentstyle?obj.currentstyle[sty]:getcomputedstyle(obj)[sty]; } </script> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: python远程连接MySQL数据库
下一篇: python爬取淘宝商品销量信息