你好,我自定义了一个2行的Bootstrap导航栏(上面是logo和社交链接,下面是导航链接)。我试图隐藏滚动时上半部分,但我找不到一种方法来顺利做到这一点。我认为下面的代码是我找到的最好的解决方案,但目前,transitionend上的eventlistener不起作用,也没有添加“隐藏”类。
var scrollpos = window.scrollY;
var header = document.getElementById("header-up-section");
function add_class_on_scroll() {
header.classList.add('visuallyhidden');
header.addEventListener('transitionend', function(e) {
header.classList.add('hidden');
}, {
capture: false,
once: true,
passive: false
});
}
function remove_class_on_scroll() {
header.classList.remove('hidden');
setTimeout(function () {
header.classList.remove('visuallyhidden');
}, 20);
}
window.addEventListener('scroll', function(){
scrollpos = window.scrollY;
if(scrollpos > 20){
add_class_on_scroll();
}
else {
remove_class_on_scroll();
}
console.log(scrollpos);
});
*/and CSS :
#header-up-section.visuallyhidden {
opacity: 0;
}
#header-up-section.hidden {
display: none !important;
}header-up部分变为不可见,但div并未隐藏。有什么好的办法吗?
发布于 2020-05-10 18:16:18
最后,JS不需要这样做。只需要两个bootstrap导航栏,并在第二个导航栏(这是一个两行导航栏的伪‘向下部分’)中添加一个bootstrap类'sticky-top‘。它完美地完成了这项工作;)
https://stackoverflow.com/questions/61274577
复制相似问题