gpt4 book ai didi

javascript - 路点和 Inview,使用通配符选择器跟踪多个元素

转载 作者:行者123 更新时间:2023-11-28 04:58:22 26 4
gpt4 key购买 nike

我在一个页面上有多个部分,其 id 类似于 ss-section-story ss-section-problem ss-section-solution 和我试图跟踪每个部分何时进入和退出视口(viewport)。问题是,在我当前的代码中,它仅跟踪 id 为 ss-section 的第一部分。我希望能够跟踪进入和退出的每个部分。

JS

var inview = new Waypoint.Inview({
element: $('section[id^="ss-section"]')[0],
enter: function(direction) {
console.log(this.element.id + ' enter');
},
entered: function(direction) {
console.log(this.element.id + ' entered');
},
exit: function(direction) {
console.log(this.element.id + ' exit');
},
exited: function(direction) {
console.log(this.element.id + ' exited');
}
})

我知道它必须只跟踪第一部分,因为在 element 选项上它被限制为该数组中的 [0] 但当我删除它时,我会这些 console.log 语句上未定义。

我希望能够做到类似但从文档来看,它没有这样的语法:

$('section[id^="ss-section"]').Waypoint.Inview({
enter: function(direction) {
console.log(this.element.id + ' enter');
},
entered: function(direction) {
console.log(this.element.id + ' entered');
},
exit: function(direction) {
console.log(this.element.id + ' exit');
},
exited: function(direction) {
console.log(this.element.id + ' exited');
}
})

有关如何仅使用 Waypoint.Inview 的一个实例来跟踪每个单独部分的任何建议

最佳答案

首先,你可以进行收藏:

var $collection = $('section[id^="ss-section"]');

然后你对其进行迭代:

$collection.each(function () {
new Waypoint.Inview({
element: this,
enter: function(direction) {
console.log(this.element.id + ' enter');
},
entered: function(direction) {
console.log(this.element.id + ' entered');
},
exit: function(direction) {
console.log(this.element.id + ' exit');
},
exited: function(direction) {
console.log(this.element.id + ' exited');
}
})
})

仅此而已。至少对我有用。

关于javascript - 路点和 Inview,使用通配符选择器跟踪多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com