Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace alert in web api, scrollwidth property #34733

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 78 additions & 60 deletions files/en-us/web/api/element/scrollwidth/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,91 @@ without a need for horizontal scrollbar, its `scrollWidth` is equal to

## Value

A number.
An integer.

## Examples

### Detecting overflowing content

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.

#### HTML

```html
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Example</title>
<style>
div {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

#aDiv {
width: 100px;
}

button {
margin-bottom: 2em;
}
</style>
</head>

<body>
<div id="aDiv">FooBar-FooBar-FooBar-FooBar</div>
<button id="aButton">Check for overflow</button>

<div id="anotherDiv">FooBar-FooBar-FooBar-FooBar</div>
<button id="anotherButton">Check for overflow</button>
</body>
<script>
const buttonOne = document.getElementById("aButton");
const buttonTwo = document.getElementById("anotherButton");
const divOne = document.getElementById("aDiv");
const divTwo = document.getElementById("anotherDiv");

//check to determine if an overflow is happening
function isOverflowing(element) {
return element.scrollWidth > element.offsetWidth;
}

function alertOverflow(element) {
if (isOverflowing(element)) {
alert("Contents are overflowing the container.");
} else {
alert("No overflows!");
}
}

buttonOne.addEventListener("click", () => {
alertOverflow(divOne);
});

buttonTwo.addEventListener("click", () => {
alertOverflow(divTwo);
});
</script>
</html>
<div id="aDiv">FooBar-FooBar-FooBar-FooBar</div>
evelinabe marked this conversation as resolved.
Show resolved Hide resolved
<button id="aButton">Check for overflow</button>
<pre id="aLog"></pre>
<div id="anotherDiv">FooBar-FooBar-FooBar-FooBar</div>
<button id="anotherButton">Check for overflow</button>
<pre id="anotherLog"></pre>
```

#### CSS

```css
div {
padding: 0.15em;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

button {
margin: 0.15em 0 0.5em 0;
}

pre {
margin: 0.5em 0;
}

#aDiv {
width: 100px;
}

#aLog {
margin-bottom: 2em;
}
```

### Result
#### JavaScript

```js
const aButton = document.getElementById("aButton");
const anotherButton = document.getElementById("anotherButton");

const aDiv = document.getElementById("aDiv");
const anotherDiv = document.getElementById("anotherDiv");

const aLog = document.getElementById("aLog");
const anotherLog = document.getElementById("anotherLog");

// Check if the scrollWidth is bigger than the offsetWidth or not
function isOverflowing(element) {
return element.scrollWidth > element.offsetWidth;
}

function checkOverflow(element) {
if (isOverflowing(element)) {
aLog.innerText = `Contents are overflowing, scrollWidth is ${element.scrollWidth}px`;
} else {
anotherLog.innerText = `No overflows, scrollWidth is ${element.scrollWidth}px`;
}
}

aButton.addEventListener("click", () => {
checkOverflow(aDiv);
});

anotherButton.addEventListener("click", () => {
checkOverflow(anotherDiv);
});
```

#### Result

Click the buttons to check if the content is overflowing the containers.

{{EmbedLiveSample('Examples')}}
{{EmbedLiveSample('Examples', '100%', 190)}}

## Specifications

Expand Down