jQuery,如果宽度小于,请更改此属性 [英] jquery if width is less than, change this attr

查看:102
本文介绍了jQuery,如果宽度小于,请更改此属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $(".section").each(function(i, el){
    var section = $('.section');
    var width = section.width();
    if (width < 960)
        section.attr('class', 'section-slim');
});

这似乎很好用,在刷新浏览器时,如何使它在调整大小时起作用? 例如:如果有人缩小浏览器窗口的大小,会添加此类吗?

This seems to work fine, on browser refresh, how do I make it act when resized? Ex: if someone makes their browser window smaller, it will add this class?

推荐答案

将事件绑定到jQuery的调整大小"事件

bind the event to jQuery's "resize" event

$(window).on("resize load", function () {
    $(".section").each(function(i, el){
    var section = $('.section');
    var width = section.width();
    if (width <= 960) {
        section.attr('class', 'section-slim');
    }
})

这篇关于jQuery,如果宽度小于,请更改此属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