From 1dea5e43e2ddc74a9cefd8f4c64cb30315e61691 Mon Sep 17 00:00:00 2001 From: Alcides A Date: Mon, 5 Oct 2020 22:08:39 -0300 Subject: [PATCH 1/3] add elementbiggerthanviewport --- snippets/elementBiggerThanViewport.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 snippets/elementBiggerThanViewport.md diff --git a/snippets/elementBiggerThanViewport.md b/snippets/elementBiggerThanViewport.md new file mode 100644 index 000000000..511573da7 --- /dev/null +++ b/snippets/elementBiggerThanViewport.md @@ -0,0 +1,27 @@ +--- +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(); //
...
+``` + + From e7d34adfc85fa4f3d5b977cddb5709ab5eedd8b2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 6 Oct 2020 17:41:22 +0300 Subject: [PATCH 2/3] Update and rename elementBiggerThanViewport.md to getElementsBiggerThanViewport.md --- snippets/elementBiggerThanViewport.md | 27 ----------------------- snippets/getElementsBiggerThanViewport.md | 25 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 27 deletions(-) delete mode 100644 snippets/elementBiggerThanViewport.md create mode 100644 snippets/getElementsBiggerThanViewport.md diff --git a/snippets/elementBiggerThanViewport.md b/snippets/elementBiggerThanViewport.md deleted file mode 100644 index 511573da7..000000000 --- a/snippets/elementBiggerThanViewport.md +++ /dev/null @@ -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(); //
...
-``` - - diff --git a/snippets/getElementsBiggerThanViewport.md b/snippets/getElementsBiggerThanViewport.md new file mode 100644 index 000000000..eab20310f --- /dev/null +++ b/snippets/getElementsBiggerThanViewport.md @@ -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(); //
+``` + + From bec3fd2e8dbecb75912d74877eef3fa173bf7a94 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 6 Oct 2020 17:41:48 +0300 Subject: [PATCH 3/3] Update getElementsBiggerThanViewport.md --- snippets/getElementsBiggerThanViewport.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/getElementsBiggerThanViewport.md b/snippets/getElementsBiggerThanViewport.md index eab20310f..664cbf985 100644 --- a/snippets/getElementsBiggerThanViewport.md +++ b/snippets/getElementsBiggerThanViewport.md @@ -1,5 +1,5 @@ --- -title: elementBiggerThanViewport +title: getElementsBiggerThanViewport tags: browser,intermediate --- @@ -21,5 +21,3 @@ const getElementsBiggerThanViewport = () => { ```js getElementsBiggerThanViewport(); //
``` - -