亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么marginLeft不能寫在head下的style里面?

http://img1.sycdn.imooc.com//5837ee6d0001495b10320709.jpg

<!DOCTYPE?html>
<html>
<head>
	<title>Promise?animation</title>
	<meta?charset="utf-8">
	<style?type="text/css">
		.ball{
			width:?40px;
			height:?40px;
			border-radius:?50%;
			margin-left:?0px;
		}
		.ball_01{
			background:?red;
		}
		.ball_02{
			background:?yellow;
		}
		.ball_03{
			background:?green;
		}
	</style>
</head>
<body>
	<div?class="ball?ball_01"?style="margin-left:?0"></div>
	<div?class="ball?ball_02"?style="margin-left:?0"></div>
	<div?class="ball?ball_03"?style="margin-left:?0"></div>
</body>
<script?type="text/javascript"?src="node_modules/bluebird/js/browser/bluebird.js"></script>
<script?type="text/javascript">
	window.onload=function?()?{
		var?ball_01=document.querySelector(".ball_01");
	????var?ball_02=document.querySelector(".ball_02");
	????var?ball_03=document.querySelector(".ball_03");

		function?animate(ball,distance,callback)?{
			setTimeout(function(){
				var?marginLeft?=?parseInt(ball.style.marginLeft,10);
				if(marginLeft?===?distance){
					callback&&callback();
				}else{
					if(marginLeft?<?distance){
						marginLeft++;
					}else{
						marginLeft--;
					}
					ball.style.marginLeft?=?marginLeft?+?"px";
					animate(ball,distance,callback);
				}
			},?13);
		}

		//?animate(ball_01,100,function(){
		//?	animate(ball_02,200,function(){
		//?		animate(ball_03,300,function(){
		//?			animate(ball_03,150,function(){
		//?				animate(ball_02,150,function(){
		//?					animate(ball_01,150,function(){
		//?						//?
		//?					})
		//?				})
		//?			})
		//?		})
		//?	})
		//?})


		var?Promise?=?window.Promise;
		function?promiseAnmiate(ball,distance){
			return?new?Promise(function(resolve,reject){
				function?_animate()?{
					setTimeout(function(){
						var?marginLeft?=?parseInt(ball.style.marginLeft,10);
						if(marginLeft?===?distance){
							resolve();
						}else{
							if(marginLeft<distance){
								marginLeft++;
							}else{
								marginLeft--;
							}
							ball.style.marginLeft?=?marginLeft?+?"px";
							_animate();
						}
					},?13);
				}
				_animate();
			});
		}

		promiseAnmiate(ball_01,100)
		.then(function(){
			return?promiseAnmiate(ball_02,200)
		})
		.then(function(){
			return?promiseAnmiate(ball_03,300)
		})
		.then(function(){
			return?promiseAnmiate(ball_03,150)
		})
		.then(function(){
			return?promiseAnmiate(ball_02,150)
		})
		.then(function(){
			return?promiseAnmiate(ball_01,150)
		})
	}
</script>
</html>


正在回答

2 回答

ball.style.marginLeft 拿到的是行內樣式! style 只是你獲取的這個 DOM節點 的一個屬性而已(如果這個DOM節點 有 style 這個屬性的話) 靜下心來多鞏固下基礎吧。

0 回復 有任何疑惑可以回復我~
#1

精慕門8897441

并不是你說的,.style.marginLeft是可以修改外部寫的樣式的,你可以單獨寫一個函數試試,只修改margionLeft的值,但是在這個例子里不行,不知道怎么回事。
2016-12-20 回復 有任何疑惑可以回復我~
#2

hey自然

哥~看下dom相關的知識再過來說不是我說的這個樣子
2016-12-20 回復 有任何疑惑可以回復我~

如果去掉div下的style,就不能運行,為什么會出現這種問題

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
進擊Node.js基礎(二)
  • 參與學習       76735    人
  • 解答問題       242    個

本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務器端

進入課程

為什么marginLeft不能寫在head下的style里面?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號