跨浏览器窗口调整大小事件 - JavaScript / jQuery
在 Firefox 中使用窗口调整大小事件的正确(现代)方法是什么,WebKit和 Internet Explorer?
您可以打开/关闭两个滚动条吗?
What is the correct (modern) method for tapping into the window resize event that works in Firefox, WebKit, and Internet Explorer?
And can you turn both scrollbars on/off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
jQuery 有一个内置方法:
为了 UI 响应能力,您可以考虑使用setTimeout 仅在几毫秒后调用您的代码,如以下示例所示,灵感来自 这个:
jQuery has a built-in method for this:
For the sake of UI responsiveness, you might consider using a setTimeout to call your code only after some number of milliseconds, as shown in the following example, inspired by this:
以下是利用调整大小事件的非 jQuery 方法:
它适用于所有现代浏览器。 它不会为您限制任何内容。 这是一个实际的示例。
Here is the non-jQuery way of tapping into the resize event:
It works on all modern browsers. It does not throttle anything for you. Here is an example of it in action.
很抱歉提出一个旧线程,但如果有人不想使用 jQuery,你可以使用这个:
Sorry to bring up an old thread, but if someone doesn't want to use jQuery you can use this:
由于您对 jQuery 持开放态度,
Since you are open to jQuery, this plugin seems to do the trick.
使用 jQuery 1.9.1 我刚刚发现,虽然技术上相同)*,但这在 IE10 中不起作用(但在 Firefox 中):
而这在两种浏览器中都有效:
编辑:
)* 实际上在技术上不相同,正如 WraithKenny 和 亨利·布莱斯。
Using jQuery 1.9.1 I just found out that, although technically identical)*, this did not work in IE10 (but in Firefox):
while this worked in both browsers:
Edit:
)* Actually not technically identical, as noted and explained in the comments by WraithKenny and Henry Blyth.
jQuery
默认提供$(window).resize()
函数:jQuery
provides$(window).resize()
function by default:我认为 jQuery 插件“jQuery resize event”是对此的最佳解决方案,因为它负责限制事件,以便它在所有浏览器中都相同。 它与安德鲁斯答案类似,但更好,因为您可以将调整大小事件挂接到特定元素/选择器以及整个窗口。 它为编写干净的代码开辟了新的可能性。
该插件位于此处
如果添加大量侦听器,则会出现性能问题,但对于大多数使用情况来说,它是完美的。
I consider the jQuery plugin "jQuery resize event" to be the best solution for this as it takes care of throttling the event so that it works the same across all browsers. It's similar to Andrews answer but better since you can hook the resize event to specific elements/selectors as well as the entire window. It opens up new possibilities to write clean code.
The plugin is available here
There are performance issues if you add a lot of listeners, but for most usage cases it's perfect.
我认为你应该对此添加进一步的控制:
I think you should add further control to this:
希望它对 jQuery 有所帮助
首先定义一个函数,如果有现有函数,请跳到下一步。
浏览器调整大小使用如下。
hope it will help in jQuery
define a function first, if there is an existing function skip to next step.
browser resize use like these.
除了提到的窗口调整大小函数之外,重要的是要了解,如果在没有消抖事件的情况下使用调整大小事件,则会触发很多事件。
Paul Irish 有一个出色的功能,可以大量消除调整大小调用。 非常推荐使用。 跨浏览器工作。 前几天在IE8下测试了一下,一切正常。
http://www.paulirish.com/2009/throttled-smartresize- jquery-event-handler/
请务必查看演示以了解差异。
这是为了完整性而设计的函数。
Besides the window resize functions mentioned it is important to understand that the resize events fire a lot if used without a deboucing the events.
Paul Irish has an excellent function that debounces the resize calls a great deal. Very recommended to use. Works cross-browser. Tested it in IE8 the other day and all was fine.
http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler/
Make sure to check out the demo to see the difference.
Here is the function for completeness.