gpt4 book ai didi

javascript - 航点.js : Add class when N distance to top

转载 作者:行者123 更新时间:2023-11-28 07:56:09 25 4
gpt4 key购买 nike

我不知道这是否容易,但我只是无法用 Waypoint.js 弄清楚一些东西当用户达到例如 100px 的滚动时,我需要向 #stickytop 添加一个类知道如何做到这一点吗?偏移对我不起作用。提前致谢

最佳答案

这并不是 jQuery 路径点的真正用例。它旨在测试特定元素距屏幕顶部的距离,而不是用户滚动的距离。

但是您可以使用 jQuery 轻松地完成您想要的操作:

$(document).on('scroll', function () {
// if the scroll distance is greater than 100px
if ($(window).scrollTop() > 100) {
// do something
$('#stickytop').addClass('myClass');
}
});

或者普通的 JavaScript:

document.addEventListener('scroll', function () { 
if (window.scrollY > 100) {
var el = document.getElementById('#stickytop');
el.className = el.className + " myClass";
}
})

关于javascript - 航点.js : Add class when N distance to top,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26063187/

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