简体   繁体   中英

jQuery resize listener to be triggered only change in width

I need to trigger a function only when the there is a change in width and not in height. How can I do that without duplicating the function?

$(window).resize(function() {
var pageWidth = $(window).width();
if(pageWidth > 100){
    $("#content").show();
}
if(pageWidth < 100){
    $("#content").hide();
}
}); 

Actually I think I figured it out

var initialWidth = $(window).width();
$(window).resize(function() {
  var pageWidth = $(window).width();
  if(pageWidth > 100 && pageWidth !== initialWidth){
    $("#content").show();
    initialWidth = pageWidth;
  }
  if(pageWidth < 100 && pageWidth !== initialWidth){
    $("#content").hide();
    initialWidth = pageWidth;
  }
});

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