Skip to content

Commit f3c8680

Browse files
adding simple resize observer examples
1 parent 895708d commit f3c8680

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Resize observer border-radius test</title>
6+
<style>
7+
html {
8+
height: 100%;
9+
}
10+
11+
body {
12+
height: inherit;
13+
margin: 0;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
}
18+
div {
19+
background-color: green;
20+
width: 50%;
21+
height: 50%;
22+
}
23+
</style>
24+
</head>
25+
<body>
26+
<div>
27+
</div>
28+
<script>
29+
const resizeObserver = new ResizeObserver(entries => {
30+
for (let entry of entries) {
31+
entry.target.style.borderRadius = Math.min(100, (entry.contentRect.width/10) +
32+
(entry.contentRect.height/10)) + 'px';
33+
}
34+
});
35+
36+
resizeObserver.observe(document.querySelector('div'));
37+
38+
</script>
39+
</body>
40+
</html>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Resize observer text test</title>
6+
<style>
7+
html {
8+
height: 100%;
9+
font-family: 'helvetica neue', arial, sans-serif;
10+
}
11+
12+
body {
13+
height: inherit;
14+
margin: 0;
15+
display: flex;
16+
justify-content: center;
17+
align-items: center;
18+
}
19+
20+
div {
21+
background-color: #eee;
22+
border: 1px solid #ccc;
23+
padding: 20px;
24+
width: 50%;
25+
min-width: 320px;
26+
}
27+
28+
h1 {
29+
margin: 0;
30+
}
31+
32+
p {
33+
line-height: 1.5;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div>
39+
<h1>So what happened?</h1>
40+
<p>And remember, don't do anything that affects anything, unless it turns out you were supposed to, in which case, for the love of God, don't not do it! Ow, my spirit! I don't want to be rescued. You guys aren't Santa! You're not even robots. I've got to find a way to escape the horrible ravages of youth. Suddenly, I'm going to the bathroom like clockwork, every three hours. And those jerks at Social Security stopped sending me checks. Now 'I' have to pay 'them'!</p>
41+
</div>
42+
<script>
43+
if(ResizeObserver) {
44+
const h1Elem = document.querySelector('h1');
45+
const pElem = document.querySelector('p');
46+
47+
const resizeObserver = new ResizeObserver(entries => {
48+
for (let entry of entries) {
49+
h1Elem.style.fontSize = Math.max(2, entry.contentRect.width/200) + 'rem';
50+
pElem.style.fontSize = Math.max(1.2, entry.contentRect.width/600) + 'rem';
51+
}
52+
});
53+
54+
resizeObserver.observe(document.querySelector('div'));
55+
}
56+
</script>
57+
</body>
58+
</html>

0 commit comments

Comments
 (0)