為什么實現(xiàn)不了和老師一樣的效果,各位大神,求助
window.onload=function(){
var div=document.getElementById('move'),
a=div.getElementsByTagName('a');
for(var i=0;i<a.length;i++){
a[i].onmouseover=function(){
var _this=this.getElementsByTagName('img')[0];
startmove(_this,{top:-25,opacity:0},function(){
_this.style.top=30+'px';
startmove(_this,{top:25,opacity:1});
})
}
}
}
function getstyle(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
//startmove(obj,{attr1:target1,attr2:target2}):同時傳入多個屬性與屬性值
function startmove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var flag=true;//假設(shè)所有的運動都到達目標值
for(var attr in json){
//取當前值
var icur=0;
if (attr=='opacity') {
icur=Math.round(parseFloat(getstyle(obj,attr))*100);
}else{
icur=parseInt(getstyle(obj,attr));
}
//算速度
var speed=(json[attr]-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
//檢測停止
//某一個屬性到達目標值之后就會清除定時器,這時候如果其中有一個屬性比較快到達目標值時,那么就會觸發(fā)清除定時器,導(dǎo)致其它的屬性還沒有到達目標值,就被迫停止了。所以得讓所有得屬性到達目標值時才能關(guān)閉定時器
if (icur!=json[attr]) {
flag=false;
}
//如果屬性是透明度的話要用if另做判斷,因為透明度沒有單位,和其他屬性不一樣
if (attr=='opacity') {
obj.style.filter='alpha(opacity:'+(icur+speed)+')';
obj.style.opacity=(icur+speed)/100;
}else{
obj.style[attr]=icur+speed+'px';
}
}
//如果所有的運動都到達目標值,那么就觸發(fā)觸發(fā)清除定時器,如果有外加函數(shù),那么就再執(zhí)行外加函數(shù)fn()
if (flag==true) {
clearInterval(obj.timer);
//如果有外加函數(shù),就執(zhí)行
if (fn) {
fn();
}
}
},30)
}
實現(xiàn)不了效果會是我結(jié)構(gòu)上的問題嗎?求大神幫忙看下,謝謝!
2017-01-17
是這個 ?
var _this=_this.getElementsByTagName('i')[0];
2022-03-28
電話號碼布局
ltobaywfnkqeslrdqzvzrctaticjkzfjngkdpxeltwhyzstxetqgkzlsavvxmolmrmjvjsgiedkirwmskfbrgywseknwoqyrwpfcktufqqkm
2017-01-17
var _this=this.getElementsByTagName('img')[0];
改成
var _this=this.getElementsByTagName('i')[0];