jQuery – Oppress $(window).resize() in when scroll in mobile

I found that the $(window).resize() will be triggered when you scroll up/down the website on a mobile device. To prevent this happens, we could add a checking on the screen width.

var width = $(window).width();
$(window).resize(function(){
  if ($(window).width() != width) {
    // Only action on screen width change
    width = $(window).width();
  }
});

 

Done =)

Reference: StackOverflow – javascript resize event on scroll – mobile

2 thoughts on “jQuery – Oppress $(window).resize() in when scroll in mobile”

  1. Yes, exactly! The reason this happens in Chrome seems to be the browserbar that dis- and reappears on scroll. This triggers the resize event. Tracking the width like in the code above prevents triggering the resize event. Thank you!

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.