问题
I recently upgraded jquery-waypoints from 2.x to 3.x in my project and found that a lot of my code broke. All references to $(this)
within my handlers had to be changed to $(this.element)
which, once I figured out was the cause of my troubles, was easy enough to fix.
I haven't been able to figure out what happened to the triggerOnce
option which would prevent a waypoint from firing multiple times. Any idea why this was removed and how can I achieve the same functionality?
回答1:
I know an answer was already marked as correct, but I'd like to expand beyond a simple comment.
3.0, being a major version, made breaking changes. One of them was the removal of triggerOnce
. It is noted in the changelog. It is also mentioned in the destroy docs where using destroy
at the end of the handler is called out as an alternative to the old triggerOnce
.
Previously, triggerOnce
wasn't the exact same as calling destroy
because all Waypoint methods were called on jQuery objects. The elements in those jQuery objects may have multiple waypoints attached to them, but there was no way to separate them once they were created. If you called destroy
all waypoints on that element were destroyed. The triggerOnce
option, however, worked on an individual waypoint basis behind the scenes. Now that 3.0 returns instances of the Waypoint class directly and this
within the handler is a reference to the Waypoint instance instead of the element, there is no difference between triggerOnce
and calling this.destroy()
to end a handler. So the code was removed.
回答2:
I figured out a solution. Adding this.disable() to the end of my handler disables the waypoint after it's been fired, preventing it from being called again. I really think this should be documented.
回答3:
If you need to disable a jQuery waypoint (v3.0):
$('.my-waypoint').waypoint(function(direction){
// Do some stuff
this.destroy();
});
来源:https://stackoverflow.com/questions/27629472/what-happened-to-the-triggeronce-option-in-waypoints-3-0