javascript - 自定义航点函数错误

标签 javascript jquery jquery-waypoints

您好,我正在编写一个自定义航点函数,但遇到错误,想知道是否有人可以帮助我。该功能正在使用 waypoints.js

http://imakewebthings.com/waypoints/

这是我到目前为止的代码

var waypoint = function(triggerElement, animatedElement, className, offsetVal) {
    element: document.getElementById(triggerElement);
    handler: function(direction) {
        if (direction === 'down') {
            animatedElement.addClass(className);
            this.destroy();
        }
        else {

        }
    };
    offset: offsetVal;
};

//Trigger Elements
var section2 = jQuery('#section-2');

//Waypoint Instances
waypoint(section2, "section-2-orange-dot", "section-2-orange-dot-active", 500);

我在第三行遇到错误

处理程序:函数(方向){

“未捕获的语法错误:意外的标记(”

谢谢!

最佳答案

您需要使用逗号来分隔函数参数,而不是分号。您还将选定的 jQuery 对象作为triggerElement 传递,因此您不需要 getElementById:

function waypoint (triggerElement, animatedElement, className, offsetVal)
{
    return new Waypoint({ 
        element: triggerElement,
        handler: function(direction) {
            if (direction === 'down') {
                animatedElement.addClass(className);
                this.destroy();
            }
        },
        offset: offsetVal
    });
}

关于javascript - 自定义航点函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44550282/

相关文章:

javascript - 我可以从事件处理程序中更改全局变量吗?

javascript - Sticky Nav 使用 Waypoints 调整大小问题

jquery - 为什么 (jQuery) Waypoints "bottom-in-view"不适用于隐藏元素?

javascript - 转义 meteor react 中的html标签

php - 如何用 php 数组替换 javascript 数组?

javascript - 检查类(class)内部或外部的选择

php - 单击元素时切换 CSS 样式

javascript - 使 jQuery Waypoints 无法与 JavaScript 创建的内容很好地配合

javascript - 引导模式内的视频事件

javascript - 从多维数组中的多行中分离最常见元素的高效算法