【问题标题】:Do something if screen width is less than 960 px如果屏幕宽度小于 960 像素,请执行某些操作
【发布时间】:2011-12-04 15:12:20
【问题描述】:

如果我的屏幕宽度小于 960 像素,我如何让 jQuery 执行某些操作?无论我的窗口大小如何,下面的代码都会触发第二个警报:

if (screen.width < 960) {
    alert('Less than 960');
}
else {

    alert('More than 960');
}

【问题讨论】:

  • 您是在页面加载时这样做还是在调整浏览器大小时这样做。说 alert(screen.width) 以查看用于决定的内容总是一个好主意。
  • 为什么不media queries
  • 为什么不用媒体查询?我可以想到任何媒体查询都无济于事的情况。如果 jdln 想使用 jQuery,我们假设他有充分的理由这样做。
  • 在媒体查询不起作用的地方有很多用途,例如仅当屏幕宽度大于 1000 像素时才激活砌体效果。

标签: javascript jquery


【解决方案1】:

使用 jQuery 获取窗口的宽度。

if ($(window).width() < 960) {
   alert('Less than 960');
}
else {
   alert('More than 960');
}

【讨论】:

【解决方案2】:

您可能希望将其与调整大小事件结合使用:

 $(window).resize(function() {
  if ($(window).width() < 960) {
     alert('Less than 960');
  }
 else {
    alert('More than 960');
 }
});

对于 R.J.:

var eventFired = 0;

if ($(window).width() < 960) {
    alert('Less than 960');

}
else {
    alert('More than 960');
    eventFired = 1;
}

$(window).on('resize', function() {
    if (!eventFired) {
        if ($(window).width() < 960) {
            alert('Less than 960 resize');
        } else {
            alert('More than 960 resize');
        }
    }
});

我尝试了http://api.jquery.com/off/,但没有成功,所以我选择了 eventFired 标志。

【讨论】:

  • 包装在调整大小事件中只会让它在调整大小时触发,而不是在加载时触发。
  • @Ted 这就是我说combine it with的原因。
  • @R.J.请参阅上面的编辑。我不知道您所说的“清除”事件是什么意思,所以我选择阻止事件再次触发。
  • 太棒了,几天来我一直试图弄清楚如何在调整大小时绑定equalHeights 函数,但仅适用于大于 768 的宽度。这就像一个魅力。谢谢
  • 在该代码之后添加一个调整大小事件是有意义的,以便在加载时无论浏览器的当前大小是什么,它都会运行所需的代码
【解决方案3】:

我建议不要将 jQuery 用于此类事情并继续使用 window.innerWidth:

if (window.innerWidth < 960) {
    doSomething();
}

【讨论】:

  • 为什么最好不要使用 jQuery?
  • @raison 我发现 $(window).width() 不会包含滚动条 - 所以如果你有一个 960px 的屏幕,它将返回 944px 并且你的 js 不会按预期触发.此解决方案将返回 960px
  • @raison 如果不需要,为什么要使用 jQuerry?它带来了额外的问题......
  • 这可能是这里最好的解决方案
  • 这应该是答案,因为不包括滚动条,因此它可以与 CSS 中的媒体查询结合使用。
【解决方案4】:

您还可以使用带有 javascript 的媒体查询。

const mq = window.matchMedia( "(min-width: 960px)" );

if (mq.matches) {
       alert("window width >= 960px");
} else {
     alert("window width < 960px");
}

【讨论】:

    【解决方案5】:
    // Adds and removes body class depending on screen width.
    function screenClass() {
        if($(window).innerWidth() > 960) {
            $('body').addClass('big-screen').removeClass('small-screen');
        } else {
            $('body').addClass('small-screen').removeClass('big-screen');
        }
    }
    
    // Fire.
    screenClass();
    
    // And recheck when window gets resized.
    $(window).bind('resize',function(){
        screenClass();
    });
    

    【讨论】:

      【解决方案6】:

      我建议(需要 jQuery):

      /*
       * windowSize
       * call this function to get windowSize any time
       */
      function windowSize() {
        windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
        windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
      
      }
      
      //Init Function of init it wherever you like...
      windowSize();
      
      // For example, get window size on window resize
      $(window).resize(function() {
        windowSize();
        console.log('width is :', windowWidth, 'Height is :', windowHeight);
        if (windowWidth < 768) {
          console.log('width is under 768px !');
        }
      });
      

      在 CodePen 中添加: http://codepen.io/moabi/pen/QNRqpY?editors=0011

      然后您可以使用 var 轻松获取窗口的宽度:windowWidth 和高度:windowHeight

      否则,获取一个 js 库: http://wicky.nillia.ms/enquire.js/

      【讨论】:

        【解决方案7】:

        使用

        $(window).width()
        

        $(document).width()
        

        $('body').width()
        

        【讨论】:

          【解决方案8】:

          我知道我回答这个问题迟了,但我希望它对任何有类似问题的人有所帮助。当页面出于任何原因刷新时它也可以工作。

          $(document).ready(function(){
          
          if ($(window).width() < 960 && $(window).load()) {
                  $("#up").hide();
              }
          
              if($(window).load()){
                  if ($(window).width() < 960) {
                  $("#up").hide();
                  }
              }
          
          $(window).resize(function() {
              if ($(window).width() < 960 && $(window).load()) {
                  $("#up").hide();
              }
              else{
                  $("#up").show();
              }
          
              if($(window).load()){
                  if ($(window).width() < 960) {
                  $("#up").hide();
                  }
              }
              else{
                  $("#up").show();
              }
          
          });});
          

          【讨论】:

            【解决方案9】:

            试试这个代码

            if ($(window).width() < 960) {
             alert('width is less than 960px');
            }
            else {
             alert('More than 960');
            }
            

               if ($(window).width() < 960) {
                 alert('width is less than 960px');
                }
                else {
                 alert('More than 960');
                }
            &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

            【讨论】:

              【解决方案10】:

              不,这些都行不通。你需要的是this!!!

              试试这个:

              if (screen.width <= 960) {
                alert('Less than 960');
              } else if (screen.width >960) {
                alert('More than 960');
              }
              

              【讨论】:

              • 发布链接不是答案。
              • 对不起,我有点赶时间!等一下,我现在就在做……
              • 请注意如果 screen.width (如果使用 vanilla JS 最好使用 document.body.clientWidth 捕获)是 960 ) 因此使用 else if here 是多余的。使用document.body.clientWidth &lt;=960 ? handleSmallerThan960() : handleLargerThan960() 或一些变体。不需要 else if(冗余代码)
              猜你喜欢
              • 2013-03-07
              • 1970-01-01
              • 2023-03-27
              • 2012-02-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-12-26
              • 1970-01-01
              相关资源
              最近更新 更多