简体   繁体   中英

jquery for an if statement to act like CSS media query

I have this jquery code which works great:

$(document).scroll(function(){
    if ($(this).scrollTop()>175){
        // animate fixed div to small size:
        $('header').stop().animate({ height: 90 },100);
    } else {
        //  animate fixed div to original size
        $('header').stop().animate({ height: 175 },100);
    }
});

I need the above to be present and read by all browsers/devices at all times but I also need to adjust the height of the smaller header (currently 90px - beneath the line // animate fixed div to small size:) for ONLY browser widths between 641px and 959px. I need the height of the header to be 120px for those browser widths only otherwise use the above code.

A bit like Media queries where I'd like it update itself as the browser is resized.

How do I do this exactly?

$(window).width();

is what you are looking for. And,

$(window).on("resize",function(){  
   if($(window).width()>641 && $(window).width()<959){
          // do the animations
   }    
}

is the main idea of it.

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