gpt4 book ai didi

javascript - jquery 航路点,在视口(viewport)中心而不是顶部时激活?

转载 作者:搜寻专家 更新时间:2023-11-01 04:47:27 24 4
gpt4 key购买 nike

所有文档都讨论了路点何时到达视口(viewport)的顶部,但我希望在路点的任何部分位于视口(viewport)中心时触发。

如果我向下滚动,这段代码工作得很好,但当我向上滚动时,它显然不起作用。

$('.section').waypoint(function(direction) {
highlight('#' + this.id);
}, {
context: '#scroll',
offset: function (direction) {
return $(this).height();
}
});

我尝试了下面的代码和几个变体,它甚至没有命中任何一个 return 语句。

$('.section').waypoint(function(direction) {
highlight('#' + this.id);
}, {
context: '#scroll',

offset: function (direction) {
if (direction == 'down') {
return -$(this).height();
} else {
return 0;
}
}
});

所以现在我正在尝试这个,基于航点示例,但是 $active.id 不像 this.id 那样工作,所以我的函数“突出显示”失败了。

$('.section').waypoint(function (direction) {
var $active = $(this);
if (direction == 'down') {
$active = $active.prev();
}
if (!$active.length) {
$active = $(this);
}
highlight($active.id);
}, {
context: '#scroll',
offset: function (direction) {
return $(this).height();
}
});

最佳答案

offset 选项不带方向参数。我很想知道您是否从文档中的某个地方得到了它,因为如果有一个部分在 offset 函数中使用了 direction,那么这是一个错误。

您可以通过使用 % 偏移量告诉路点在元素顶部到达视口(viewport)中间时触发:

offset: '50%'

如果向上滚动和向下滚动时需要不同的偏移量,最好通过创建两个不同的航路点来实现:

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

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

$things.waypoint(function(direction) {
if (direction === 'up') {
// do stuff
}
}, {
offset: function() {
// This is the calculation that would give you
// "bottom of element hits middle of window"
return $.waypoints('viewportHeight') / 2 - $(this).outerHeight();
}
});

关于javascript - jquery 航路点,在视口(viewport)中心而不是顶部时激活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593254/

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