【问题标题】:How do I stop resize function from firing after a css value has changed?如何在 css 值更改后停止调整大小功能?
【发布时间】:2012-11-02 12:15:43
【问题描述】:

在通过 CSS 媒体查询更改 css 值后,我试图阻止调整大小函数触发。这不起作用......我希望“有一个切换”只出现一次。我该如何杀死它?

$(window).bind('resize', function(event) {
    if ($('#toggle').css('display') == 'none')
        {
            console.log("there's no toggle!");
        }
        else {
            console.log("there IS a toggle!");
            return false; // trying to stop the function
        }
});

【问题讨论】:

    标签: jquery return media-queries window-resize stoppropagation


    【解决方案1】:

    应该这样写,使用 .on() 和 .off()。

    $(window).on('resize', function() {
    
    if ($('#toggle').css('display') == 'none')
        {
            console.log("there's no toggle!");
        }
        else {
            console.log("there IS a toggle!");
            $(window).off('resize');
        }
    });
    

    【讨论】:

    • 使用 on 和 off 比 bind 和 unbind 有什么优势?
    • 从 jquery 1.7 开始,首选 .on() 处理程序...最终 .bind() 将被弃用(被踢到路边),就像他们对 .live() 所做的那样 .on() handler 更灵活,可以同时替换 .live() 和 .bind() .... 你可以在这里阅读更多内容 :) api.jquery.com/bind
    【解决方案2】:

    使用unbind()

    $(window).unbind('resize');
    

    /

    else {
            console.log("there IS a toggle!");
            $(window).unbind('resize');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多