jquery 航点“向上”事件的不同偏移量

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

different offset for jquery waypoint "up" event

jqueryjquery-waypoints

提问by Softlion

i'll love to have 2 offsets in jquery waypoint. Currently there is only one, the same, for up and down scrolling.

我会喜欢在 jquery 航点中有 2 个偏移量。目前只有一个,相同的,用于向上和向下滚动。

I'm using a "down" offset of 25%, and would like an "up" offset of "75%". So when the top of a block is at 25% of the top of the viewport and the scolling is goin down, 'down' is triggered. And when the top of a block is at 75% of the top of the viewport and the scolling is goin up, 'up' is triggered.

我正在使用 25% 的“向下”偏移,并且想要“75%”的“向上”偏移。因此,当块的顶部位于视口顶部的 25% 并且向下滚动时,将触发“向下”。当块的顶部位于视口顶部的 75% 并且滚动向上时,“向上”被触发。

Anyone has already writen code for this hysteresis?

有人已经为此滞后编写了代码吗?

回答by imakewebthings

You can do this by creating two waypoints, each with different offsets, each only responding to one direction:

您可以通过创建两个航点来实现这一点,每个航点都有不同的偏移量,每个只响应一个方向:

$('.thing').waypoint(function(direction) {
  if (direction === 'down') {
    // Do stuff
  }
}, {
  offset: '25%'
}).waypoint(function(direction) {
  if (direction === 'up') {
    // Do stuff
  }
}, {
  offset: '75%'
});

Update: If you're using the jQuery build of Waypoints 3.0, the above code will not work because waypointno longer chains the jQuery Object. It instead returns an array of the Waypoint instances created. If you're not interested in keeping that array reference around, the code would look like this:

更新:如果您使用的是 Waypoints 3.0 的 jQuery 版本,上面的代码将不起作用,因为waypoint不再链接 jQuery 对象。相反,它返回创建的 Waypoint 实例的数组。如果您对保留该数组引用不感兴趣,代码将如下所示:

var $things = $('.thing');

$things.waypoint(function(direction) {
  if (direction === 'down') {
    // Do stuff
  }
}, {
  offset: '25%'
});

$things.waypoint(function(direction) {
  if (direction === 'up') {
    // Do stuff
  }
}, {
  offset: '75%'
});