jQuery 返回 false 必要但破坏了 .each 代码

发布于 2024-12-03 19:05:10 字数 2236 浏览 1 评论 0原文

我在 iOS Mobile Safari 中的页面遇到问题 - 页面在方向更改时冻结。我的函数在 $(window).resize(); 上执行,这是 Mobile Safari 监听的纵向/横向方向。

在绞尽脑汁并拼命尝试之后,我将 return false; 添加到了函数的 .each(); 部分,瞧!方向改变不再冻结!但是...

现在该函数未针对我定义的元素的 .each(); 运行。

如何让该函数针对所有元素运行,同时保持 return false; 添加的任何行为(这可以防止 Mobile Safari 冻结)?

测试站点:http://brantley.dhut.ch/

谢谢!

JavaScript:

(function($){
$.respond = function(callback) {

    $(document).ready(function() {
        dimensions();
    });

    $(window).load(function() {
        dimensions();
        $('#load').fadeOut('fast', function() {
            $('.z:first').fadeIn('slow');
            $('#status').fadeIn('slow');
            $('footer').fadeIn('slow');
        });
    });

    $(window).resize(function() {
        dimensions();
    });

    function dimensions() {
        var i = $('.z');
        i.each(function() {

            var browserWidth = $(window).width();
            var imgRatio = ($(this).width() / $(this).height());
            var availableHeight = ($(document).height() - $('header').height() - $('#status').height() - $('footer').height() - 80);
            var browserRatio = (browserWidth / availableHeight);

            if (imgRatio >= 1) {
                $(this).addClass('landscape');
            }

            if (browserRatio >=1.5) {
                $('#container').css('min-height', '360px');
            } else {
                $('#container').css('min-height', '555px');
            }

            if (browserRatio >= imgRatio) {
                /* landscape */
                //$('body').css('background', 'blue');
                $(this).height(availableHeight).width('auto').css('margin-top', '0');       
            } else {
                /* portrait */
                //$('body').css('background', 'green');
                $(this).width(browserWidth - 40).height('auto').css('margin-top', (availableHeight - $(this).height())/2);
            }

            $(this).css('margin-left', (browserWidth - $(this).width())/2);

        });

        return false;

    };

};
})(jQuery);

I've been having issues with my page in iOS Mobile Safari- the page freezes on orientation change. My function is excecuted on $(window).resize(); which is what Mobile Safari listens to for portrait/landscape orientation.

After wracking my brain and desperately trying things, I added return false; to the .each(); section of my function and voila! Orientation Change no longer freezes! But...

Now the function isn't run for .each(); of my defined elements.

How can I let the function run for all the elements while maintaining whatever behavior it is the return false; adds (which prevents Mobile Safari from freezing)?

Test site: http://brantley.dhut.ch/

THANK YOU!

JavaScript:

(function($){
$.respond = function(callback) {

    $(document).ready(function() {
        dimensions();
    });

    $(window).load(function() {
        dimensions();
        $('#load').fadeOut('fast', function() {
            $('.z:first').fadeIn('slow');
            $('#status').fadeIn('slow');
            $('footer').fadeIn('slow');
        });
    });

    $(window).resize(function() {
        dimensions();
    });

    function dimensions() {
        var i = $('.z');
        i.each(function() {

            var browserWidth = $(window).width();
            var imgRatio = ($(this).width() / $(this).height());
            var availableHeight = ($(document).height() - $('header').height() - $('#status').height() - $('footer').height() - 80);
            var browserRatio = (browserWidth / availableHeight);

            if (imgRatio >= 1) {
                $(this).addClass('landscape');
            }

            if (browserRatio >=1.5) {
                $('#container').css('min-height', '360px');
            } else {
                $('#container').css('min-height', '555px');
            }

            if (browserRatio >= imgRatio) {
                /* landscape */
                //$('body').css('background', 'blue');
                $(this).height(availableHeight).width('auto').css('margin-top', '0');       
            } else {
                /* portrait */
                //$('body').css('background', 'green');
                $(this).width(browserWidth - 40).height('auto').css('margin-top', (availableHeight - $(this).height())/2);
            }

            $(this).css('margin-left', (browserWidth - $(this).width())/2);

        });

        return false;

    };

};
})(jQuery);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-12-10 19:05:10

尝试这个优化的代码:

