简体   繁体   English

仅在调整窗口宽度时才调用jquery函数

[英]Call jquery function only when window width gets resized

I have a function responsive that changes behaviour of certain elements on my website, including hiding popups etc. I call it in 2 cases: 我有一个responsive功能,可以更改网站上某些元素的行为,包括隐藏弹出窗口等。我在2种情况下称其为:

$(document).ready(responsive);
$(window).resize(responsive);

The problem occurs on android chrome, as the virtual keyboard actually changes the height of the screen, and triggers responsive function, which closes my popups (some of them have text fields, making it impossible to type). 问题发生在android chrome上,因为虚拟键盘实际上改变了屏幕的高度,并触发了responsive功能,从而关闭了我的弹出式窗口(其中一些具有文本字段,因此无法键入)。

How can I prevent this from happening? 如何防止这种情况发生? I read somewhere a good point that android virtual keyboard only changes height of the screen, not a width, so I assume it would be a good idea to compare width before and after resize. 我在某处读到了一个很好的观点,即android虚拟键盘只会改变屏幕的高度,而不是宽度,因此我认为比较调整大小前后的宽度是一个好主意。 So I created this function to compare the widths before and after and run resize() if width is different, but it doesn't work as expected, and console logs show different document widths even though I only changed the height of the screen (using chrome developer tools). 因此,我创建了此函数来比较前后的宽度,并在宽度不同的情况下运行resize() ,但无法按预期运行,并且即使我仅更改了屏幕的高度,控制台日志也显示了不同的文档宽度(使用chrome开发人员工具)。

Any idea what went wrong or how can I prevent function responsive being launched on height change? 知道发生了什么问题或如何防止高度变化时启动功能responsive

function resizeWidth() {
    var existingWidth = $(document).width();

    $(window).resize(function() {
        var newWidth = $(document).width();
        if (existingWidth != newWidth) {
            $(window).resize(responsive);
            console.log(existingWidth);
            console.log(newWidth);
        };
    });
};

$(window).resize(resizeWidth);

Firstly you are attaching a handler to the resize event multiple times. 首先,您要多次将处理程序附加到resize事件。 One on load, then another every time the resize happens and resizeWidth is called. 一个加载时, 每次重新调整大小并resizeWidth时再加载一次。 You should remove the handler within that function. 您应该在该函数中删除处理程序。 Also, I guess you just want to call the responsive() function, not attach yet another resize handler when the width changes. 另外,我猜您只是想调用responsive()函数,而不是在宽度改变时附加另一个调整大小处理程序。

The main issue you have is that the scope of existingWidth is not low enough for it to be seen over multiple events. 你拥有的主要问题是,范围existingWidth是不是足够低为它显现在多个事件。 You could make it global, although that is generally considered bad practice. 您可以将其设置为全局,尽管通常认为这是不好的做法。 Instead you could use a data attribute, like this: 相反,您可以使用data属性,如下所示:

function resizeWidth() {
    var existingWidth = $(document).data('resize-width');
    var newWidth = $(document).width();
    if (existingWidth != newWidth) {
         responsive();
         $(document).data('resize-width', newWidth);
    };
};

$(window).resize(resizeWidth);

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

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