-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (45 loc) · 1.56 KB
/
script.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
57
58
59
60
// PRELOADER
var loader = document.getElementById("loading");
function preLoader() {
loader.style.display = "none";
}
// ADD ADDITIONAL COMMANDS
var addBtn = document.getElementById('btn');
addBtn.addEventListener('click', () => {
console.log('addBtn');
document.getElementById("additional").style.display = "block";
document.getElementById("moreAdditional").style.display = "block";
});
// SMOOTH SCROLL TO TOP
function smoothScroll(target, duration) {
var target = document.querySelector(target);
// console.log(target);
var targetPosition = target.getBoundingClientRect().top;
// console.log(targetPosition);
var startPosition = window.pageYOffset;
// console.log(startPosition);
var distance = targetPosition - startPosition;
// console.log(distance);
var startTime = null;
// console.log(startTime);
function animation(currentTime) {
if (startTime === null) startTime = currentTime;
var timeElapsed = currentTime - startTime;
// console.log(timeElapsed);
var run = ease(timeElapsed, startPosition, distance, duration);
// console.log(run);
window.scrollTo(0, run);
if (timeElapsed < duration) requestAnimationFrame(animation);
}
function ease(t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
}
requestAnimationFrame(animation);
}
var myBtn = document.querySelector('#myBtn');
myBtn.addEventListener('click', function () {
smoothScroll('#top', 1000);
});