Skip to content

Commit d89f09b

Browse files
overscroll-behavior property demo
1 parent 0af2a91 commit d89f09b

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

overscroll-behavior/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>overscroll-behavior demo</title>
6+
<link href="style.css" rel="stylesheet">
7+
<script src="main.js" defer></script>
8+
</head>
9+
<body>
10+
<header>
11+
<h1>overscroll-behavior demo</h1>
12+
</header>
13+
<main>
14+
15+
</main>
16+
<footer>
17+
<p>Copyright nobody; have fun.</p>
18+
</footer>
19+
<section class="chatbox">
20+
<header>
21+
<h2>Active chat</h2>
22+
</header>
23+
<div class="messages">
24+
<p class="me">Chris: Hello!</p>
25+
<p class="you">Bob: I am well — how are you?</p>
26+
<p class="me">Chris: Fine thanks, just documenting overscroll-behavior</p>
27+
<p class="you">Bob: Oooh, sounds hard!</p>
28+
<p class="me">Chris: Nah, it's OK really. It is a useful property to have for mobile apps especially.</p>
29+
<p class="you">Bob: Cool, where can I learn more?</p>
30+
</div>
31+
<form>
32+
<input type="text" aria-label="Enter your chat message here" required autofocus>
33+
<button>Send</button>
34+
</form>
35+
</section>
36+
</body>
37+
</html>

overscroll-behavior/main.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Generate fake contacts
2+
3+
let mainElem = document.querySelector('main');
4+
5+
for(var i = 1; i < 51; i++) {
6+
let divElem = document.createElement('div');
7+
let pElem = document.createElement('p');
8+
9+
pElem.textContent = 'Contact ' + i;
10+
11+
mainElem.appendChild(divElem);
12+
divElem.appendChild(pElem);
13+
}
14+
15+
// Allow submission of chat messages
16+
17+
let form = document.querySelector('form');
18+
let input = document.querySelector('input');
19+
let messages = document.querySelector('.messages');
20+
messages.scrollTop = messages.scrollHeight;
21+
22+
form.addEventListener('submit', e => {
23+
e.preventDefault();
24+
25+
let pElem = document.createElement('p');
26+
pElem.textContent = 'Chris: ' + input.value;
27+
28+
messages.appendChild(pElem);
29+
messages.scrollTop = messages.scrollHeight;
30+
31+
input.value = '';
32+
input.focus();
33+
});

overscroll-behavior/style.css

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* General set up */
2+
3+
* {
4+
box-sizing: border-box;
5+
}
6+
7+
html {
8+
font-family: sans-serif;
9+
}
10+
11+
body {
12+
margin: 0;
13+
}
14+
15+
/* Styling of contacts */
16+
17+
header, footer {
18+
background-color: #a60000;
19+
color: white;
20+
text-align: center;
21+
height: 50px;
22+
line-height: 50px;
23+
text-shadow: 2px 2px 1px black;
24+
}
25+
26+
h1, h2 {
27+
margin: 0;
28+
}
29+
30+
main div {
31+
padding: 10px 20px;
32+
border-bottom: 1px solid #eee;
33+
}
34+
35+
p {
36+
margin: 0;
37+
}
38+
39+
main div p {
40+
line-height: 20px;
41+
}
42+
43+
header {
44+
position: sticky;
45+
top: 0;
46+
}
47+
48+
/* Styling of chatbox */
49+
50+
.chatbox {
51+
background: white;
52+
width: 200px;
53+
height: 300px;
54+
border: 1px solid black;
55+
position: fixed;
56+
bottom: 10px;
57+
right: 10px;
58+
}
59+
60+
.messages {
61+
height: 220px;
62+
overflow: auto;
63+
/* use overscroll-behavior-y to stop page scrolling behind the chatbox,
64+
when the chatbox's scroll limits are reached */
65+
overscroll-behavior-y: contain;
66+
}
67+
68+
.messages p {
69+
margin: 10px;
70+
padding: 5px;
71+
border: 1px solid black;
72+
}
73+
74+
.me {
75+
background-color: #bbb;
76+
}
77+
78+
.you {
79+
background-color: #eee;
80+
}
81+
82+
.chatbox form {
83+
height: 30px;
84+
display: flex;
85+
}
86+
87+
form input, form button {
88+
display: block;
89+
}
90+
91+
form input {
92+
width: 70%;
93+
}
94+
95+
form button {
96+
width: 30%;
97+
font-size: 0.7rem;
98+
}

0 commit comments

Comments
 (0)