鼠標(biāo)移出后div繼續(xù)向右移動(dòng)怎么回事
var timer=null;
window.onload=function () {
? ?var oDiv=document.getElementById('div1');
? ?oDiv.onmouseover=function () {
? ? ? ?startMove(0);
? ?}
? ?oDiv.onmouseout=function () {
? ? ? ?startMove(-200);
? ?}
}
function startMove(iTarget) {
? ?clearInterval(timer);
? ?var oDiv=document.getElementById('div1');
? ?timer=setInterval(function () {
? ? ? ?var speed=0;
? ? ? ?if (oDiv.style.left>iTarget){
? ? ? ? ? ?speed=-10;
? ? ? ?}else {
? ? ? ? ? ?speed=10;
? ? ? ?}
? ? ? ?if (oDiv.offsetLeft==iTarget){
? ? ? ? ? ?clearInterval(timer);
? ? ? ?}else{
? ? ? ? ? ?oDiv.style.left=oDiv.offsetLeft+speed+'px';
? ? ? ?}
? ?},30)
}
2016-08-03
??? function moveS(iTarget){
??????? var div1=document.getElementById('div1');
??????? clearInterval(timer);
??????? timer=setInterval(function(){
??????????? var speed;
??????????? if(div1.offsetLeft>iTarget){
??????????????? speed=-10;
??????????? }else{
??????????????? speed=10;
??????????? }
??????????? if(div1.offsetLeft==iTarget ){
??????????????? clearInterval(timer);
??????????? }else{
??????????????? div1.style.left=div1.offsetLeft + speed + 'px';
??????????? }
??????? },30)
??? }
2016-08-03
當(dāng)他完全右移出來后 你沒有判斷
加一句
if(oDiv.offsetLeft==0) {
clearInterval(timer);}
讓計(jì)時(shí)器停止工作就行了