2017-05-04 59 views
0

我希望div adiv b在鼠標結束時切換位置div b然後在鼠標離開時切換回div a。但它的超級毛病,即使鼠標沒有離開div a也會切換。當鼠標仍然在div a時,我如何獲得它到而不是運行navMouseOut,爲什麼它這樣做。 (請測試代碼,看看什麼錯)onmouseover with javascript

document.getElementById("b").onmouseover = function() { 
 
    navMouseOver() 
 
}; 
 
document.getElementById("a").onmouseout = function() { 
 
    navMouseOut() 
 
}; 
 

 
function navMouseOver() { 
 
    document.getElementById("a").style = ("top: 50%;"); 
 
    document.getElementById("b").style = ("top: 40%; "); 
 
} 
 

 
function navMouseOut() { 
 
    document.getElementById("a").style = ("top: 40%;"); 
 
    document.getElementById("b").style = ("top: 50%;"); 
 
}
#a { 
 
    position: fixed; 
 
    top: 40%; 
 
    left: 20%; 
 
    background-color: red; 
 
} 
 

 
#b { 
 
    position: fixed; 
 
    top: 50%; 
 
    left: 20%; 
 
    background-color: lightblue; 
 
}
<div id="a"> 
 
    <p>content a</p> 
 
</div> 
 
<div id="b"> 
 
    <p>content b...</p> 
 
</div>

+0

順便說一句,你不需要匿名函數包裝到其他函數的調用,只是說'的document.getElementById( 「B」)的onmouseover = navMouseOver;'。 – nnnnnn

回答

0

使用OnMouseEnter在代替的onmouseover

document.getElementById("b").onmouseenter = function() { 
navMouseOver() 
}; 

document.getElementById("a").onmouseout = function() { 
navMouseOut() 
}; 
+0

下面是一個演示如何做到這一點的小提琴:https://jsfiddle.net/8ohoe1dm/ – Nisarg

+0

如果您添加了解釋,這個答案會更有幫助。 – nnnnnn

0

我認爲這個問題是在傳播,讓看到這個函數的onmouseout,即使你將鼠標移出P元素,但仍然在DIV中,onmouseout仍然會執行。

你可以寫HTML這樣的:

<div id="a"> 
    content a 
</div> 
<div id="b"> 
    content b... 
</div> 

或使用stoppropagation()或cancelBubble()

1

的問題是,當元素切換位置,mouseovermouseenter事件被觸發爲元素,新定位在鼠標上。爲了防止這種情況,您可以使用切換爲真的標誌來決定是否運行重新定位代碼。

var target1 = document.getElementById("mouseoverTarget1"); 
 
var switching = false; 
 
var inStartingPosition = true; 
 

 
target1.addEventListener("mouseover", function() { 
 
    if (!switching) { 
 
    switching = true; 
 
    target1.style.top = inStartingPosition ? "50px" : "0px"; 
 
    target2.style.top = inStartingPosition ? "-50px" : "0px"; 
 
    inStartingPosition = inStartingPosition ? false : true; 
 
    console.log("mouseover target 1"); 
 
    setTimeout(() => { 
 
     switching = false; 
 
    }, 100); 
 
    } 
 
}); 
 

 
var target2 = document.getElementById("mouseoverTarget2"); 
 

 
target2.addEventListener("mouseover", function() { 
 
    if (!switching) { 
 
    switching = true; 
 
    target1.style.top = inStartingPosition ? "50px" : "0px"; 
 
    target2.style.top = inStartingPosition ? "-50px" : "0px"; 
 
    inStartingPosition = inStartingPosition ? false : true; 
 
    console.log("mouseover target 2"); 
 
    setTimeout(() => { 
 
     switching = false; 
 
    }, 100); 
 
    } 
 
});
#mouseoverTarget1, #mouseoverTarget2 { 
 
    width: 50px; 
 
    height: 50px; 
 
    position: relative; 
 
} 
 

 
#mouseoverTarget1 { 
 
    background-color: red; 
 
} 
 

 
#mouseoverTarget2 { 
 
    background-color: blue; 
 
}
<div id="mouseoverTarget1"></div> 
 
<div id="mouseoverTarget2"></div>

+0

*「當鼠標懸停在元素上時,鼠標懸停事件會重複發生。」* - 只有當它發生泡沫時纔會發生,所以如果鼠標移動到元素的子元素上,它會再次觸發。只留在元素上不會導致它重複觸發。但是,mouseenter不起泡。 – nnnnnn