Skip to content

Commit bf1d743

Browse files
evelinabeJosh-Cena
andauthored
Replace alert in web api, scrollwidth property (#34733)
* Replace alert in scrollwidth example * Code review changes * Code review changes * Update index.md --------- Co-authored-by: Joshua Chen <[email protected]>
1 parent 4656260 commit bf1d743

File tree

1 file changed

+78
-60
lines changed
  • files/en-us/web/api/element/scrollwidth

1 file changed

+78
-60
lines changed

files/en-us/web/api/element/scrollwidth/index.md

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,91 @@ without a need for horizontal scrollbar, its `scrollWidth` is equal to
2626
2727
## Value
2828

29-
A number.
29+
An integer.
3030

3131
## Examples
3232

33+
### Detecting overflowing content
34+
35+
In this example, we use the `scrollWidth` property to check if the content of an element is overflowing its boundaries. We have two `div` elements, the first with a width of `100px`, and the second without a fixed width. Their content is exactly the same, and we display a message about whether each one is overflowing its container.
36+
37+
#### HTML
38+
3339
```html
34-
<!doctype html>
35-
<html lang="en-US">
36-
<head>
37-
<meta charset="UTF-8" />
38-
<title>Example</title>
39-
<style>
40-
div {
41-
overflow: hidden;
42-
white-space: nowrap;
43-
text-overflow: ellipsis;
44-
}
45-
46-
#aDiv {
47-
width: 100px;
48-
}
49-
50-
button {
51-
margin-bottom: 2em;
52-
}
53-
</style>
54-
</head>
55-
56-
<body>
57-
<div id="aDiv">FooBar-FooBar-FooBar-FooBar</div>
58-
<button id="aButton">Check for overflow</button>
59-
60-
<div id="anotherDiv">FooBar-FooBar-FooBar-FooBar</div>
61-
<button id="anotherButton">Check for overflow</button>
62-
</body>
63-
<script>
64-
const buttonOne = document.getElementById("aButton");
65-
const buttonTwo = document.getElementById("anotherButton");
66-
const divOne = document.getElementById("aDiv");
67-
const divTwo = document.getElementById("anotherDiv");
68-
69-
//check to determine if an overflow is happening
70-
function isOverflowing(element) {
71-
return element.scrollWidth > element.offsetWidth;
72-
}
73-
74-
function alertOverflow(element) {
75-
if (isOverflowing(element)) {
76-
alert("Contents are overflowing the container.");
77-
} else {
78-
alert("No overflows!");
79-
}
80-
}
81-
82-
buttonOne.addEventListener("click", () => {
83-
alertOverflow(divOne);
84-
});
85-
86-
buttonTwo.addEventListener("click", () => {
87-
alertOverflow(divTwo);
88-
});
89-
</script>
90-
</html>
40+
<div id="div1">FooBar-FooBar-FooBar-FooBar</div>
41+
<button id="button1">Check for overflow</button>
42+
<pre id="log1"></pre>
43+
<div id="div2">FooBar-FooBar-FooBar-FooBar</div>
44+
<button id="button2">Check for overflow</button>
45+
<pre id="log2"></pre>
46+
```
47+
48+
#### CSS
49+
50+
```css
51+
div {
52+
padding: 0.15em;
53+
overflow: hidden;
54+
white-space: nowrap;
55+
text-overflow: ellipsis;
56+
}
57+
58+
button {
59+
margin: 0.15em 0 0.5em 0;
60+
}
61+
62+
pre {
63+
margin: 0.5em 0;
64+
}
65+
66+
#div1 {
67+
width: 100px;
68+
}
69+
70+
#log1 {
71+
margin-bottom: 2em;
72+
}
9173
```
9274

93-
### Result
75+
#### JavaScript
76+
77+
```js
78+
const button1 = document.getElementById("button1");
79+
const button2 = document.getElementById("button2");
80+
81+
const div1 = document.getElementById("div1");
82+
const div2 = document.getElementById("div2");
83+
84+
const log1 = document.getElementById("log1");
85+
const log2 = document.getElementById("log2");
86+
87+
// Check if the scrollWidth is bigger than the offsetWidth or not
88+
function isOverflowing(element) {
89+
return element.scrollWidth > element.offsetWidth;
90+
}
91+
92+
function checkOverflow(element, log) {
93+
if (isOverflowing(element)) {
94+
log.innerText = `Content is overflowing, scrollWidth is ${element.scrollWidth}px`;
95+
} else {
96+
log.innerText = `No overflows, scrollWidth is ${element.scrollWidth}px`;
97+
}
98+
}
99+
100+
button1.addEventListener("click", () => {
101+
checkOverflow(div1, log1);
102+
});
103+
104+
button2.addEventListener("click", () => {
105+
checkOverflow(div2, log2);
106+
});
107+
```
108+
109+
#### Result
110+
111+
Click the buttons to check if the content is overflowing the containers.
94112

95-
{{EmbedLiveSample('Examples')}}
113+
{{EmbedLiveSample("detecting_overflowing_content", "100%", "190")}}
96114

97115
## Specifications
98116

0 commit comments

Comments
 (0)