如何使此功能在滚动后添加类 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
或者更多 长。
jsFiddle演示
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 );
演示