JQuery returns to the top and jquery returns to the top.

Source: Internet
Author: User

JQuery returns to the top and jquery returns to the top.

When the content of a page is relatively long, there is usually a "Back to Top" button at a fixed position in the lower right corner of the page. After clicking it, the page's scroll bar gradually rolls back to the top, this article describes an implementation process.
First, a button is required.

<Button id = "btn_top" title = "Back to Top"> Back to Top </button>

Then locate this element to the bottom right corner. We use position: fixed. The button below adds some basic styles

#btn_top {    position: fixed;    bottom: 10px;    right: 10px;    display: none;}

At this time, this button is not displayed. We need to display the "Back to Top" button when the page's scroll bar is 50 pixels at the top, which is implemented through JQuery.

$(function () {    $(window).scroll(function () {        if ($(window).scrollTop() >= 50) {            $('#btn_top').fadeIn();        }        else {            $('#btn_top').fadeOut();        }    });});

Finally, we add a click event to the button "Back to Top" and use an animation to roll the scroll bar to the top.

$('#btn_top').click(function () {    $('html,body').animate({ scrollTop: 0 }, 500);});

Okay, you're done!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.