javascript 滚动时的javascript调整大小事件 - 移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9361968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript resize event on scroll - mobile
提问by gabitzish
I'm trying to make a website for mobile. I'm having the "resize" event binded on window, wich should rearrange elements when the mobile device is turning (portrait <-> landscape). On iPhone and Samsung Galaxy SII, the event is triggered when I'm scrolling down the page, and that's not good.
我正在尝试为移动设备制作网站。我在窗口上绑定了“调整大小”事件,应该在移动设备转动时重新排列元素(纵向 <-> 横向)。在 iPhone 和三星 Galaxy SII 上,当我向下滚动页面时会触发该事件,这并不好。
Any suggestions?
有什么建议?
采纳答案by scessor
Use the onOrientationChange
event and the window.orientation
property instead.
请改用onOrientationChange
事件和window.orientation
属性。
Also see this answer.
另请参阅此答案。
Here link to a test page.
这里链接到一个测试页面。
回答by sidonaldson
Cache the width of the viewport and on resize return false if the width is still the same.
缓存视口的宽度,如果宽度仍然相同,则在调整大小时返回 false。
A small jQuery snippet:
一个小的 jQuery 片段:
var cachedWidth = $(window).width();
$(window).resize(function(){
var newWidth = $(window).width();
if(newWidth !== cachedWidth){
//DO RESIZE HERE
cachedWidth = newWidth;
}
});