gpt4 book ai didi

javascript - 向下滚动然后向上滚动时如何隐藏div

转载 作者:数据小太阳 更新时间:2023-10-29 04:33:31 24 4
gpt4 key购买 nike

我想像图片一样显示和隐藏菜单。

您可以在下图中看到有一个树部分。第一部分,当您打开页面时,右下角的菜单仍会显示。

向下滚动时菜单淡入,向上滚动时菜单淡出。

facebook 和 tumblr 就是这样做的。我想了解他们如何做到这一点。任何人都可以告诉我一些小例子。

我创建了这个 DEMO 来自 codepen.io 但它只是 header think 并且向上滚动时也存在问题。

enter image description here

var previousScroll = 0, // previous scroll position
menuOffset = 54, // height of menu (once scroll passed it, menu is hidden)
detachPoint = 650, // point of detach (after scroll passed it, menu is fixed)
hideShowOffset = 6; // scrolling value after which triggers hide/show menu
// on scroll hide/show menu
$(window).scroll(function() {
if (!$('nav').hasClass('expanded')) {
var currentScroll = $(this).scrollTop(), // gets current scroll position
scrollDifference = Math.abs(currentScroll - previousScroll); // calculates how fast user is scrolling
// if scrolled past menu
if (currentScroll > menuOffset) {
// if scrolled past detach point add class to fix menu
if (currentScroll > detachPoint) {
if (!$('nav').hasClass('detached'))
$('nav').addClass('detached');
}
// if scrolling faster than hideShowOffset hide/show menu
if (scrollDifference >= hideShowOffset) {
if (currentScroll > previousScroll) {
// scrolling down; hide menu
if (!$('nav').hasClass('invisible'))
$('nav').addClass('invisible');
} else {
// scrolling up; show menu
if ($('nav').hasClass('invisible'))
$('nav').removeClass('invisible');
}
}
} else {
// only remove “detached” class if user is at the top of document (menu jump fix)
if (currentScroll <= 0){
$('nav').removeClass();
}
}
// if user is at the bottom of document show menu
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
$('nav').removeClass('invisible');
}
// replace previous scroll position with new one
previousScroll = currentScroll;
}
})
// shows/hides navigation’s popover if class "expanded"
$('nav').on('click touchstart', function(event) {
showHideNav();
event.preventDefault();
})
// clicking anywhere inside navigation or heading won’t close navigation’s popover
$('#navigation').on('click touchstart', function(event){
event.stopPropagation();
})
// checks if navigation’s popover is shown
function showHideNav() {
if ($('nav').hasClass('expanded')) {
hideNav();
} else {
showNav();
}
}
// shows the navigation’s popover
function showNav() {
$('nav').removeClass('invisible').addClass('expanded');
$('#container').addClass('blurred');
window.setTimeout(function(){$('body').addClass('no_scroll');}, 200); // Firefox hack. Hides scrollbar as soon as menu animation is done
$('#navigation a').attr('tabindex', ''); // links inside navigation should be TAB selectable
}
// hides the navigation’s popover
function hideNav() {
$('#container').removeClass('blurred');
window.setTimeout(function(){$('body').removeClass();}, 10); // allow animations to start before removing class (Firefox)
$('nav').removeClass('expanded');
$('#navigation a').attr('tabindex', '-1'); // links inside hidden navigation should not be TAB selectable
$('.icon').blur(); // deselect icon when navigation is hidden
}
// keyboard shortcuts
$('body').keydown(function(e) {
// menu accessible via TAB as well
if ($("nav .icon").is(":focus")) {
// if ENTER/SPACE show/hide menu
if (e.keyCode === 13 || e.keyCode === 32) {
showHideNav();
e.preventDefault();
}
}
// if ESC show/hide menu
if (e.keyCode === 27 || e.keyCode === 77) {
showHideNav();
e.preventDefault();
}
})

最佳答案

您可能正在寻找这样的东西?每当您滚动时,它都会检查您滚动了多远以及从之前的滚动位置向哪个方向滚动。

var previousScroll = 0;
$(window).scroll(function(event){
var scroll = $(this).scrollTop();
if (scroll > previousScroll){
// downscroll code
} else {
// upscroll code
}
previousScroll = scroll;
});

这里有一些免费的 JSFuddle,对该脚本进行了一些修改和实时 Action 应用:https://jsfiddle.net/d00h1zmn/4/

关于javascript - 向下滚动然后向上滚动时如何隐藏div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29558930/

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