gpt4 book ai didi

javascript - 如何在 100vh 滚动后切换类

转载 作者:可可西里 更新时间:2023-11-01 02:04:10 25 4
gpt4 key购买 nike

如何让这个函数在滚动100vh后添加类?
目前它在 850px 之后添加类。

$("document").ready(function($){
var nav = $('#verschwinden');

$(window).scroll(function () {
if ($(this).scrollTop() > 850) {
nav.addClass("doch");
} else {
nav.removeClass("doch");
}
});
});

最佳答案

100vh 在 jQuery 中很简单,如 $(window).height() 而在纯 JavaScript 中是 window.innerHeight or a bit more longer .

jsFiddle demo

jQuery(function($) {

var $nav = $('#verschwinden');
var $win = $(window);
var winH = $win.height(); // Get the window height.

$win.on("scroll", function () {
if ($(this).scrollTop() > winH ) {
$nav.addClass("doch");
} else {
$nav.removeClass("doch");
}
}).on("resize", function(){ // If the user resizes the window
winH = $(this).height(); // you'll need the new height value
});

});

您还可以通过简单地使用以下命令使 if 部分更短一些:

$nav.toggleClass("doch", $(this).scrollTop() > winH );

demo

关于javascript - 如何在 100vh 滚动后切换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26729776/

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