Lars Jensen
Lars Jensen

Reputation: 67

How to trigger?

I have a little piece of code I want to trigger when the windows gets to 640px. or below I have tried many different suggestions, I have found here or elsewhere but none of them works.

$(function() {
    $('p').css('display','none');
        $('h1').click(function(){
            $('p').stop(true,true).slideToggle(1000);
    });
});

Upvotes: 4

Views: 49

Answers (1)

Phiter
Phiter

Reputation: 14992

You can bind window resize events by doing:

$(window).on('resize', function(event){
// Do stuff here
});

You can get the window size by doing:

var windowWidth = $(window).width();
if(windowWidth < 640){
    // Do stuff here
}

So, your function will work like this:

$(window).on('resize', function(event)
    {
       var windowWidth = $(window).width();
       if(windowWidth < 640){
       $('p').css('display','none');
           $('h1').click(function(){
               $('p').stop(true,true).slideToggle(1000);
       });

    }
});

Upvotes: 4

Related Questions