-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathjquery.scrollUp.js
More file actions
166 lines (142 loc) · 5.4 KB
/
jquery.scrollUp.js
File metadata and controls
166 lines (142 loc) · 5.4 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function ($, window, document) {
'use strict';
// Main function
$.fn.scrollUp = function (options) {
// Ensure that only one scrollUp exists
if (!$.data(document.body, 'scrollUp')) {
$.data(document.body, 'scrollUp', true);
$.fn.scrollUp.init(options);
}
};
// Init
$.fn.scrollUp.init = function (options) {
// Define vars
var o = $.fn.scrollUp.settings = $.extend({}, $.fn.scrollUp.defaults, options),
triggerVisible = false,
animIn, animOut, animSpeed, scrollDis, scrollEvent, scrollTarget, $self;
// Create element
if (o.scrollTrigger) {
$self = $(o.scrollTrigger);
} else {
$self = $('<a/>', {
id: o.scrollName,
href: '#top'
});
}
// Set scrollTitle if there is one
if (o.scrollTitle) {
$self.attr('title', o.scrollTitle);
}
$self.appendTo('body');
// If not using an image display text
if (!(o.scrollImg || o.scrollTrigger)) {
$self.html(o.scrollText);
}
// Minimum CSS to make the magic happen
$self.css({
display: 'none',
position: 'fixed',
zIndex: o.zIndex
});
// Active point overlay
if (o.activeOverlay) {
$('<div/>', {
id: o.scrollName + '-active'
}).css({
position: 'absolute',
'top': o.scrollDistance + 'px',
width: '100%',
borderTop: '1px dotted' + o.activeOverlay,
zIndex: o.zIndex
}).appendTo('body');
}
// Switch animation type
switch (o.animation) {
case 'fade':
animIn = 'fadeIn';
animOut = 'fadeOut';
animSpeed = o.animationSpeed;
break;
case 'slide':
animIn = 'slideDown';
animOut = 'slideUp';
animSpeed = o.animationSpeed;
break;
default:
animIn = 'show';
animOut = 'hide';
animSpeed = 0;
}
// If from top or bottom
if (o.scrollFrom === 'top') {
scrollDis = o.scrollDistance;
} else {
scrollDis = $(document).height() - $(window).height() - o.scrollDistance;
}
// Scroll function
scrollEvent = $(window).scroll(function () {
if ($(window).scrollTop() > scrollDis) {
if (!triggerVisible) {
$self[animIn](animSpeed);
triggerVisible = true;
}
} else {
if (triggerVisible) {
$self[animOut](animSpeed);
triggerVisible = false;
$(window).trigger('ontop.scrollup');
}
}
});
if (o.scrollTarget) {
if (typeof o.scrollTarget === 'number') {
scrollTarget = o.scrollTarget;
} else if (typeof o.scrollTarget === 'string') {
scrollTarget = Math.floor($(o.scrollTarget).offset().top);
}
} else {
scrollTarget = 0;
}
// To the top
$self.click(function (e) {
e.preventDefault();
$('html, body').animate({
scrollTop: scrollTarget
}, o.scrollSpeed, o.easingType);
});
};
// Defaults
$.fn.scrollUp.defaults = {
scrollName: 'scrollUp', // Element ID
scrollDistance: 300, // Distance from top/bottom before showing element (px)
scrollFrom: 'top', // 'top' or 'bottom'
scrollSpeed: 300, // Speed back to top (ms)
easingType: 'linear', // Scroll to top easing (see http://easings.net/)
animation: 'fade', // Fade, slide, none
animationSpeed: 200, // Animation in speed (ms)
scrollTrigger: false, // Set a custom triggering element. Can be an HTML string or jQuery object
scrollTarget: false, // Set a custom target element for scrolling to. Can be element or number
scrollText: 'Scroll to top', // Text for element, can contain HTML
scrollTitle: false, // Set a custom <a> title if required. Defaults to scrollText
scrollImg: false, // Set true to use image
activeOverlay: false, // Set CSS color to display scrollUp active point, e.g '#00FFFF'
zIndex: 2147483647, // Z-Index for the overlay
};
// Destroy scrollUp plugin and clean all modifications to the DOM
$.fn.scrollUp.destroy = function (scrollEvent) {
$.removeData(document.body, 'scrollUp');
$('#' + $.fn.scrollUp.settings.scrollName).remove();
$('#' + $.fn.scrollUp.settings.scrollName + '-active').remove();
// If 1.7 or above use the new .off()
if ($.fn.jquery.split('.')[1] >= 7) {
$(window).off('scroll', scrollEvent);
// remove scrollup events namespace
$(window).off('.scrollup');
// Else use the old .unbind()
} else {
$(window).unbind('scroll', scrollEvent);
$(window).unbind('ontop.scrollup');
}
};
$.scrollUp = $.fn.scrollUp;
})(jQuery, window, document);