promise实例小球运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://apps.bdimg.com/libs/bluebird/1.2.2/bluebird.js"></script>
<style>
*{margin:0; padding:0;}
.div1{width:50px; height:50px; background:red; border-radius:50%; margin-top:10px; margin-left:0;}
.div2{width:50px; height:50px; background:yellow; border-radius:50%; margin-top:10px; margin-left:0;}
.div3{width:50px; height:50px; background:blue; border-radius:50%; margin-top:10px; margin-left:0;}
</style>
</head> <body>
<div style="margin:20px; position:relative">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</div>
<script>
window.onload = function(){
/*function move(obj, dis, cb){ setTimeout(function(){
var marginLeft = parseInt(obj.offsetLeft,10);
if(marginLeft == dis) {
cb && cb()
} else {
if(marginLeft < dis) {
marginLeft++
} else {
marginLeft--
}
obj.style.marginLeft = marginLeft + 'px'
move(obj, dis, cb)
}
},10) }
move(d1,150,function(){
move(d2,150,function(){
move(d3, 150, function(){
move(d3,0, function() {
move(d2,0,function(){
move(d1,0)
})
})
})
})
})*/
var d1 = document.querySelector('.div1')
var d2 = document.querySelector('.div2')
var d3 = document.querySelector('.div3')
var Promise = window.Promise
function promiseMove(obj,dis) {
return new Promise(function(resolve, reject) {
function move(){
setTimeout(function(){
var marginLeft = parseInt(obj.offsetLeft,10);
if(marginLeft == dis) {
resolve()
} else {
if(marginLeft < dis) {
marginLeft++
} else {
marginLeft--
}
obj.style.marginLeft = marginLeft + 'px'
move()
} },10)
}
move()
})
}
promiseMove(d1,100)
.then(function(){return promiseMove(d2,100) })
.then(function(){return promiseMove(d3,100) })
.then(function() {return promiseMove(d3,0)})
.then(function() {return promiseMove(d2,0)})
.then(function() {return promiseMove(d1,0)})
} </script>
</body>
</html>
promise实例小球运动的更多相关文章
- Cocos2d-x加速度计实例:运动的小球
下面我们通过一个实例介绍一下如果通过层加速度计事件实现访问加速度计.该实例场景如下图所示,场景中有一个小球,当我们把移动设备水平放置,屏幕向上,然后左右晃动移动设备来改变小球的位置. 下面我们再看看具 ...
- Promise实现小球的运动
Promise简要说明 Promise可以处理一些异步操作:如像setTimeout.ajax处理异步操作是一函数回调的方式;当然ajax在jQuery版本升级过程中,编写方式也有所变动. P ...
- JavaScript实例:运动的小球
本篇博文通过制作一个小球运动动画的实例,来学习在HTML5的画布上实现动画制作的方法,同时理解面向对象程序设计的基本思想. 1.绘制小球 先在HTML页面中设置一个画布. <canvas id= ...
- Windows Store App JavaScript 开发:小球运动示例
通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...
- 原生js小球运动
//html代码 <input type="button" value="小球运动" /> <div></div> //js ...
- Win10系列:JavaScript小球运动示例
通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...
- surface实例-小球弹起事例
ball.java package com.example.sufacedemo; import android.graphics.Bitmap; import android.graphics.Bi ...
- c语言编程实例——小球跳动
1.预备知识 1.1 相关头文件 "#include"是c语言中用以申明所需调用的库函数或自定义函数的头文件路径及文件名.#include ""和#includ ...
- 笔记:promise实例+注释
////////////////////////////////////////////// var data = [1,2,3,4]; var promise = new Promise((reso ...
随机推荐
- (转) WTF is computer vision?
WTF is computer vision? Posted Nov 13, 2016 by Devin Coldewey, Contributor Next Story Someon ...
- Perforce P4V,添加映射
- CenOS 7 安装wordpress
1:Centos安装 php和html yum install httpd -y yum install php -y 2:将 wordpress 拖到目录 /var/www/html 3:浏览器访 ...
- Angular.JS学习笔记——1
内容来自:http://www.runoob.com/angularjs/angularjs-intro.html AngularJS 是一个 JavaScript 框架.它是一个以 JavaScri ...
- 在.htaccess文件中写RewriteRule无效的问题的解决
近来在Apache Rewrite 拟静态配置时,遇到个问题.写的如下: RewriteEngine onRewriteRule ^/t_(.*)/$ /test.php?id=$1 保存在httpd ...
- PHP 返回13位时间戳
13位时间戳生成函数如下所示: private function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); retu ...
- [ActionScript 3.0] 图片左右循环移动
有时候多张图片展示需求中,需要左右循环移动展示,为了以后省时间思考,写个例子: import com.tweener.transitions.Tweener; import flash.display ...
- jquery分页插件
css代码: /*分页*/ .pageList { clear: both; overflow: hidden; } .pageList a, .pageList span { border: 1px ...
- U盘安装CentOS 7.0
U盘安装CentOS 7.0 由于学习需要centos环境,so上网下载centos安装镜像,发现版本已经到7.0了,(╮(╯▽╰)╭,上次折腾还是6.4的版本呢,)花了一点时间下载下来按照装6.4的 ...
- Tomcat7启动log打印到INFO: At least one JAR was scanned for TLDs yet contained no TLDs.就停止不动了
环境: RHEL7,tomcat7.0.70 问题: 启动tomcat时,catalina.out日志打印到如下内容就停止不动了,也不报错 SEVERE: FarmWarDeployer can on ...