2016-12-04 71 views
0

如果我將另一個div懸停,我想減少某些div的不透明度,但是如果我停止懸停div,則不透明度應該再次變回1離開懸停時的事件

HTML

<div id="feld1" onmouseover="show1()"> </div> 
<div id="feld2" onmouseover="show2()"> </div> 
<div id="feld3" onmouseover="show3()"> </div> 

JS

function show1() { 
    if ($('#feld1:hover').length != 0) { 
    document.getElementById("feld2").style.opacity = 0.1; 
    document.getElementById("feld3").style.opacity = 0.1; 
    } else { 
    document.getElementById("feld2").style.opacity = 1; 
    document.getElementById("feld3").style.opacity = 1; 
    } 
} 

它是如何工作的任何想法?

+0

我想你想[mouseout事件](http://www.w3schools.com/jsref/event_onmouseout.asp) – TheUknown

+0

你有OnMouseLeave在以及HTTP: //www.w3schools.com/jsref/event_onmouseleave.asp –

+0

你可以用CSS做到這一點。 '#feld1:hover〜div [id^= feld] {opacity:0.1}'https://jsfiddle.net/qk1ymooo/1/ – 2016-12-04 23:59:56

回答

1

好吧,我知道了......

document.getElementById("feld1").onmouseover = function() {mouseOver()}; 
document.getElementById("feld1").onmouseout = function() {mouseOut()}; 

function mouseOver() { 

    document.getElementById("feld2").style.opacity = 0.1; 
    document.getElementById("feld3").style.opacity = 0.1; 
} 

function mouseOut() { 
    document.getElementById("feld2").style.opacity = 1; 
    document.getElementById("feld3").style.opacity = 1; 
} 
+1

'document.getElementById(「feld1」)。onmouseover = mouseOver;'不要需要包裝功能。確保你不包含'()'。 – 2016-12-05 00:01:42