Update and rename elementBiggerThanViewport.md to getElementsBiggerThanViewport.md

This commit is contained in:
Angelos Chalaris
2020-10-06 17:41:22 +03:00
committed by GitHub
parent 1dea5e43e2
commit e7d34adfc8
2 changed files with 25 additions and 27 deletions

View File

@ -1,27 +0,0 @@
---
title: elementBiggerThanViewport
tags: beginner
---
Get elements in html bigger than the viewport width, useful when you're looking for what is causing the horizontal scroll
- Returns all elements that are bigger than the viewport
```js
const getElementsBiggerThanViewport = () =>
let docWidth = document.documentElement.offsetWidth;
[].forEach.call(
document.querySelectorAll('*'),
(el) => {
if (el.offsetWidth > docWidth) console.log('el', el);
}
);
```
```js
getElementsBiggerThanViewport(); // <section id="about">...</section>
```

View File

@ -0,0 +1,25 @@
---
title: elementBiggerThanViewport
tags: browser,intermediate
---
Returns an array of HTML elements whose width is larger than that of the viewport's.
- Use `HTMLElement.prototype.offsetWidth()` to get the width of the `document`.
- Use `Array.prototype.reduce()` on the result of `document.querySelectorAll()` to check the width of all elements in the document.
```js
const getElementsBiggerThanViewport = () => {
const docWidth = document.documentElement.offsetWidth;
return [...document.querySelectorAll('*')].reduce((acc, el) => {
if (el.offsetWidth > docWidth) acc.push(el);
return acc;
}, []);
}
```
```js
getElementsBiggerThanViewport(); // <div id="ultra-wide-item" />
```