tags:

views:

155

answers:

2

I used

$(document).ready(function() {

$("#main ul li").hover(function(){

var fade = $('img.g-img', this);
fade.fadeTo('slow', 0.5);


$("#main ul li").removeClass("active");
$(this).toggleClass("active");

});


}); 

I want to stop fade, when mouseout. This code not working:

fade.stop().fadeTo('slow',1)

How can I do this? Thanks in advance.

+1  A: 

You need to pass two parameters to hover. (A mouseenter handler and a mouseleave handler)

For example:

$(document).ready(function() {
    $("#main ul li").hover(
        function() { //mouseenter handler
            var fade = $('img.g-img', this);
            fade.fadeTo('slow', 0.5);

            $("#main ul li").removeClass("active");
            $(this).toggleClass("active");
        }, 
        function () {  //mouseleave handler
            var fade = $('img.g-img', this);
            fade.stop().fadeTo('slow',1)
        }
    );
});
SLaks
in your mouseleave handler, fade is out of scope. You'll need to define fade before calling hover(), or define it in your mouseleave handler too.
Kyle Trauberman
@Kyle: I forgot about that; thanks.
SLaks
This code working perfect, Thanks!
+1  A: 

The fade variable you are defining is only in scope within the event handler. Plus you need two event handlers for hover(): one for when the mouse enters and another for when it leaves.

$(document).ready(function() {
  $("#main ul li").hover(function() {
    $(this).addClass("active").find("img.g-img").stop().fadeTo("slow", 0.5);
  }, function() {
    $(this).removeclass("active").find("img.g-img").stop().fadeTo("slow", 1);
  });
});
cletus
Hi cletus;Thanks for help.This code not remove the previous fading