(function($){
    $.respond = function(callback) {
        var $doc = $(document),
            $win = $(window),
            $z, $load, $footer, $header, $status, $container,
            resizing = false;

        $doc.ready(function() {
            $z          = $('.z');
            $load       = $('#load');
            $footer     = $('footer');
            $header     = $('header');
            $status     = $('#status');
            $container  = $('#container');

            dimensions();
        });

        $win.load(function() {
            dimensions();
            $load.fadeOut('fast', function() {
                $z.filter(':first').fadeIn('slow');
                $status.fadeIn('slow');
                $footer.fadeIn('slow');
            });
        });

        $win.resize(function() {
            dimensions();
        });

        function dimensions() {
            if(resizing){
                return; // break; the function is still running
            } else{
                resizing = true;
            } 

            var browserWidth    = $win.width(),
                imgRatio        = $this.width() / $this.height(),
                availableHeight = ($doc.height() - $header.height() - $status.height() - $footer.height() - 80),
                browserRatio    = browserWidth / availableHeight;

            $z.each(function() {
                var $this = $(this);
                if (imgRatio >= 1) {
                    $this.addClass('landscape');
                }

                if (browserRatio >=1.5) {
                    $container.css('min-height', '360px');
                } else {
                    $container.css('min-height', '555px');
                }

                if (browserRatio >= imgRatio) {
                    /* landscape */
                    //$('body').css('background', 'blue');
                    $this
                        .height(availableHeight)
                        .width('auto')
                        .css('margin-top', '0');
                } else {
                    /* portrait */
                    //$('body').css('background', 'green');
                    $this
                        .width(browserWidth - 40)
                        .height('auto')
                        .css('margin-top', (availableHeight - $this.height())/2);
                }

                $this.css('margin-left', (browserWidth - $this.width())/2);

            });

            resizing = false;
        };
    };
})(jQuery);

Try this optimized code:

(function($){
    $.respond = function(callback) {
        var $doc = $(document),
            $win = $(window),
            $z, $load, $footer, $header, $status, $container,
            resizing = false;

        $doc.ready(function() {
            $z          = $('.z');
            $load       = $('#load');
            $footer     = $('footer');
            $header     = $('header');
            $status     = $('#status');
            $container  = $('#container');

            dimensions();
        });

        $win.load(function() {
            dimensions();
            $load.fadeOut('fast', function() {
                $z.filter(':first').fadeIn('slow');
                $status.fadeIn('slow');
                $footer.fadeIn('slow');
            });
        });

        $win.resize(function() {
            dimensions();
        });

        function dimensions() {
            if(resizing){
                return; // break; the function is still running
            } else{
                resizing = true;
            } 

            var browserWidth    = $win.width(),
                imgRatio        = $this.width() / $this.height(),
                availableHeight = ($doc.height() - $header.height() - $status.height() - $footer.height() - 80),
                browserRatio    = browserWidth / availableHeight;

            $z.each(function() {
                var $this = $(this);
                if (imgRatio >= 1) {
                    $this.addClass('landscape');
                }

                if (browserRatio >=1.5) {
                    $container.css('min-height', '360px');
                } else {
                    $container.css('min-height', '555px');
                }

                if (browserRatio >= imgRatio) {
                    /* landscape */
                    //$('body').css('background', 'blue');
                    $this
                        .height(availableHeight)
                        .width('auto')
                        .css('margin-top', '0');
                } else {
                    /* portrait */
                    //$('body').css('background', 'green');
                    $this
                        .width(browserWidth - 40)
                        .height('auto')
                        .css('margin-top', (availableHeight - $this.height())/2);
                }

                $this.css('margin-left', (browserWidth - $this.width())/2);

            });

            resizing = false;
        };
    };
})(jQuery);
忆梦 2024-12-10 19:05:10

return false; 不在 .each() 中,而是在事件处理程序本身中,这意味着您要取消任何待处理的事件。

我假设您的函数导致另一个 resize 事件发生,从而导致无限循环。

通过运行一次处理程序并使用全局标志来优化代码以了解您来自该函数并且它应该可以工作:

第一部分:

var _resizeTimer = 0;
$(window).resize(function() {
    window.clearTimeout(_resizeTimer);
    _resizeTimer = window.setTimeout(dimensions, 200);
});

第二部分:

var _insideDimensions = false;
function dimensions() {
    if (_insideDimensions)
        return;
    var i = $('.z');
    _insideDimensions = true;
    i.each(function() {
        //.....
    });
    _insideDimensions = false;
}

The return false; is not in the .each() but rather in the event handler itself, meaning you're cancelling any pending events.

I assume that your function is causing another resize event to happen, thus causing endless loop.

Optimize the code by running the handler once plus use global flag to know you came from the function and it should work:

First part:

var _resizeTimer = 0;
$(window).resize(function() {
    window.clearTimeout(_resizeTimer);
    _resizeTimer = window.setTimeout(dimensions, 200);
});

Second part:

var _insideDimensions = false;
function dimensions() {
    if (_insideDimensions)
        return;
    var i = $('.z');
    _insideDimensions = true;
    i.each(function() {
        //.....
    });
    _insideDimensions = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文