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>
|