Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay and animate toggle jquery

Tags:

jquery

toggle

I am trying to play arround and I am totally newbie with jquery! So I need some help definetly :)

$(function() {
        $('#switch').on('click', function() {
            $('#customOverlay').toggle();
        });
    });

I made an light switcher and I am trying to turn the light on/off by adding customOverlay.

It works really fine but not as expected. I want to delay it for like 1000ms and I want to animate it cause this way it just turn the visibility on and off really quick. Is this possible cause I cannot even delay it and I dont know how would I animate toggle.

Thank you in advance!

like image 775
dvlden Avatar asked Oct 09 '12 17:10

dvlden


2 Answers

Try .fadeToggle() :

$(function() {
        $('#switch').on('click', function() {
            $('#customOverlay').delay(1000).fadeToggle();
        });
});

OR

$(function() {
        $('#switch').on('click', function() {
            $('#customOverlay').fadeToggle(1000);
        });
});

The other answers that only use .delay() and .toggle won't work because .delay() only applies to the animation queue and .toggle() doesn't animate, while .fadeToggle() does.

like image 122
j08691 Avatar answered Oct 20 '22 14:10

j08691


Try using .delay()

$('#customOverlay').delay(1000).fadeToggle();
like image 23
Sushanth -- Avatar answered Oct 20 '22 15:10

Sushanth --