javascript jQuery Waypoints 触发一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16002273/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:53:22  来源:igfitidea点击:

jQuery Waypoints Fire Once

javascriptjqueryjquery-waypoints

提问by user1214467

I am using http://imakewebthings.com/jquery-waypointsand I need to do some action when the user scrolls down to the area with the class div1. However, I need it only fire once and not every time the user scrolls to that location. — only once

我正在使用http://imakewebthings.com/jquery-waypoints,当用户向下滚动到带有 class 的区域时,我需要执行一些操作div1。但是,我只需要触发一次,而不是每次用户滚动到该位置时触发。 ——只有一次

$('.div1').waypoint(function(direction) 
{
    alert(CARRY OUT MY ACTION);
});

This needs to only happen on the first scroll to that section — up or down.

这只需要在第一次滚动到该部分时发生——向上或向下。

回答by Jerome Braeken

triggerOnce()is replaced with destroy(). Just add this.destroy().

triggerOnce()替换为destroy().只需添加this.destroy().

$('.div1').waypoint(function(direction){
    alert('CARRY OUT MY ACTION')
    this.destroy()
});

For more options check the API of Waypoints.

有关更多选项,请查看WaypointsAPI

回答by George Hodgson

If you pass a second parameter to the waypoint()function, you can include an object of configuration options. Setting the triggerOnceoption to true will make the plugin behave the way you'd like.

如果将第二个参数传递给waypoint()函数,则可以包含配置选项的对象。将该triggerOnce选项设置为 true 将使插件按照您希望的方式运行。

$('.div1').waypoint(function(direction) 
{
    alert('CARRY OUT MY ACTION');
},  
{ 
    triggerOnce: true 
});

回答by Seer

In the new API, it seems that there is no triggerOnce option anymore, but still can be used the waypoint.disable()method after the first call

在新的API中,似乎不再有triggerOnce选项了,但waypoint.disable()第一次调用后仍然可以使用该方法

回答by Marc

The answer is to use this.destroy()at the end of your handler function. Here is an example that will work:

答案是this.destroy()在处理程序函数的末尾使用。这是一个可以工作的示例:

$('.div1').waypoint(function(direction){

    handler: function(direction) {

        alert('CARRY OUT MY ACTION');

        this.destroy();

    }

});

Also see the waypoint.destroy()documentation.

另请参阅waypoint.destroy()文档。