javascript – JS或jQuery或窗口调整大小或窗口宽度小于npx

栏目: jQuery · 发布时间: 7年前

内容简介:我正在寻找一个脚本,当用户调整窗口大小或窗口宽度小于900px时,它会进行somenthing.翻译自:https://stackoverflow.com/questions/10642424/js-or-jquery-or-window-resize-or-when-window-width-is-less-than-npx

我正在寻找一个脚本,当用户调整窗口大小或窗口宽度小于900px时,它会进行somenthing.

嗯,你可以使用jQuery,但……

您可以订阅窗口调整大小事件,如下所示:

$(window).on("resize", function(event){
  console.log( $(this).width() );
});

请注意这一点,因为当宽度小于n时执行代码的if语句在调整满足这些条件的窗口时会执行该代码的次数非常多.最好设置一些标志或向文档添加类,并使这些部分成为您的条件.

CSS媒体查询就在哪里!

但是,你问的问题听起来最适合用 CSS Media Queries 来解决.例如,如果我们想在窗口小于900px时改变主体背景颜色:

@media screen and (max-width: 900px) {
  body {
    background: #ccc;
  }
}

对于旧版IE: https://github.com/scottjehl/Respond ,甚至还有一个很好的polyfill

翻译自:https://stackoverflow.com/questions/10642424/js-or-jquery-or-window-resize-or-when-window-width-is-less-than-npx


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Web Security Testing Cookbook

Web Security Testing Cookbook

Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99

Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具