简体   繁体   English

$(window).resize 无限循环

[英]$(window).resize goes on infinite loop

If someone is able to find the issue, that would help me a lot!如果有人能够找到问题,那将对我有很大帮助!

I have created a script that shows a banner on the top of a page only if the width of the windows is less or equal than 600px and if the scroll is less than 34px away from the top, then hide it if the user scrolls more than 34px from the top or if the window width makes more than 600px.我创建了一个脚本,仅当 windows 的宽度小于或等于 600px 并且滚动距离顶部小于 34px 时,才在页面顶部显示横幅,然后隐藏它如果用户滚动超过距顶部 34 像素,或者如果 window 宽度超过 600 像素。

It works well but then I have tried to do the same thing if the user resizes the window, and there comes the troubles.它工作得很好,但是如果用户调整 window 的大小,我会尝试做同样的事情,麻烦就来了。 When I add the code to do that, the banner is showed even if the window is resized to more than 600px and the banner is showed/hidden/showed/hidden/etc.当我添加代码来执行此操作时,即使将 window 的大小调整为超过 600 像素并且横幅显示/隐藏/显示/隐藏/等,也会显示横幅。 several times in a row.连续几次。

Here is my script:这是我的脚本:

$(document).ready(function() {
            checkWidth();
        });
        var resizeId;
        $(window).resize(function() {
            clearTimeout(resizeId);
            resizeId = setTimeout(checkWidth, 500);
         });

        function checkWidth(){
            if(window.innerWidth <= 600){
                var scrolled = false;
                $(window).scroll(function() {
                    if ($(window).scrollTop() > 34 && scrolled == false) {
                        $("#balancemtop").slideToggle("slow");
                        scrolled = true;
                    } else if ($(window).scrollTop() < 34 && scrolled == true) {
                        scrolled = false;
                        $("#balancemtop").slideToggle("slow");
                    }
                });
            } else {
                $("#balancemtop").css("display", "none");
                }
            };

Any idea?任何想法?

Thanks in advance for any help!提前感谢您的帮助!

Try taking this call $(window).scroll(function() {... outside of your checkWidth function. Since this call is assigning an event handler, every time you call checkWidth, you are adding another event handler. This does not replace the previous event handle,r but rather stacks on top of it so that after scrolling a couple of times, you are stuck with a ton of copies of the same event handler. Eventually, if you scroll enough this will cause your page to become unresponsive.尝试在您的 checkWidth function 之外进行此调用$(window).scroll(function() {...由于此调用分配了一个事件处理程序,因此每次调用 checkWidth 时,您都在添加另一个事件处理程序。这不会取代之前的事件句柄 r 而是堆叠在它上面,这样在滚动几次之后,你就会被同一个事件处理程序的大量副本卡住。最终,如果你滚动得足够多,这将导致你的页面变得无响应.

I would separate it like this:我会这样分开它:

function checkWidth(){
    if(window.innerWidth <= 600){
        var scrolled = false; 
        if ($(window).scrollTop() > 34 && scrolled == false) {
            $("#balancemtop").slideToggle("slow");
            scrolled = true;
        } else if ($(window).scrollTop() < 34 && scrolled == true) {
            scrolled = false;
            $("#balancemtop").slideToggle("slow");
        }
    } else {
        $("#balancemtop").css("display", "none");
    }
};

$(window).scroll(function() {
    checkWidth();
});

The checkWidth() function does the same, but it doesn't assign the event handler to the window. checkWidth() function 做同样的事情,但它没有将事件处理程序分配给 window。 Instead, you make a single assignment outside of the checkWidth() function for $(window).scroll .相反,您在 checkWidth() function 之外为$(window).scroll进行单个分配。

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

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