setInterval和settimeout简单动画区别
2016-07-14 00:00
513 查看
<style>
#box1{
position:absolute;
width:50px;
height:50px;
left:100px;
top:150px;
background:#666666
}
#box2{
position:absolute;
width:50px;
height:50px;
left:100px;
top:250px;
background:#666666
}
</style>
<script type="text/javascript">
window.onload=function(){
setTimeout(function(){ //只执行一次
var box1=document.getElementById('box1');
var left1=parseInt(GetStyle(box1,'left'));
box1.style.left=left1+1+'px';
},2000);
var time=setInterval(function(){ //会一直执行 到500停止
var box2=document.getElementById('box2');
var left2=parseInt(GetStyle(box2,'left'));
box2.style.left=left2+1+'px';
if(left2==500){
clearInterval(time);
}
},20);
//获取css的type属性
function GetStyle(element,type){
if(element.currentStyle) {
return element.currentStyle[type]
}else if(window.getComputedStyle) {
return window.getComputedStyle(element , null)[type];
}
}
}
</script>
</head>
<body>
<div id="box1"> </div>
<div id="box2"> </div>
</body>
#box1{
position:absolute;
width:50px;
height:50px;
left:100px;
top:150px;
background:#666666
}
#box2{
position:absolute;
width:50px;
height:50px;
left:100px;
top:250px;
background:#666666
}
</style>
<script type="text/javascript">
window.onload=function(){
setTimeout(function(){ //只执行一次
var box1=document.getElementById('box1');
var left1=parseInt(GetStyle(box1,'left'));
box1.style.left=left1+1+'px';
},2000);
var time=setInterval(function(){ //会一直执行 到500停止
var box2=document.getElementById('box2');
var left2=parseInt(GetStyle(box2,'left'));
box2.style.left=left2+1+'px';
if(left2==500){
clearInterval(time);
}
},20);
//获取css的type属性
function GetStyle(element,type){
if(element.currentStyle) {
return element.currentStyle[type]
}else if(window.getComputedStyle) {
return window.getComputedStyle(element , null)[type];
}
}
}
</script>
</head>
<body>
<div id="box1"> </div>
<div id="box2"> </div>
</body>
相关文章推荐
- PHP爬虫:百万级别知乎用户数据爬取与分析
- 才一年,H5的发展就成这样了
- 甲骨文要放弃不赚钱的Java?开发者心痛
- Hadoop调度器原理解析
- Hadoop中mapred包和mapreduce包
- swiper教程--swiper的基础使用(二十)
- UITableView+UITableViewStyleGrouped 处理section之间间隙
- 灵光一闪-(面对对象实践)
- JAVA学习日志(6-2-static应用)
- oracle表和视图
- Nodejs npm的常用指令
- JodConverter doc转pdf (Debian 8)
- 今日头条刷阅读量工具,今日头条刷阅读量软件
- 基于dubbo的demo
- dubbo的生产者配置
- 搜索引擎使用中的奇技淫巧
- dubbo的消费者
- 微信公众号发起H5支付
- 母乳喂养的方法、知识与注意事项
- 胡萝卜含有丰富的果胶物质