1

我在一个页面上有多个部分,ID 类似ss-section-story ss-section-problem ss-section-solution,我试图跟踪每个部分何时进入和退出视口。问题是,使用当前代码,我只使用 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

4

1 回答 1

0

首先,您可以进行收藏:

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');
      }
   })
})

就这样。至少它对我有用。

于 2018-04-20T16:35:40.410 回答