-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscrollToTop.js
56 lines (49 loc) · 2 KB
/
scrollToTop.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
/*
jQuery Scroll To Top Plugin
Appends a link allowing you to go back to the top of the page after you've scrolled down a certain distance
version 1
copyright NOE Interactive
*/
(function($, $w) {
"use strict";
$.fn.backtotop = function(options) {
//Options
var settings = {
topAnchor : 'body', //Selector of the element you want to reach when you go back up
topOffset : 300, //Distance from the top the body has to travel to reveal the back to top button
animationSpeed: 1000, //The speed of the animation to go back to top
animationEase: 'swing', //The easing function used for the animation
bckTopLinkTitle: 'go back to the top of the page', // Link Title
bckTopLinkClass : 'backTopLink' //Link Class
};
options && $.extend(settings, options);
//We store all the selectors we are going to need
var $bckTop = $(this),
$b = $('html,body'),
isVisible = false,
$topAnchor = $(settings.topAnchor),
//Link creation
$bckTopLink = $('<a>', {'class' : settings.bckTopLinkClass+' backtotopinstance', title : settings.bckTopLinkTitle, text : settings.bckTopLinkTitle})
.appendTo($bckTop)
.hide()
.click(function(e) {
e.preventDefault();
$b.animate({scrollTop: $topAnchor.position().top},settings.animationSpeed,settings.animationEase);
return false;
});
$w.scroll(_sc);
_sc();//First launch without scroll
function _sc(){
var scrollTop = $w.scrollTop();
if(scrollTop > settings.topOffset && !isVisible) { //Is the link visible?
//No -> show it
$bckTopLink.stop().fadeIn('slow');
isVisible = true;
} else if(scrollTop <= settings.topOffset && isVisible) {
//Yes -> hide it
$bckTopLink.stop().fadeOut('slow');
isVisible = false;
}
}
}
})(jQuery, jQuery(window));