分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

10慕课网《进击Node.js基础(一)》初识promise

发布时间:2023-09-06 02:00责任编辑:沈小雨关键词:jsNode

首先用嘴简单的方式实现一个动画效果

<!doctype><html><head><title>Promise animation</title><style type="text/css"> ???.ball { ???????width: 40px; ???????height: 40px; ???????border-radius: 20px; ???} ???.ball1 { ???????background: red; ???} ???.ball2 { ???????background: yellow; ???} ???.ball3 { ???????background: green; ???}</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 type="text/javascript"> ???????//定义三个球 ???????var ball1 = document.querySelector(‘.ball1‘) ???????var ball2 = document.querySelector(‘.ball2‘) ???????var ball3 = document.querySelector(‘.ball3‘) ???????//球,移动距离,回调函数 ???????function animate(ball, distance, cd){ ???????????//每13毫秒改变一次圆球的位置,直到到达指定位置 ???????????setTimeout(function(){ ???????????????var marginLeft = parseInt(ball.style.marginLeft,10) ???????????????//球达到预期位置 ???????????????if(marginLeft === distance){ ???????????????????cd && cd() ???????????????}else{ ???????????????????//球在左侧 ???????????????????if(marginLeft < distance){ ???????????????????????marginLeft++ ???????????????????}else{ ???????????????????????//球在右侧 ???????????????????????marginLeft-- ???????????????????} ???????????????????//调整球的位置 ???????????????????ball.style.marginLeft = marginLeft ???????????????????animate(ball, distance, cd) ???????????????} ???????????},13) ???????} ???????//控制动画 ???????animate(ball1, 100,function(){ ???????????animate(ball2, 200, function(){ ???????????????animate(ball3, 150, function(){ ???????????????????animate(ball2, 150, function(){ ???????????????????????animate(ball1, 150, function(){ ???????????????????????}) ???????????????????}) ???????????????}) ???????????}) ???????}) ???</script></body></html>

10慕课网《进击Node.js基础(一)》初识promise

原文地址:https://www.cnblogs.com/-beauTiFul/p/9195868.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved