【问题标题】:jQuery resize listener to be triggered only change in widthjQuery调整大小监听器仅触发宽度变化
【发布时间】:2015-06-08 00:38:06
【问题描述】:

只有当宽度发生变化而不是高度发生变化时,我才需要触发一个函数。 如何在不复制功能的情况下做到这一点?

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

【问题讨论】:

    标签: javascript jquery resize width listener


    【解决方案1】:

    其实我觉得我想通了

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多