-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial-scrolltop.js
More file actions
71 lines (63 loc) · 2.19 KB
/
Copy pathmaterial-scrolltop.js
File metadata and controls
71 lines (63 loc) · 2.19 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
/**
* Material-scrollTop
*
* Author: Bartholomej
* Website: https://github.com/bartholomej/material-scrollTop
* Docs: https://github.com/bartholomej/material-scrollTop
* Repo: https://github.com/bartholomej/material-scrollTop
* Issues: https://github.com/bartholomej/material-scrollTop/issues
*/
(function($) {
function mScrollTop(element, settings) {
var _ = this,
breakpoint;
var scrollTo = 0;
_.btnClass = '.material-scrolltop';
_.revealClass = 'reveal';
_.btnElement = $(_.btnClass);
_.initial = {
revealElement: 'body',
revealPosition: 'top',
padding: 0,
duration: 600,
easing: 'swing',
onScrollEnd: false
}
_.options = $.extend({}, _.initial, settings);
_.revealElement = $(_.options.revealElement);
breakpoint = _.options.revealPosition !== 'bottom' ? _.revealElement.offset().top : _.revealElement.offset().top + _.revealElement.height();
scrollTo = element.offsetTop + _.options.padding;
$(document).scroll(function() {
if (breakpoint < $(document).scrollTop()) {
_.btnElement.addClass(_.revealClass);
} else {
_.btnElement.removeClass(_.revealClass);
}
});
_.btnElement.click(function() {
var trigger = true;
$('html, body').animate({
scrollTop: scrollTo
}, _.options.duration, _.options.easing, function() {
if (trigger) { // Fix callback triggering twice on chromium
trigger = false;
var callback = _.options.onScrollEnd;
if (typeof callback === "function") {
callback();
}
}
});
return false;
});
}
$.fn.materialScrollTop = function() {
var _ = this,
opt = arguments[0],
l = _.length,
i = 0;
if (typeof opt == 'object' || typeof opt == 'undefined') {
_[i].materialScrollTop = new mScrollTop(_[i], opt);
}
return _;
};
}(jQuery));