$(window).resize()会触发方向更改吗? [英] Will $(window).resize() fire on orientation change?
问题描述
我在调整浏览器窗口时使用它来运行一些代码: $(window).resize(callback)
Im using this to run some code when a browser window is resized: $(window).resize(callback)
我还需要在手机和平板电脑中更改方向时运行此代码。以上火灾会对此事件有所影响吗?
I also need to run this code when the orientation is changed in phones and tablets. Will the above fire on this event?
推荐答案
有些设备/浏览器有,有些没有。您需要决定支持的浏览器和设备。
Some devices/browsers do, some not. You need to decide your supported browsers and devices.
如果您想要安全,您应该使用resize事件并获取/检查其中的尺寸;如果你知道你想要的设备改变了方向:
If you want to be on secure side you should use the resize event and get/check the sizes inside in it; if you know your desired devices go with a simple orientation change:
简易解决方案:
// Listen for orientation changes
window.addEventListener("orientationchange", function() {
// Announce the new orientation number
alert(window.orientation);
}, false);
更安全/支持
// Listen for resize changes
window.addEventListener("resize", function() {
// Get screen size (inner/outerWidth, inner/outerHeight)
}, false);
David Walsh写了一篇关于调整大小和方向变化事件的好文章。
有关方向更改和尺寸的更多信息,请访问:
http://davidwalsh.name/orientation-change
David Walsh wrote a good article about resize and orientation change event. More about orientation change and sizes here: http://davidwalsh.name/orientation-change
这篇关于$(window).resize()会触发方向更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!