1. js实现动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>animate</title>
<style>
.ball {
width: 40px;
height: 40px;
margin-bottom: 5px;
border-radius: 20px;
}
.ball1 {
background: red;
}
.ball2 {
background: blue;
}
.ball3 {
background: yellow;
}
</style>
</head>
<body>
<div class="ball ball1" style="margin-left: 0"></div>
<div class="ball ball2" style="margin-left: 0"></div>
<div class="ball ball3" style="margin-left: 0"></div>
<script>
var ball1 = document.querySelector(".ball1");
var ball2 = document.querySelector(".ball2");
var ball3 = document.querySelector(".ball3");
function animate(ball, left, callback) {
setTimeout(function () {
var marginLeft = parseInt(ball.style.marginLeft, 10);
if (marginLeft === left) {
callback && callback();
} else {
if (marginLeft < left) {
marginLeft += 2;
} else {
marginLeft -= 2;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left, callback);
}
}, 13);
}
animate(ball1, 100, function () {
animate(ball2, 200, function () {
animate(ball3, 300, function () {
animate(ball1, 200, function () {
animate(ball3, 200, function () {
animate(ball2, 180, function () {
animate(ball2, 220, function () {
animate(ball2, 200, function () {
console.log("over");
})
})
})
})
})
})
})
});
</script>
</body>
</html>
上述代码就可以实现一个动画。注意下面几点:
"htmlcode">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>animate</title>
<style>
.ball {
width: 40px;
height: 40px;
margin-bottom: 5px;
border-radius: 20px;
}
.ball1 {
background: red;
}
.ball2 {
background: blue;
}
.ball3 {
background: yellow;
}
</style>
</head>
<body>
<div class="ball ball1" style="margin-left: 0"></div>
<div class="ball ball2" style="margin-left: 0"></div>
<div class="ball ball3" style="margin-left: 0"></div>
<script>
var ball1 = document.querySelector(".ball1");
var ball2 = document.querySelector(".ball2");
var ball3 = document.querySelector(".ball3");
function promiseAnimate(ball, left) {
return new Promise(function (resolve, reject) {
function animate(ball, left) {
setTimeout(function () {
var marginLeft = parseInt(ball.style.marginLeft, 10);
if (marginLeft === left) {
resolve();
} else {
if (marginLeft < left) {
marginLeft += 2;
} else {
marginLeft -= 2;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left);
}
}, 13);
}
animate(ball,left);
});
}
promiseAnimate(ball1, 500)
.then(function () {
return promiseAnimate(ball2, 200);
})
.then(function () {
return promiseAnimate(ball3, 300);
})
.then(function () {
return promiseAnimate(ball1, 200);
})
.then(function () {
return promiseAnimate(ball3, 200);
})
.then(function () {
return promiseAnimate(ball2, 180);
})
.then(function () {
return promiseAnimate(ball2, 220);
})
.then(function () {
return promiseAnimate(ball2, 200);
})
</script>
</body>
</html>
这同样可以达到效果,并且这样做的好处是,修改更加容易一些。对于promise,有几点需要注意:
1.在执行promise相关函数的时候,要返回一个promise对象,这是常用的做法。
2.只有返回了promise对象,我们才能实用then。
3.并且在then中还要返回promise对象,这样我们就可以不断的使用then()来管理异步。
4.在promise执行之后,要使用resolve()来表明这个promise执行的结束。 这样,才能执行then方法。
问题: 在then中如果直接执行promiseAnimate(ball2, 200);不可以吗? 为什么一定要return呢?
答: 当然不可以,因为如果直接执行,确实返回了一个promise对象,但是这个promise对象只是在then下面的函数中啊, 我们必须在这个函数继续返回这个promise对象才能达到继续使用then的目的。
其中resolve()代表着这个异步过程的结束。
综上所述: 动画多用setTimeout和调用自己的方式执行,当然,使用setInterval也是一样的,只是前者我们更为推荐。 无论是使用setTimeout还是setInterval,都不可避免的会产生如果解决异步的问题。 之前我们解决异步的方式是使用回调函数,但是回调函数非常容易就会产生回调地狱,所以用promise会更好一些。
总结
以上所述是小编给大家介绍的JS动画实现回调地狱promise的实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。