简体   繁体   English

绑定事件的宽度变化,而不是高度变化(调整大小时都这样做);

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

I have next javascript code: 我有下一个JavaScript代码:

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

it works ok but I dont need this to trigger on height change, I need to work it only on a widht change. 它可以正常工作,但是我不需要在高度变化时触发它,我只需要在高度变化时就可以工作。

So I'm trying to add an if statement inside resize detection and to bind $special.handler only if width changes. 所以我试图在大小调整检测中添加一个if语句,并且仅在宽度改变时才绑定$special.handler

Can't find a simple way to do this yet, so would you be so kind to help me find what I'm doing wrong? 尚无法找到一种简单的方法来执行此操作,那么您是否会帮助我发现我在做什么错呢?

For the moment I've ended up with next code 目前,我已经完成了下一个代码

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

There is no width change event but if context has to be maintained this can be used - 没有宽度更改事件,但是如果必须维护上下文,则可以使用-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM