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 ...
随机推荐
- grunt压缩js文件
grunt是node中很好的管理项目的工具,利用它可以实现对整个项目的管理,避免很多重复性的工作如合并.压缩,检查语法等. 使用grunt首先要安装node环境,nodejs官网http://node ...
- zabbix问题处理
工作的时候回遇到各种各样的问题. 今天遇到一个关于zabbix的问题. "Zabbix agent on host.name is unreachable for 5 minutes&quo ...
- Hibernate3 和Hibernate4 在配置文件上的区别
在使用hibernate之前要首先对hibernate进行一些基础的配置信息,像映射文件XXX.hbm.xml XXX代表当前的domain的模型类名 <?xml version=" ...
- polyfill之javascript函数的兼容写法——Array篇
1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...
- 读取其他软件listview控件的内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 别再为了this发愁了------JS中的this机制
别再为了this发愁了------JS中的this机制 题记:JavaScript中有很多令人困惑的地方,或者叫做机制.但是,就是这些东西让JavaScript显得那么美好而与众不同.比方说函数也是对 ...
- 【python】python定时器
#coding:utf-8 import os import time def print_ts(message): print "[%s] %s"%(time.strftime( ...
- ubuntu 常见问题
1.problem 1 用apt-get安装时,提示:E could not get lock /vaa/lib/dpkg/lock -open等 reasons :可能是由于上次使用apt-get ...
- 02shell编程环境的搭建
02shell编程环境的搭建 [02]Shell编程 02shell编程环境的搭建 在不同的操作系统上搭建shell编程环境 Linux Windows Mac 编辑器的选择 系统环境的搭建 注: 选 ...
- 结算凭证中委托付款部分sql
select a.makevdate,a.summary,a.totalcredit,a.cent_typeid,c.accidname from fts_voucher a,fts_voucher_ ...