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

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

大神幫忙找找錯,為什么說getComputedStyle在window下獲取失敗?

<!doctype?html>
<html>
<head>
<meta?charset="utf-8">
<title>鏈式運動</title>
<style>
*{
	padding:0;
	margin:0;
	list-style:none;
}
#box{
	margin:0?auto;
	background:orange;
	width:200px;
	height:200px;
	opacity:0.3;
	filter:alpha(opacity:30);
	margin-bottom:20px;
}
</style>
<script>
window.onload=function(){
	var?div=document.getElementById("box");
	div.onmouseover=function(){
		change(this,'width',400,function(){
			change(this,'height',400);
		});
	}
}
function?getStyle(obj,attr){
	if(obj.currentStyle){
		return?obj.currentStyle[attr];
	}
	else{
		return?getComputedStyle(obj,false)[attr];
	}
}

function?change(obj,attr,target,fn){
	clearInterval(obj.timer);

	obj.timer?=?setInterval(function(){
		var?val?=?0;
		if(attr?==?'opacity'){
			val?=?Math.round(parseFloat(getStyle(obj,attr))*100);
		}
		else{
			val?=?parseInt(getStyle(obj,attr));
		}


		var?speed?=?(target-val)/8;
		speed?=?speed>0???Math.ceil(speed)?:?Math.floor(speed);


		if(val?==?target){
			clearInterval(obj.timer);
//判斷是否傳入了fn
			if(fn){
				fn();
			}
		}
		else{
			if(attr?==?'opacity'){
				obj.style.filter?=?'alpha(opacity:'?+?val+speed?+?')';
				obj.style.opacity?=?(val+speed)/100;
			}
			else{
				obj.style[attr]?=?val?+?speed?+"px";
			}
		}
	},30)
}
</script>
</head>
<body>
<div?id="box"></div>
</body>
</html>


正在回答

4 回答

別直接用This,將this賦值給一個變量,如var that=this;

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

但是如果是for循環里面的話 ?this就不能改啊

0 回復 有任何疑惑可以回復我~
window.onload=function(){
????var?div=document.getElementById("box");
????div.onmouseover=function(){
????????change(this,'width',400,function(){
????????????change(this,'height',400);
????????});
????}
}

這里的外層change()可以傳this,但是到了里層change()時,this就代表window對象了??梢愿某蒬iv

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

舉報

0/150
提交
取消
JS動畫效果
  • 參與學習       113918    人
  • 解答問題       1502    個

通過本課程JS動畫的學習,從簡單動畫開始,逐步深入各種動畫框架封裝

進入課程

大神幫忙找找錯,為什么說getComputedStyle在window下獲取失???

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

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

幫助反饋 APP下載

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

公眾號

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