This repository was archived by the owner on Dec 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (50 loc) · 1.38 KB
/
index.js
File metadata and controls
65 lines (50 loc) · 1.38 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
var scrollTo = require('scroll-to');
var extend = require('xtend/mutable');
function ScrollUp(el, options){
if (!(this instanceof ScrollUp)) return new ScrollUp (el, options);
//check what parametrs are recieved
if (arguments[1]){
this.el = el;
}else{
if(arguments[0]){
if(arguments[0] instanceof Element){
this.el = el;
}else {
options = el;
}
}
}
if (!this.el){
//if el isn't provided
this.el = document.createElement('div');
this.el.innerHTML = "▲<br><span>" + "back to top" + "</span>";
this.appendEl.appendChild(this.el);
}
extend(this, options);
this.el.classList.add("back-to-top-btn");
this.el.setAttribute('hidden', true);
window.addEventListener('scroll', this.hideShow.bind(this));
this.el.addEventListener('click', this.moveToTop.bind(this));
}
var proto = ScrollUp.prototype;
//defaults
proto.scrollDistance = 900;
proto.animateScrollDuration = 500;
proto.ease = "out-cube";
proto.appendEl = document.body;
proto.hideShow = function(){
var pageY = window.pageYOffset || document.documentElement.scrollTop;
var innerHeight = this.scrollDistance || document.documentElement.clientHeight;
if(pageY > innerHeight){
this.el.removeAttribute('hidden');
}else{
this.el.setAttribute('hidden', true);
}
}
proto.moveToTop = function(){
scrollTo(0, 0, {
ease: this.ease,
duration: this.animateScrollDuration
});
}
module.exports = ScrollUp;