在宽度更改时绑定事件,而不是在高度更改时绑定(就像调整大小一样);

Bind event on width change, not height (as resize do both);

本文关键字:绑定 调整 一样 事件 高度      更新时间:2023-09-26

我有下一个javascript代码:

   function() {
        $( this ).on( "resize", $special.handler);
    }

它还可以,但我不需要在高度变化时触发它,我只需要在宽度变化时使用它。

因此,我试图在调整大小检测中添加一个if语句,并仅在宽度更改时绑定$special.handler

还找不到一个简单的方法来做这件事,所以你能帮我找出我做错了什么吗?

目前我已经完成了下一个代码

 function() {
        var widthBefore = $(this).width();
        $( this ).on( "resize", function() {
            if ($(this).width() != widthBefore) {
                $(this).on( WIDTH CHANGE ONLY, $special.handler);
                widthBefore = $(this).width();
            }
        });
    }

没有宽度更改事件,但如果必须维护上下文,则可以使用-

function() {
        var widthBefore = $(this).width();
        $( this ).on( "resize", function() {
            if ($(this).width() != widthBefore) {
                $.proxy($special.handler,this);
                widthBefore = $(this).width();
            }
        });
    }