js实现4个方向滚动的球
更新时间:2017年03月06日 10:32:35 作者:marie0119
本文主要介绍了js实现4个方向滚动球的实例,具有很好的参考价值。下面跟着小编一起来看下吧
效果图:

代码如下:
<!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>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
相关文章
使用dynatrace-ajax跟踪JavaScript的性能
DynaTrace 致力于分析后台应用性能的表现已经好几年了,最近,他们通过发布dynaTrace Ajax Edition进入了前端性能分析领域. 它是一个运行在IE下的BHO免费工具. 虽然我喜欢Firefox和它下面的所有插件,但我知道基于IE的测试和调试也是很重要的。2010-04-04
JavaScript 获取 URL 中参数值的方法详解(最新整理)
本文将详细介绍几种在 JavaScript 中获取 URL 参数值的方法,包括现代浏览器支持的 URLSearchParams、正则表达式解析以及自定义函数解析方案,并讨论各自的优缺点及适用场景,感兴趣的朋友一起看看吧2025-04-04
jscript之List Excel Color Values
jscript之List Excel Color Values...2007-06-06


最新评论