移动页面元素,先从原点出发,向左移动20px,再向上移动50,最后再向左移30px,请把运动路径画下来

一.用callback形式回调

const walk=(target,direction,distance,callback)=>{
	setTimeout(()=>{
		//parseInt()   10代表只能是10的倍数
		let currentLeft=parseInt(target.style.left,10)
		let currentTop=parseInt(target.style.top,10)
		console.info('currentLeft',currentLeft,'distance',distance,'currentTop',currentTop)
		const shouldFinish=(direction==='left'&&currentLeft===-distance)||(direction==='top'&&currentTop===-distance)
		//本次移动结束,继续下一次移动
		if(shouldFinish){
			callback&&callback()
		}else{
			if(direction==='left'){
				currentLeft--
				target.style.left=`${currentLeft}px`
			}else if(direction==='top'){
				currentTop--
				target.style.top=`${currentTop}px`
			}
			walk(target,direction,distance,callback)
		}
		
	},1000)
}

//二 用Promise方案解决

const walkExtend=(target,direction,distance)=>
new Promise((resolve,reject)=>{
	const innerWalk=()=>{
		setTimeout(()=>{
			let currentLeft=parseInt(target.style.left,10)
			let currentTop=parseInt(target.style.top,10)
			console.info('currentLeft',currentLeft,'distance',distance,'currentTop',currentTop)
			const shouldFinish=(direction==='left'&&currentLeft===-distance)||(direction==='top'&&currentTop===-distance)
			//本次移动结束,继续下一次移动
			if(shouldFinish){
				//任务结束
				resolve()
			}else{
				if(direction==='left'){
					currentLeft--
					target.style.left=`${currentLeft}px`
				}else if(direction==='top'){
					currentTop--
					target.style.top=`${currentTop}px`
				}
				innerWalk()
			}
		},20)
	}
	innerWalk()
})

三.html调用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="./yibu.js"></script>
	</head>
	<style>
		#man{
			width:10px;
			height:10px;
			background-color: skyblue;
		}
	</style>
	<body>
		<div id="man"></div>
		<script>
		    window.addEventListener("load",function(){
				const target=document.querySelectorAll('#man')[0]
				target.style.cssText=`position:absolute;left:0px;top:0px;`
				//用callback解决
				walk(target,'left',10,()=>{
					walk(target,'top',50,()=>{
						walk(target,'left',30,Function.prototype)
					})
				})
				//用promise解决
				walkExtend(target,'left',20).then(()=>
					walkExtend(target,'top',50)).then(()=>
						walkExtend(target,'left',30))
				//用Generator解决
				function *taskGenerator(){
					yield walkExtend(target,'left',20)
					yield walkExtend(target,'top',50)
					yield walkExtend(target,'left',30)
				}
				const gen=taskGenerator()
				gen.next()  //要手动执行next函数
				//用async await解决
				const task=async function(){
					await walkExtend(target,'left',20)
					await walkExtend(target,'top',50)
					await walkExtend(target,'left',30)
				}
				task()
			})
		</script>
	</body>
</html>

缺点:跑出屏幕外并没有做处理。简化版本初始值都设为0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值