<!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实例小球运动的更多相关文章

  1. Cocos2d-x加速度计实例:运动的小球

    下面我们通过一个实例介绍一下如果通过层加速度计事件实现访问加速度计.该实例场景如下图所示,场景中有一个小球,当我们把移动设备水平放置,屏幕向上,然后左右晃动移动设备来改变小球的位置. 下面我们再看看具 ...

  2. Promise实现小球的运动

        Promise简要说明 Promise可以处理一些异步操作:如像setTimeout.ajax处理异步操作是一函数回调的方式;当然ajax在jQuery版本升级过程中,编写方式也有所变动. P ...

  3. JavaScript实例:运动的小球

    本篇博文通过制作一个小球运动动画的实例,来学习在HTML5的画布上实现动画制作的方法,同时理解面向对象程序设计的基本思想. 1.绘制小球 先在HTML页面中设置一个画布. <canvas id= ...

  4. Windows Store App JavaScript 开发:小球运动示例

    通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...

  5. 原生js小球运动

    //html代码 <input type="button" value="小球运动" /> <div></div> //js ...

  6. Win10系列:JavaScript小球运动示例

    通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...

  7. surface实例-小球弹起事例

    ball.java package com.example.sufacedemo; import android.graphics.Bitmap; import android.graphics.Bi ...

  8. c语言编程实例——小球跳动

    1.预备知识 1.1 相关头文件 "#include"是c语言中用以申明所需调用的库函数或自定义函数的头文件路径及文件名.#include ""和#includ ...

  9. 笔记:promise实例+注释

    ////////////////////////////////////////////// var data = [1,2,3,4]; var promise = new Promise((reso ...

随机推荐

  1. 初遇 dotcloud

    逛园子的时候看到新浪SAE,正学习建站呢,好东西.(论环境的影响...) 不过发现新浪SAE只支持 Python2,我更喜欢 Python3 e...找找其他的,发现了 dotcloud,遂试试,下面 ...

  2. fedora wine qq

    http://blog.lilydjwg.me/2015/10/26/run-tencent-qq-lite-with-wine.186640.html

  3. 【转】error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

    错误信息: /usr/local/memcacheq/bin/memcacheq: error while loading shared libraries: libevent-2.0.so.5: c ...

  4. linux 安装 python2.7

    若新安装虚拟机,或者新装linux系统.需安装gcc等yum -y install gcc gcc-c++ autoconf automake cmake ntp rsync ssh vim  yum ...

  5. 前端 动态表单提交(post、put)

    第一步:form表单定义统一属性 <input type="text" class="form-value" /> 第二步:获取所有值 var fo ...

  6. [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only

    Okay for centos 6.4 also On apu.0xdata.loc, after this install was done $ which python /usr/local/bi ...

  7. OAF_开发系列11_实现OAF通过DataBoundValues动态显示表列的左右对齐

    20150712 Created By BaoXinjian

  8. mongodb高级查询

    前几篇,老玩家绕道即可,新手晚上闲着也是蛋疼,不如把命令敲一边,这样你就会对MongoDB有一定的掌握啦.如果没有安装MongoDB去看我的上一篇博客  MongoDB下载安装与简单增删改查 前奏:启 ...

  9. 前端网站helper

    聚合api 一.颜色类网站http://colorhunt.co 这个网站给我们提供了很多的配色方案,我们直接使用就OK了.使用方法也很简单,鼠标移动到对应的颜色上,我们就可以看到颜色的十六进制码,复 ...

  10. mint安装相关数据库lib

    sudo apt-get install libmysqlclient-dev sudo apt-get install sqlite3 libsqlite3-dev