-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.backtotop.js
50 lines (40 loc) · 1.28 KB
/
jquery.backtotop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;(function( $, window, document, undefined ) {
var pluginName = 'backtotop',
defaults = {
backToTopTxt: "back to top"
};
function Plugin( element, options ) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
$backToTopEle = $(this.element);
$backToTopEle.text(this.options.backToTopTxt)
.attr("title", this.options.backToTopTxt)
.click(function () {
$("html, body").animate({ scrollTop: 0 }, 120);
}),
$backToTopFun = function () {
var st = $(document).scrollTop(),
winh = $(window).height();
(st > 0) ? $backToTopEle.show() : $backToTopEle.hide();
//IE6 positioning
if (!window.XMLHttpRequest) {
$backToTopEle.css("top", st + winh - 166);
}
};
$(window).bind("scroll", $backToTopFun);
$(function () { $backToTopFun(); });
};
$.fn[pluginName] = function (options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin(this, options));
}
});
}
})( jQuery, window, document );