拖动调整大小手柄时多次触发调整大小事件
我希望这个 jQuery 插件能够工作,但它没有:
http://andowebsit.es/blog/noteslog.com/post/how-to-fix-the-resize-event-in-ie (旧链接是noteslog.com/post/how-to-fix-the-resize-event-in-ie)。
我在他的网站上添加了一条评论,但它们已经过审核,因此您可能还看不到它。
但无论如何,让我解释一下我的愿望。 我希望当用户暂停调整大小和/或完成调整大小时触发“调整大小”类型的事件,而不是在用户主动拖动浏览器的窗口调整大小手柄时触发。 我有一个相当复杂且耗时的 OnResizeHandled
函数,我需要运行,但不能运行 100 次,只是因为用户将窗口扩大了 100 像素,并且每移动一个像素就会触发该事件。 我想最好的选择是在用户完成调整大小后处理它。
I was hoping this jQuery plug-in would work, but it didn't:
http://andowebsit.es/blog/noteslog.com/post/how-to-fix-the-resize-event-in-ie
(old link was noteslog.com/post/how-to-fix-the-resize-event-in-ie ).
I added a comment to his site, but they're moderated, so you might not see it yet.
But anyhow, let me explain my desire. I want a "resize" type of event to be fired when the user either pauses his resize, and/or completes his resize, not while the user is actively dragging the browser's window resize handle. I have a fairly complex and time consuming OnResizeHandled
function I need to run, but not run 100 times just because the user widened the window by 100px and the event was fired for ever pixel of movement. I guess a best bet would be to handle it once the user has completed the resize.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
像这样的代码怎么样:
这应该确保函数仅在用户停止调整大小时调整大小。
How about some code like this:
That should make sure the function only resizes when the user stops resizing.
借鉴并发解决方案的一些想法来管理来自浏览器的大量事件。
例如,当您第一次收到调整大小事件时,将标志设置为 true 指示用户当前正在调整大小。 设置超时以在 1 秒后调用实际的调整大小事件处理程序。 然后,只要该标志为 true,就忽略调整大小事件。 然后,在实际的处理程序中,一旦一切都完成并正确,将标志设置回 false。
这样,您每秒仅处理一次最新事件(或根据您的要求而定的其他时间段)。 如果用户在调整大小的过程中暂停,它将进行处理。 如果用户完成,它将进行处理。
这可能不适合您,但是还有许多其他使用锁的方法可能会更有帮助。
Borrow some ideas from concurrency solutions to manage the flood of events coming from the browser.
For example, when you first get a resize event, set a flag to true indicating that the user is currently resizing. Set a timeout to call the actual resize event handler after 1 second. Then, whenever this flag is true, ignore the resize event. Then, in the actual handler, once everything is done and correct, set the flag back to false.
That way you only process the latest event once every second (or some other period of time depending on your requirements). If the user pauses in the middle of resizing, it will process. If the user finished, it will process.
This might not be suitable for you, but there are many other ways of using locks that might be more helpful.
Paul Irish 有一个很棒的 Debounced jQuery 插件 可以解决这个问题。
Paul Irish has a great Debounced jQuery plugin that solves this problem.
如果您喜欢图书馆,您应该检查下划线,下划线已经满足您的需求,并且您的项目中可能会有更多。
下面是下划线去抖动器如何工作的示例:
然后只需在调整窗口大小时调用该去抖动器函数
即可检出此处可用的所有下划线函数 http ://underscorejs.org/
If you are into libraries, you should checkout underscore, underscore already handles your need and many more you will probably have in your projects.
here is an example of how underscore debouncer works:
then just call that debouncer function on the resize of the window
checkout all the underscore functions available here http://underscorejs.org/
当用户暂停调整大小时设置超时怎么样? 发生调整大小时重置超时,或者在超时到期时触发调整大小事件处理程序。
How about setting a timeout for when the user pauses the resize? Reset the timeout when resizing occurs, or fire the resize event handler if the timeout expires.
既然它是用 jQuery 构建的,你不能将某种 onComplete 事件绑定到扩展并传递一个在调用时应该执行的函数吗?
编辑:
当用户开始调整窗口大小时,设置一个观察窗口尺寸的间隔。 如果尺寸在您规范的预设时间段内没有发生变化,那么您可以认为窗口大小调整“已完成”。 您选择的 JavaScript 函数是 setInterval(),它接受两个参数 - 一个调用每个刻度的函数,以及每个刻度需要多长时间(以毫秒为单位)。
至于如何在 jquery 框架内专门编写它,嗯,我对 jquery 的了解还不够,无法告诉你。 也许其他人可以更具体地帮助您,但同时您可以考虑扩展您已经与 onComplete 事件链接的 jquery 扩展,该事件的工作原理就像我刚才描述的那样。
Since it's built in jQuery, can't you bind some kind of onComplete event to the extension and pass a function that should be executed when it's called?
Edit:
When the user starts resizing the window, set an interval that watches the window's dimensions. If the dimensions haven't changed in a pre-set time period of your specification, then you can consider the window resizing "completed". Your javascript function of choice in this would be setInterval(), which accepts two arguments - a function to call each tick, and how long each tick takes in milliseconds.
As to how to write that specifically within the jquery framework, well, I don't know enough about jquery to be able to tell you that. Perhaps someone else can help you more specifically, but at the same time you may consider extending the jquery extension you already linked with an onComplete event that works like I just described.
几天前我扩展了默认的 jQuery
on
-event-handler。 如果在给定的时间间隔内未触发事件,它将一个或多个事件的事件处理程序函数附加到所选元素。 如果您只想在延迟后触发回调(例如调整大小事件),这非常有用。https://github.com/yckart/jquery.unevent.js
像任何东西一样使用它其他
on
或bind
- 事件处理程序,除了您可以传递一个额外的参数作为最后一个:http://jsfiddle.net/ARTsinn/EqqHx/
I extended some days ago the default jQuery
on
-event-handler. It attach an event handler function for one or more events to the selected elements if the event was not triggered for a given interval. This is useful if you want to fire a callback only after a delay, like the resize event, or else.https://github.com/yckart/jquery.unevent.js
Use it like any other
on
orbind
-event handler, except that you can pass an extra parameter as a last:http://jsfiddle.net/ARTsinn/EqqHx/