jQuery 返回 false 必要但破坏了 .each 代码
我在 iOS Mobile Safari 中的页面遇到问题 - 页面在方向更改时冻结。我的函数在 $(window).resize();
上执行,这是 Mobile Safari 监听的纵向/横向方向。
在绞尽脑汁并拼命尝试之后,我将 return false;
添加到了函数的 .each();
部分,瞧!方向改变不再冻结!但是...
现在该函数未针对我定义的元素的 .each();
运行。
如何让该函数针对所有元素运行,同时保持 return false;
添加的任何行为(这可以防止 Mobile Safari 冻结)?
谢谢!
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个优化的代码:
Try this optimized code:
return false;
不在.each()
中,而是在事件处理程序本身中,这意味着您要取消任何待处理的事件。我假设您的函数导致另一个
resize
事件发生,从而导致无限循环。通过运行一次处理程序并使用全局标志来优化代码以了解您来自该函数并且它应该可以工作:
第一部分:
第二部分:
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:
Second part: