forked from EnjoyDigital/ED-Back-to-top
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackToTop.js
38 lines (31 loc) · 767 Bytes
/
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
/**
* Creates a neat little back to top button
* that only shows if the user has scrolled
* a certain amount.
*/
(function($){
'use strict';
$.extend({
backToTop: function(options){
var settings = $.extend({
'fromTop': 50
}, options);
var $button = $('<a/>', {
'class': 'ed-backToTop',
'href': '#'
}).appendTo($('body'));
$button.on('click', function(e){
e.preventDefault();
$('html, body').animate({ scrollTop: '0px' });
});
$(window).scroll(function(){
var distance = $(window).scrollTop();
if(distance >= settings.fromTop && !$button.is(':visible')){
$button.fadeIn();
}else if(distance < settings.fromTop && $button.is(':visible')){
$button.fadeOut();
}
});
}
});
})(jQuery);