简体   繁体   中英

Svelte - hide and show nav on scroll

I want the nav to hide scrolling down 60px and to show when scrolling up 60px, no matter in which part of the page.

I did this, but it's incomplete, what am I missing?

<script>
 let y = 0;
</script>

<svelte:window bind:scrollY="{y}" />

<nav class:hideNav={y > 60}>
 <ul>
  <li>link</li>
 </ul>
</nav>

<style>
nav {
  position: fixed;
  top: 0;
}

.hideNav {
  top: -70px;
}
</style>

Your code seems to perfectly hide the navbar after you scroll the specified amount, here is a REPL of your code in action. maybe the body of your content has no scroll?

here is another implementation REPL that further elaborates how to use scrolling position

<script>
    import {onMount, onDestroy} from 'svelte'
    const scrollNavBar = 60
    let show = false
    onMount(() => {
        window.onscroll = () => {
            if (window.scrollY > scrollNavBar) {
                show = true
            } else {
                show = false
            }
        }
    })
    
    onDestroy(() => {
        window.onscroll = () => {}
    })
</script>

<style>

    .scrolled {
        
        transform: translate(0,calc(-100% - 1rem))
    }
    
    nav {
        width: 100%;
        position: fixed;
        box-shadow: 0 -0.4rem 0.9rem 0.2rem rgb(0 0 0 / 50%);
        padding: 10px;
        transition: 0.5s ease
        
    }
    :global(body) {
        margin: 0;
        padding: 0;
        height: 200vh;
    }
</style>

<nav class:scrolled={show}>
    elemnt
</nav>

In your REPL, it seems like the nav does not reappear on scrolling up. It does appear only at the top of the page.

I am also trying to show the nav when the user scrolls up by 30px anywhere on the page, for instance. I think that it was what OP is asking as well.

I found a REPL successfully doing it with jQuery but I am struggling to make it work in Svelte at the moment. Any clue?

I will revert back if I succeed.

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();

$(window).scroll(function(event){
    didScroll = true;
});

setInterval(function() {
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
}, 250);

function hasScrolled() {
    var st = $(this).scrollTop();
    
    // Make sure they scroll more than delta
    if(Math.abs(lastScrollTop - st) <= delta)
        return;
    
    // If they scrolled down and are past the navbar, add class .nav-up.
    // This is necessary so you never see what is "behind" the navbar.
    if (st > lastScrollTop && st > navbarHeight){
        // Scroll Down
        $('header').removeClass('nav-down').addClass('nav-up');
    } else {
        // Scroll Up
        if(st + $(window).height() < $(document).height()) {
            $('header').removeClass('nav-up').addClass('nav-down');
        }
    }
    
    lastScrollTop = st;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM