-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.backtothetop.js
117 lines (102 loc) · 4.19 KB
/
jquery.backtothetop.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(function($) {
$(window).on('load.BackToTheTop', function() {
$('a[href^="#"]').BackToTheTop();
});
$.fn.BackToTheTop = function( options ) {
var defaults = {
duration: 300,
easing: 'swing',
offset: 0,
hash: true,
scrolloffset: 0,
fadein: 'slow',
fadeout: 'slow',
display: 'bottom-right',
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 999,
position : 'fixed'
};
$.extend( defaults, options );
var init = function() {
$('a[href^="#"]').on('click.BackToTheTop', function() {
var scrollTop =
$(this).data('backtothetop-scrolltop') !== undefined ? $(this).data('backtothetop-scrolltop')
: $(this.hash).offset() ? $(this.hash).offset().top
: $(this).attr('id') == 'backtothetop-fixed' && $(this).attr('href') == '#' ? 0
: null ;
if (scrollTop === null)
return;
var duration = typeof $(this).data('backtothetop-duration') === "undefined" ? defaults.duration : $(this).data('backtothetop-duration');
var easing = typeof $(this).data('backtothetop-easing') === "undefined" ? defaults.easing : $(this).data('backtothetop-easing');
var offset = typeof $(this).data('backtothetop-offset') === "undefined" ? defaults.offset : $(this).data('backtothetop-offset');
var hash = typeof $(this).data('backtothetop-hash') === "undefined" ? defaults.hash : $(this).data('backtothetop-hash');
var href = $(this).attr('href');
$('html,body').animate(
{ 'scrollTop' : scrollTop + offset }, duration, easing,
function() {
if (hash === true) {
window.history.pushState('', '', href);
}
}
);
return false;
});
};
var fixed = function() {
var elem = $('a#backtothetop-fixed');
if ( !elem )
return;
var scrollOffset = typeof elem.data('backtothetop-fixed-scroll-offset') === "undefined" ? defaults.scrolloffset : elem.data('backtothetop-fixed-scroll-offset');
var fadeIn = typeof elem.data('backtothetop-fixed-fadein') === "undefined" ? defaults.fadein : elem.data('backtothetop-fixed-fadein');
var fadeOut = typeof elem.data('backtothetop-fixed-fadeout') === "undefined" ? defaults.fadeout : elem.data('backtothetop-fixed-fadeout');
var display = typeof elem.data('backtothetop-fixed-display') === "undefined" ? defaults.display : elem.data('backtothetop-fixed-display');
var top = typeof elem.data('backtothetop-fixed-top') === "undefined" ? defaults.top : elem.data('backtothetop-fixed-top');
var bottom = typeof elem.data('backtothetop-fixed-bottom') === "undefined" ? defaults.bottom : elem.data('backtothetop-fixed-bottom');
var left = typeof elem.data('backtothetop-fixed-left') === "undefined" ? defaults.left : elem.data('backtothetop-fixed-left');
var right = typeof elem.data('backtothetop-fixed-right') === "undefined" ? defaults.right : elem.data('backtothetop-fixed-right');
var zindex = typeof elem.data('backtothetop-fixed-zindex') === "undefined" ? defaults.zIndex : elem.data('backtothetop-fixed-zindex');
if (display == 'top-left') {
bottom = 'none';
right = 'none';
}
else if (display == 'top-right') {
bottom = 'none';
left = 'none';
}
else if (display == 'bottom-left') {
top = 'none';
right = 'none';
}
else if (display == 'bottom-right') {
top = 'none';
left = 'none';
}
elem.css({ 'display' : 'none' });
$(window).on('scroll.BackToTheTop', function () {
if ($(this).scrollTop() > scrollOffset) {
elem.css({
'top' : top,
'bottom' : bottom,
'left' : left,
'right' : right,
'zIndex' : zindex,
'position' : defaults.position
});
if (elem.css('display') == 'none' ) {
elem.fadeIn(fadeIn);
}
}
else if ($(this).scrollTop() <= 0 + scrollOffset) {
if (elem.css('display') != 'none' ) {
elem.fadeOut(fadeOut);
}
}
});
};
init();
fixed();
};
})(jQuery);