JQuery implements the return to top Function

Source: Internet
Author: User

Sort out two implementation functions, one is the return top in the lower right corner, and the other is the return top on the right, respectively

First implementation

I. JSP or HTML (subject structure)

Add in body

<body id="top"><p id="back-to-top"><a href="#top"><span></span></a></p></body>

Ii. CSS (style-based)

<Style> p # back-to-top {position: fixed; bottom: 50px; right: 50px;} p # back-to-top a {text-align: center; text-decoration: none; color: # d1d1d1; display: block; width: 50px; /* use the transition attribute in CSS3 to add the gradient effect to the text in the jump link */-moz-transition: color 1 s;-webkit-transition: color 1 s;-o-transition: color 1 s;} p # back-to-top a: hover {color: #979797;} p # back-to-top a span {background: # d1d1d1 url (/img/back_to_top.png) no-repeat center; border-radius: 6px; display: block; height: 50px; width: 50px; margin-bottom: 5px; /* use the transition attribute in CSS3 to add the gradient effect to the <span> label background color */-moz-transition: background 1 s;-webkit-transition: background 1 s; -o-transition: background 1 s;} # back-to-top a: hover span {background: #979797 url (/img/back_to_top.png) no-repeat center ;} </style>
Image Search for resources online

Iii. jQuery (dynamic effects)

<Script> $ (document ). ready (function () {// first hide # back-to-top $ ("# back-to-top "). hide (); // when the position of the scroll bar is below 100 pixels from the top, the jump link appears, otherwise it disappears $ (function () {$ (window ). scroll (function () {if ($ (window ). scrollTop () & gt; 100) {$ ("# back-to-top "). fadeIn (1500);} else {$ ("# back-to-top "). fadeOut (1500) ;}}); // when you click the jump link, return to the top position of the page $ ("# back-to-top "). click (function () {$ ('body, html '). animate ({scrollTop: 0}, 500); return false ;}); </script>



Second Implementation

<style>.backToTop {display: none;width: 18px;line-height: 1.2;padding: 5px 0;background-color: #000;color: #fff;font-size: 12px;text-align: center;position: fixed;_position: absolute;right: 10px;bottom: 100px;_bottom: "auto";cursor: pointer;opacity: .6;filter: Alpha(opacity = 60);}</style>

<Script> (function () {var $ backToTopTxt = "Back to Top" $ backToTopEle =$ ('<div class = "backToTop"> </div> '). appendTo ($ ("body ")). text ($ backToTopTxt ). attr ("title", $ backToTopTxt ). click (function () {$ ("html, body "). animate ({scrollTop: 0}, 120) ;}) $ backtopfun = function () {var st = $ (document ). scrollTop (), winh = $ (window ). height (); (st> 0 )? $ BackToTopEle. show (): $ backToTopEle. hide (); // locate if (! Window. XMLHttpRequest) {export backtotopele.css ("top", st + winh-166) ;};$ (window ). bind ("scroll", $ backToTopFun); $ (function () {$ backtopfun () ;}) (); </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.