用js的style屬性可以獲得html標簽的樣式是不能獲取非行間樣式,那為什么前幾節課老師可以使用啊
function move(target){
clearInterval(timer);
var oDiv=document.getElementById('div1');
timer=setInterval(function(){
var speed=(target-oDiv.offsetLeft)/20;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv.style.offsetLeft==target){
clearInterval(timer);
}else{
oDiv.style.left=oDiv.offsetLeft+speed+"px";//這里不就是可以獲取非行間樣式嗎?
}
},30)
}
function change(obj,target){
clearInterval(obj.timer);
var speed=obj.alpha>target?-10:10;
obj.timer=setInterval(function(){
// var speed=alpha>target?-10:10;
// speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.alpha==target){
clearInterval(obj.timer);
}else{
obj.alpha+=speed;
obj.style.filter='alpha(opacity:'+obj.alpha+')';
obj.style.opacity=obj.alpha/100;//還有這里
}
},30)
}
這些是前面課程的部分內容,感覺跟這節課有點矛盾啊,難道left和opacity不是DOM的屬性嗎
2016-09-23
首先css樣式表有三種樣式,
一、內聯樣式
在HTML標簽用style屬性設置,如 :
<p style="color:#f90;">這是內聯樣式</p>?
二、嵌入樣式
通過<head>標簽內通過<style>標簽設置。如:
?<style type="text/css">
? ? /*這是嵌入樣式*/
? ?.stuff{color:#f90}
</style>
三、外部樣式
通過<link>標簽設置。如:
<link rel="stylesheet" href="path/style.css" type="text/css">
而在javascript中,獲取這三種樣式表的方法是有限制的,style只能獲取元素的內聯樣式,
嵌入樣式和外部樣式使用style是獲取不到的,javascript提供了另外的獲取方式,嵌入樣式和外部樣式可以通過currentStyle(IE瀏覽器)、getComputedStyle(Firefox、opera、safari、chrome瀏覽器)的方式獲取。
使用方法分別是window.currentStyle["attr']和window.getComputedStyle(ob, pseudoElt)["attr']。
哈,其實剛開始的時候小白也不怎么懂,可能小白說的你也不是很明白,你可以參照這篇學習筆記
人家理解得可比我這半吊子水平透徹多了
( ╯□╰ )好吧,小白承認好像抄襲了,逃。。。
2017-06-17
我也遇到同樣的困惑了,style只能獲取元素的內聯樣式,那為什么我們在嵌入樣式上寫的代碼也能獲取的到呢?
2016-09-23
因為沒有加邊框border等屬性,所以使用時有些問題沒有呈現出來。多物體動畫,任務屬性一那節她解釋了。還有加了邊框屬性之后style.width=width+2*border;同理style.height=height+2*border;