ZubairAnwar
ZubairAnwar

Reputation: 83

Using scroll and resize handlers together

Can I use resize and scroll handlers together like this:

$(window).on('scroll resize',function(){

  if($(window).width() == "1024"){

           if($(window).scrollTop() == 400){
               $('div.foo').addClass('red') ; //to change to red color
            }           

 }

}) ;

The way I used , is it the correct way, what are the implications of this ? Is there a better way to incorporate both the handlers ? Thanks for your reply. Please let me know if you need a simulation of the above code. Thank you !

Upvotes: 0

Views: 51

Answers (1)

Dominique Fortin
Dominique Fortin

Reputation: 2238

do this

var handler = function(){
    if ($(window).width() == "1024") {
      if ($(window).scrollTop() == 400) {
        $('div.foo').addClass('red') ; //to change to red color
      }           
    }           
  };

$(window).on('scroll',handler).on('resize',handler);

Upvotes: 2

Related Questions