-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollToTopAndBottom.html
55 lines (48 loc) · 1.56 KB
/
scrollToTopAndBottom.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#container {
background-color: rgb(30, 75, 69);
color: rgb(90, 196, 177);
width: 250px;
height: 250px;
overflow: auto; /* <-- set overflow auto to get scroll bars */
}
/* NOTE: set the content with higher height than its container to get the scroll bars behavior */
#content {
height: 1000px;
}
</style>
</head>
<body>
<!-- CONTAINER WITH SCROLL BARS -->
<div id="container">
<div id="content">Scroll Me ↓↓</div>
</div>
<button id="scrollToTopButton">Scroll to top of the container</button>
<button id="scrollToBottomButton">Scroll to bottom of the container</button>
<h2 id="showScrollValue"></h2>
<script>
const container = document.querySelector('#container')
const showScrollValue = document.querySelector('#showScrollValue')
const scrollToTopButton = document.querySelector('#scrollToTopButton')
const scrollToBottomButton = document.querySelector(
'#scrollToBottomButton'
)
container.onscroll = () => {
showScrollValue.innerHTML =
'Scrolled from top: ' + container.scrollTop + 'px'
}
scrollToTopButton.onclick = () => {
container.scrollTop = 0
}
scrollToBottomButton.onclick = () => {
container.scrollTop = container.scrollHeight
}
</script>
</body>
</html>