已采纳回答 / 呆4
你没有清除样式,也就是加上这段*{margin:0;padding:0;}。导致你div的left并不等于200这个整数,导致你后面odiv.style.left = odiv.offsetLeft+10+'px';时,至直接跳过了odiv.offsetLeft == 100这个数,从而使得你的程序停不下来,你可以先把if(odiv.offsetLeft == 100){ clearInterval(timer); }修改为if(odiv.offsetLeft <= 100){ ...
2016-03-16
var time=null;
function startMove(obj,Target,attr,opacity,speed,fn){
clearInterval(time);
if(parseInt(getStyle(obj,attr))==Target){
clearInterval(time);
if(fn){
fn();
}else{
obj.style[attr]=parseInt(getStyle(obj,attr))+speed+"px";
obj.style.opacity=opacity;
}
}
}
function startMove(obj,Target,attr,opacity,speed,fn){
clearInterval(time);
if(parseInt(getStyle(obj,attr))==Target){
clearInterval(time);
if(fn){
fn();
}else{
obj.style[attr]=parseInt(getStyle(obj,attr))+speed+"px";
obj.style.opacity=opacity;
}
}
}
2016-03-12
var oDiv=document.getElementById("box");
oDiv.onmouseover=function(){
var obj=this;
startMove(obj,400,"width",1,5,function(){
startMove(obj,300,"height",0.5,5);
});
oDiv.onmouseleave=function(){
var obj=this;
startMove(obj,200,"height",1,-5,function(){
startMove(obj,200,"height",0.5,-5);
})
}
}
oDiv.onmouseover=function(){
var obj=this;
startMove(obj,400,"width",1,5,function(){
startMove(obj,300,"height",0.5,5);
});
oDiv.onmouseleave=function(){
var obj=this;
startMove(obj,200,"height",1,-5,function(){
startMove(obj,200,"height",0.5,-5);
})
}
}
2016-03-12
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getcomputedStyle(obj,false)[attr];
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getcomputedStyle(obj,false)[attr];
}
}