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

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

為什么把margin-left寫在css里就不能用ball.style.marginLeft了?

<!doctype html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>Promise</title>
? ?<style>
? ? ? ?.ball{width:40px;height: 40px;border-radius: 50%; margin-left: 0px;}
? ? ? ?.ball1{background: red;margin-left: 0px;}
? ? ? ?.ball2{background: green;}
? ? ? ?.ball3{background: blue;}
? ?</style>
</head>
<body>
? ?<div class="ball ball1" ></div>
? ?<div class="ball ball2" ></div>
? ?<div class="ball ball3" ></div>

<script>
? ?var ball1 = document.querySelector('.ball1');
? ?var ball2 = document.querySelector('.ball2');
? ?var ball2 = document.querySelector('.ball3');

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

? ?//ani(ball1,100,function(){})

</script>
</body>
</html>



如果 這樣寫 ?而不是寫行內 ? ?就無法用var marle = parseInt(ball.style.marginLeft,10); ? 這是為什么

正在回答

2 回答

用style獲取css樣式,只能獲取到寫到標簽內的樣式。參考網址:http://www.cnblogs.com/liulin0524/p/5315908.html

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

js里獲取style里的樣式屬性不同于直接獲取dom節點里的屬性

可借鑒

function GetCurrentStyle (obj, prop) {?????
??? if (obj.currentStyle) {????????
??????? return obj.currentStyle[prop];?????
??? }??????
??? else if (window.getComputedStyle) {????????
??????? propprop = prop.replace (/([A-Z])/g, “-$1″);???????????
??????? propprop = prop.toLowerCase ();????????
??????? return document.defaultView.getComputedStyle (obj,null)[prop];?????
??? }??????
??? return null;???
} ?


alert(GetCurrentStyle(dd,”width”)); ???

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

舉報

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

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

進入課程

為什么把margin-left寫在css里就不能用ball.style.marginLeft了?

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

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

幫助反饋 APP下載

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

公眾號

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