jquery smoothing back to top (scrolling top)

Source: Internet
Author: User

Jquery.scrolltotop.js file

The code is as follows Copy Code

/**
* Escrolltotop jquery back to top plugin, smoothing back to top,
*
* Parameter settings
* Startline: To return the top button from the top of the distance
* Scrollto: Scroll to the distance from the top, or the position of an ID element
* Scrollduration: Smooth scrolling time
* Fadeduration: Fade time eg:[500, 100] [0] fade in, [1] fade out
* controlhtml:html Code
* ClassName: Style name
* TitleName: Return to the top title attribute
* OffsetX: Back to top right offset position
* OffsetY: Back to top bottom offset position
* Anchorkeyword: Cat point Link
* Eg:
* $.scrolltotop ({
* scrollduration:1000
*   });
*/
(function ($) {
$.scrolltotop = function (options) {
Options = Jquery.extend ({
startline:100,//Appearance return top button distance from top
scrollto:0,//scroll to the distance from the top, or the position of an ID element
scrollduration:500,//Smooth scrolling time
Fadeduration: [500, 100],//Fade out time, [0] fade in, [1] fade out
controlhtml: ' <a href= ' javascript:; ><b> back to top ↑</b></a> ',//html code
ClassName: ',//style name
TitleName: ' Back to top ',//back to top title property
Offsetx:5,//back to top right offset position
Offsety:5,//back to top bottom offset position
Anchorkeyword: ' #top ',//Cat dot Link
}, Options);

var state = {
Isvisible:false,
Shouldvisible:false
};

var current = this;

var $body, $control, $cssfixedsupport;

var init = function () {
var iebrws = document.all;
$cssfixedsupport =!iebrws | | Iebrws
&& Document.compatmode = "Css1compat"
&& window. XMLHttpRequest
$body = (window.opera)? (Document.compatmode = = "Css1compat" $ (' HTML '): $ (' body ')): $ (' html,body ');
$control = $ (' <div class= "' +options.classname+ '" id= "Topcontrol" > ' + options.controlhtml + ' </div> '). CSS ({
Position: $cssfixedsupport? ' Fixed ': ' absolute ',
Bottom:options.offsety,
Right:options.offsetx,
opacity:0,
Cursor: ' pointer '
}). attr ({
Title:options.titleName
). Click (function () {
Scrollup ();
return false;
}). Appendto (' body ');
if (document.all &&!window. XMLHttpRequest && $control. Text ()!= ') {
$control. CSS ({
Width: $control. Width ()
});
}
Togglecontrol ();
$ (' a[href= ' + Options.anchorkeyword + ' "]"). Click (function () {
Scrollup ();
return false;
});
$ (window). bind (' Scroll resize ', function (e) {
Togglecontrol ();
})

return to current;
};

var scrollup = function () {
if (! $cssfixedsupport) {
$control. CSS ({
opacity:0
});
}
var dest = isNaN (Options.scrollto)? parseint (Options.scrollto): Options.scrollto;
if (typeof dest = = "string") {
Dest = JQuery (' # ' + dest). length >= 1? JQuery (' # ' + dest). Offset (). top:0;
}
$body. Animate ({
Scrolltop:dest
}, Options.scrollduration);
};

var keepfixed = function () {
var $window = jQuery (window);
var controlx = $window. ScrollLeft () + $window. Width ()
-$control. Width ()-options.offsetx;
var controly = $window. ScrollTop () + $window. Height ()
-$control. Height ()-options.offsety;
$control. CSS ({
Left:controlx + ' px ',
Top:controly + ' px '
});
};

var Togglecontrol = function () {
var scrolltop = jQuery (window). scrolltop ();
if (! $cssfixedsupport) {
This.keepfixed ()
}
state.shouldvisible = (scrolltop >= options.startline)? True:false;
if (state.shouldvisible &&!state.isvisible) {
$control. Stop (). Animate ({
Opacity:1
}, Options.fadeduration[0]);
State.isvisible = true;
else if (state.shouldvisible = = False && state.isvisible) {
$control. Stop (). Animate ({
opacity:0
}, options.fadeduration[1]);
State.isvisible = false;
}
};

Return init ();
};
}) (JQuery);


Introducing Files:

The code is as follows Copy Code
<script type= "Text/javascript"
src= "/js/jquery-1.6.1.min.js" >
</script>
<script type= "Text/javascript"
src= "/js/jquery.scrolltotop.js" >
</script>

Call Method:

  code is as follows copy code

<script Type= "Text/javascript"
 jquery (function ($) {
    $.scrolltotop ({
    classname: ' Totop ',
    controlhtml: ' <a href= ' javascript:; > Back to Top ↑</a> ',
        //here can be replaced with pictures like ',
    //controlhtml :   src= "/go-top.png"/> ",
  //can be replaced with the following picture format
    offsety: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.