-1

I would like to add a custom class on mouseover. So that when the mouse is hovered over .leftbar, a class is added and it should be popped up(I set css for his). How do I add slow or time delay for the popup?

<script>
$(document).ready(function(){
$( ".leftbar" ).mouseenter(function() {
$( "body" ).addClass( "myclass" );
});
});

$(document).ready(function(){
$( ".leftbar" ).mouseleave(function() {
$( "body" ).removeClass( "myclass1" );
});
});
</script>

I tried this- $( "body" ).addClass( "myclass" , '300'); with no luck

Thank you!

Netizen
  • 79
  • 11

4 Answers4

0

You can use setTimeout

$(document).ready(function(){
    $( ".leftbar" ).mouseenter(function() {
        window.setTimeout(function(){
            $( "body" ).addClass( "myclass" );
        }, 300);
    });
}):

See https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout

sn_92
  • 488
  • 3
  • 11
0

Use a setTimeout, being sure to clear it when the cursor leaves.

Minor error, but myclass != myclass1.

$(document).ready(function(){
    var barTimeout = 0;
    $( ".leftbar" ).on({
        mouseenter: function(){
            barTimeout = setTimeout(function(){
                $( "body" ).addClass( "myclass" );
            }, 300);
        },
        mouseleave: function(){
            if( typeof barTimeout !== 'undefined' ) clearTimeout( barTimeout );
            $( "body" ).removeClass( "myclass" );
        }
    });
});

JSFiddle

George
  • 36,413
  • 9
  • 66
  • 103
  • Thanks a lot! This works. I am failing to add easing effect to this code - http://jsfiddle.net/3ts4tb8a/. Can you please help? – Netizen Oct 20 '14 at 16:08
0

You could take a look at the jQuery UI method addClass which allows you to pass in some animation parameters into it. View the example and documentation here http://api.jqueryui.com/addClass/

For your use, it should be as simple as adding in the delay to addClass()

Add a reference to the jQuery Library, then change your code to;

$("body").addClass("myclass", 300);
Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

You can do it like this:

$(document).ready(function () {
    $(".leftbar").hover( function () {
        $(this).delay(300).queue(function(next){
            $(this).addClass("myclass");
            next();
        });
    }, function(){
        $(this).delay(300).queue(function(next){
            $(this).removeClass("myclass");
            next();   
        });
    });
});

Check it out here: JSFiddle

Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28