Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky "back to top" button showing on page load, before scrolling down

I followed a tutorial to get a sticky "back to top" button that would appear once you scroll down. For some reason it's displaying when you're at the top of the page after the page first loads. If you scroll down, then all the way back up, it disappears (as it should). But initially it isn't behaving properly. Any idea?

Here's the live page I'm using it on, you can see it in the bottom right corner here: http://willryan.us

HTML

<a href="#" class="go-top" style="display: inline;">Back to top</a>


<script>
        $(document).ready(function() {
            // Show or hide the sticky footer button
            $(window).scroll(function() {
                if ($(this).scrollTop() > 200) {
                    $('.go-top').fadeIn(500);
                } else {
                    $('.go-top').fadeOut(300);
                }
            });

            // Animate the scroll to top
            $('.go-top').click(function(event) {
                event.preventDefault();

                $('html, body').animate({scrollTop: 0}, 300);
            })
        });
    </script>

CSS

.go-top {
    position: fixed;
    bottom: 0.75em;
    right: 0.5em;
    text-decoration: none;
    color: white;
    background-color: rgba(0, 0, 0, 0.25);
    font-size: 12px;
    padding: 10px;
    display: none;
    margin: 0;
}

.go-top:hover {
    background-color: rgba(0, 0, 0, 0.6);
    color: white;
    text-decoration: none;
}
like image 605
Will Ryan Avatar asked Mar 12 '14 19:03

Will Ryan


People also ask

How do I scroll back to the top of the page?

Scroll Back To Top link built with Bootstrap 5. Create a button that appears when you start scrolling and on click smooth scrolls you to the top of the page. To learn more read Buttons Docs . When your page is very long you may want to give your users an option to quickly scroll back to the top of the page.

What is a sticky back-to-top button?

What is a sticky back-to-top button? Also known as a scroll-to-top button or go-to-top image, the sticky back-to-top button is a helpful navigation element that helps users get back to the top of the web page they’re viewing.

How do you implement scroll down and scroll up in HTML?

To implement this functionality, we’ll use two helper classes: scroll-up and scroll-down. More specifically: As we scroll down, the body will receive the scroll-down class. As we scroll up, it’ll receive the scroll-up class. If we scroll to the top of the page, it will lose its scroll-up class.

How to hide/reveal a sticky header on scroll (with JavaScript)?

How to Hide/Reveal a Sticky Header on Scroll (With JavaScript) 1 1. Begin With the Page Markup. To start off our sticky header we’ll open the markup with a header which contains a nav. Within it, we’ll put the menu ... 2 2. Add the Sticky Header CSS. 3 3. Add the JavaScript.


3 Answers

Change your HTML from

<a href="#" class="go-top" style="display: inline;">Back to top</a>

to

<a href="#" class="go-top" style="display: none;">Back to top</a>

This will initially hide your button until you scroll.

like image 177
ntgCleaner Avatar answered Sep 22 '22 03:09

ntgCleaner


It's displaying because you haven't fired a scroll event yet to make that logic get run to hide/show it

<script>
    $(document).ready(function() {
        function checkPosition() {
            if ($(this).scrollTop() > 200) {
                $('.go-top').fadeIn(500);
            } else {
                $('.go-top').fadeOut(300);
            }
        }
        // Show or hide the sticky footer button
        $(window).scroll(checkPosition);

        // Animate the scroll to top
        $('.go-top').click(function(event) {
            event.preventDefault();

            $('html, body').animate({scrollTop: 0}, 300);
        })

        checkPosition();
    });
</script>

This new refactor will fire checkPosition at least once on page load, to make sure the button is faded out. An alternative solution would be to set display: none; in the CSS on the element, so it's hidden by default, then only shown by the javascript later

like image 37
Alex Mcp Avatar answered Sep 26 '22 03:09

Alex Mcp


I did as user ntgCleaner said and change the "display:inline" in the html to "display:none" and it seems to work. Thanks!

like image 44
Will Ryan Avatar answered Sep 23 '22 03:09

Will Ryan