/
index.md
93 lines (71 loc) · 2.32 KB
/
index.md
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: Element.scrollWidth
slug: Web/API/Element/scrollWidth
---
{{ APIRef }}
La propiedad de sólo lectura `Element.scrollWidth` retorna bien la anchura en pixels del contenido de un elemento o bien la anchura del elemento en si, la que sea mayor de ambas. Si el elemento es más ancho que su área contenedora (por ejemplo, si existen barras de desplazamiento para desplazarse a través del contenido), `scrollWidth` es mayor que `clientWidth`.
> **Nota:** El valor de esta propiedad será red redondedo a un entero. Si necesita un valor fraccional, use {{ domxref("element.getBoundingClientRect()") }}.
## Sintaxis
```js
var xScrollWidth = element.scrollWidth;
```
_xScrollWidth_ es el ancho del contenido de _element_ en pixels.
## Ejemplo
```html
<!doctype html>
<html>
<head>
<title>Ejemplo</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>
var buttonOne = document.getElementById("aButton"),
buttonTwo = document.getElementById("anotherButton"),
divOne = document.getElementById("aDiv"),
divTwo = document.getElementById("anotherDiv");
//comprueba si un desbordamiento está ocurriendo
function isOverflowing(element) {
return element.scrollWidth > element.offsetWidth;
}
function alertOverflow(element) {
if (isOverflowing(element)) {
alert("El contenido desborda el contenedor.");
} else {
alert("Sin desobordamiento!");
}
}
buttonOne.addEventListener("click", function () {
alertOverflow(divOne);
});
buttonTwo.addEventListener("click", function () {
alertOverflow(divTwo);
});
</script>
</html>
```
## Especificaciones
{{Specifications}}
## Compatibilidad con navegadores
{{Compat}}
## Véase también
- {{domxref("Element.clientWidth")}}
- {{domxref("HTMLElement.offsetWidth")}}
- [Determinando las dimensiones de elementos](/es/docs/Determining_the_dimensions_of_elements)