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


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

查看所有标签

猜你喜欢:

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

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具