简体   繁体   中英

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 !

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM