2012-10-04 56 views
2

我想了解他們,但似乎我不能。所以我想如果有人能幫助我更好地理解這些作品。jQuery懸停,mouseenter,mouseleave狀態(不透明度動畫)

當我添加懸停狀態時,它根本就不透明效果鼠標是否在元件上,或者當鼠標離開元素......它重複它...

而且的mouseenter &離開工作正常,但我不知道如何告訴他一次$(這個),所以我做了一些事情,它可以工作,但也許有人可能會告訴我什麼是正確的和更好的方法。

$("nav.topMenu-left li, nav.topMenu-right li").on('mouseenter', function() { 
    $(this).animate({'opacity': '0.5'}, 100); 
}); 

$("nav.topMenu-left li, nav.topMenu-right li").on('mouseleave', function() { 
    $(this).animate({'opacity': '1'}, 100); 
}); 

回答

1

你可以結合你的事件處理程序:

$("nav.topMenu-left li, nav.topMenu-right li").on('mouseenter mouseleave', function(e) { 
    if (e.type === 'mouseenter') 
     $(this).animate({'opacity': '0.5'}, 100); 
    else 
     $(this).animate({'opacity': '1'}, 100); 
}); 

或者你不是委託的事件,你可以使用hover方法:

$("nav.topMenu-left li, nav.topMenu-right li").hover(function(){ 
    $(this).animate({'opacity': '0.5'}, 100); 
}, function(){ 
    $(this).animate({'opacity': '1'}, 100); 
}) 
+0

他們都工作正常...感謝這兩個他們! 我會玩不同的東西。乾杯。 – dvLden

+0

@NenaddvL好,不客氣。 – undefined

1

如果可以選擇,我願意用CSS做這件事。

示例代碼中使用CSS的:hover財產

CSS

div{ 
    width: 100px; 
    height: 100px; 
    background-color: blue;     
    opacity: .5; 
} 
div:hover{ 
    opacity: 1; 
} 

EXAMPLE

否則,@未定義的解決方案是,你在找什麼=)

+0

我只是不想用緩和的過渡。感謝tho +1 :) – dvLden

+1

啊,那麼@undefined就是對的。 jQuery中的'hover'方法可以接受2個函數(第一個是mouseenter,第二個是mouseleave)'.hover(handlerIn(eventObject),handlerOut(eventObject))'。 http://api.jquery.com/hover/ – Chase