Jquery returns to the top source code for sharing-jquery returns to the top source code

Source: Internet
Author: User

Jquery returns to the top source code for sharing-jquery returns to the top source code

// JavaScript Document (function ($) {var goToTopTime; $. fn. goToTop = function (options) {var opts = $. extend ({}, $. fn. goToTop. def, options); var $ window =$ (window); $ body = (window. opera )? (Document. compatMode = "CSS1Compat "? $ ('Html '): $ ('body'): $ ('html, body'); // opera fix $ (this ). hide (); var $ this = $ (this); clearTimeout (goToTopTime); goToTopTime = setTimeout (function () {var controlLeft; if ($ window. width ()> opts. pageHeightJg * 2 + opts. pageWidth) {controlLeft = ($ window. width ()-opts. pageWidth)/2 + opts. pageWidth + opts. pageWidthJg;} else {controlLeft = $ window. width ()-opts. pageWidthJg-$ this. width ();} var cs Sfixedsupport = $. browser. msie & parseFloat ($. browser. version) <7; // determines whether ie6 var controlTop = $ window. height ()-$ this. height ()-opts. pageHeightJg; controlTop = cssfixedsupport? $ Window. scrollTop () + controlTop: controlTop; var shouldvisible = ($ window. scrollTop ()> = opts. startline )? True: false; if (shouldvisible) {$ this. stop (). show (opts. showBtntime);} else {$ this. stop (). hide (opts. showBtntime);} Then this.css ({position: cssfixedsupport? 'Absolute ': 'fixed', top: controlTop, left: controlLeft}) ;}, 500); $ (this ). click (function (event) {$ body. stop (). animate ({scrollTop: entries (opts.tar getObg ). offset (). top}, opts. duration); $ (this ). blur (); event. preventDefault (); event. stopPropagation () ;}) ;}; $. fn. goToTop. def = {pageWidth: 950, // page width pageWidthJg: 10, // The interval between the button and the page is: 50, // The interval between the button and the page bottom is startline: 20, // The scrollTop of the scroll bar that appears to return to the top is 200 away from duration: 100, // The speed time for returning to the top showBtntime:, // display \ hide the speed time for returning to the top button targetObg: "body" // target location };}) (jQuery); $ (function () {$ ('<a href = "#" class = "go-top">  </ a> '). appendTo ("body ");});

  

Note that the 18th-line variable cssfixedsupport supports position: fixed bug in ie6.

Var cssfixedsupport = $. browser. msie & parseFloat ($. browser. version) <7; // determines whether ie6var controlTop = $ window. height ()-$ this. height ()-opts. pageHeightJg; controlTop = cssfixedsupport? $ Window. scrollTop () + controlTop: controlTop;

For example, in IE6, use absolute for layout. When you scroll the mouse, you must use $ window. scrollTop () to obtain the vertical height of the scroll bar to the top to correct the controlTop parameter.

Trigger the mouse to click the event to return to the top!

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.