This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
scroll-to-element.html
84 lines (74 loc) · 2.58 KB
/
scroll-to-element.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
<html lang="">
<head>
<title>Scroll To Element Example</title>
</head>
<style>
.module {
margin: 20px auto;
width: 215%;
height: 300px;
}
.module-dummy {
background-color: lightgrey;
}
.red {
background-color: burlywood;
}
.blue {
background-color: cornflowerblue;
}
.pink {
background-color: lightpink;
}
.green {
background-color: darkseagreen;
}
.orange {
background-color: orange;
}
</style>
<body>
<p>
Click <button id="scroll-btn">here</button> to scroll down to the
green box.
</p>
<p>Then click on the green box to scroll to the pink box</p>
<p>Then click on the pink box to scroll back up</p>
<div class="module module-dummy"></div>
<div class="module module-dummy red"></div>
<div class="module module-dummy"></div>
<div class="module module-dummy blue"></div>
<div class="module module-dummy"></div>
<div class="module module-dummy green"></div>
<div class="module module-dummy"></div>
<div class="module module-dummy pink"></div>
<div class="module module-dummy"></div>
<div class="module module-dummy orange"></div>
<!-- update the path to scroll script below -->
<script type="module">
import { scrollTo, scrollIntoView } from '../dist/scroll.min.js';
const scrollButton = document.getElementById('scroll-btn');
const greenBox = document.querySelector('.green');
const pink = document.querySelector('.pink');
// when the scroll button is clicked, scroll down 2000 pixels
scrollButton.onclick = function () {
scrollIntoView(greenBox, { duration: 1000 }).then(function () {
console.log('finished scrolling to green box!');
});
};
greenBox.onclick = function () {
scrollIntoView(pink, {
duration: 1000,
easing: 'ease-in-out',
}).then(function () {
console.log('finished scrolling up from green box!');
});
};
pink.onclick = function () {
scrollTo(document.body, { top: 0 }).then(function () {
console.log('scrolled back up to top');
});
};
</script>
</body>
</html>