简体   繁体   中英

button to scroll all the way to the top with click of button

i have this javascript so that when a user is scrolling on the page there will be a small icon to the side that will scroll all the way back up the page rather than manually scrolling. The button shows fine but when i click on it it is not going all the way to the top.

html

<a href="#" class="scrollup">Scroll</a>

Script

$(document).ready(function () {

        $('#main').scroll(function () {
            if ($(this).scrollTop() > 100) {
                $('.scrollup').fadeIn();
            } else {
                $('.scrollup').fadeOut();
            }
        });

        $('.scrollup').click(function () {
            $("html, body, main_container, main").animate({ scrollTop: 0 }, 600);
            return false;
        });

    });

problem is in the selectors, you are missing either # id selector or . class selector, to me it seems id:

change this:

$("html, body, main_container, main")

to this and see if it helps:

$("html, body, #main_container, #main")
//-------------^----------------^--------these selector notations

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