javascript中clearInterval问题
我做的是一个滚动图片,当鼠标移动到图片上时,图片停止滚动,离开继续滚动如果不加window.onload,在IE6下图片不滚动,但是加上window.onload后,在i...
我做的是一个滚动图片,当鼠标移动到图片上时,图片停止滚动,离开继续滚动
如果不加window.onload,在IE6下图片不滚动,
但是加上window.onload后,在ie8,火狐,谷歌中,当鼠标移动到图片上时,图片仍然在滚动
window.onload=function(){
setInterval(movePic,10);
};
var mp=setInterval(movePic, 10);
function movePic(){
var show=document.getElementById('show');
var mypic=document.getElementById('mypic');
var mypic2=document.getElementById('mypic2');
var w=mypic.offsetWidth;
mypic2.innerHTML=mypic.innerHTML;
if(mypic.offsetWidth-show.scrollLeft<=0){
show.scrollLeft-=mypic.offsetWidth;
}else{
show.scrollLeft+=3;
}
}
function stopMove(){
clearInterval(mp);
}
function keepMove(){
mp=setInterval(movePic, 10);
}
<div id="show" onmouseover="stopMove()" onmouseout="keepMove()"> 展开
如果不加window.onload,在IE6下图片不滚动,
但是加上window.onload后,在ie8,火狐,谷歌中,当鼠标移动到图片上时,图片仍然在滚动
window.onload=function(){
setInterval(movePic,10);
};
var mp=setInterval(movePic, 10);
function movePic(){
var show=document.getElementById('show');
var mypic=document.getElementById('mypic');
var mypic2=document.getElementById('mypic2');
var w=mypic.offsetWidth;
mypic2.innerHTML=mypic.innerHTML;
if(mypic.offsetWidth-show.scrollLeft<=0){
show.scrollLeft-=mypic.offsetWidth;
}else{
show.scrollLeft+=3;
}
}
function stopMove(){
clearInterval(mp);
}
function keepMove(){
mp=setInterval(movePic, 10);
}
<div id="show" onmouseover="stopMove()" onmouseout="keepMove()"> 展开
1个回答
展开全部
你的代码写的有问题。帮你改了:
window.onload=function(){
mp = setInterval(movePic,10);
};
var mp = null;
function movePic(){
var show=document.getElementById('show');
var mypic=document.getElementById('mypic');
var mypic2=document.getElementById('mypic2');
var w=mypic.offsetWidth;
mypic2.innerHTML=mypic.innerHTML;
if(mypic.offsetWidth-show.scrollLeft<=0){
show.scrollLeft-=mypic.offsetWidth;
}else{
show.scrollLeft+=3;
}
}
function stopMove(){
clearInterval(mp);
mp = null;
}
function keepMove(){
if(mp) { return; }
mp = setInterval(movePic, 10);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询