Code for returning results to the top using jquery

Source: Internet
Author: User

In jquery, we only need a few simple statements to return to the top, add a click event to the div, and then bind the scrollto event Method of the document.

In jquery, the core code at the top is

The Code is as follows: Copy code

// Back To Top
$ (Document). ready (function (){
$ ('. Top'). click (function (){
$ (Document). scrollTo (0,500 );
});
});

// Create a link defined with the class. top
<A href = "#" class = "top"> Back To Top </a>

Next we will look at a back-to-top instance


1. Style settings:

The Code is as follows: Copy code

# ToTop {
/* Size of the selected background image */
Width: 54px;
Height: 54px;
Display: none;/* not displayed at the beginning */
Position: fixed;
Right: 25px;/* The size from the right side */
Bottom: 45px;/* size from bottom */
/* Select the image area */
Background-image: url (images/qqbg_1.5.5.png );
Background-repeat: no-repeat;
Background-position:-700px-110px;
Opacity: 0.3;/* Transparency */
}

# ToTop: hover {
Opacity: 1;/* opacity when hovering */
Filter: alpha (opacity = 100 );
}

2. JS code:

The Code is as follows: Copy code

<A href = "#" target = "_ self" id = "toTop" title = "Back to Top" onclick = "window. scrollTo (0, 0); return false "> </a>
<Script type = "text/javascript">
Function toTopHide (){
$ (Document). scrollTop ()> 400?
$ ("# ToTop"). show ()
: $ ("# ToTop"). hide ();
}
$ (Window). scroll (function () {tophide ()});


The main code is: window. scrollTo (0, 0 );
</Script>

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.