Created
October 5, 2016 18:01
-
-
Save rahatarmanahmed/6c337a5ad9015b18f968483f465b3554 to your computer and use it in GitHub Desktop.
Focusing section of a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// focus(selector) will hide everything on a page, | |
// then centers and displays what the selector matched | |
var container | |
var focus = (selector) => { | |
if (!container) { | |
container = document.createElement('div') | |
container.style.width = document.body.style.width = '100vw' | |
container.style.height = document.body.style.height = '100vh' | |
container.style.backgroundColor = 'white' | |
container.style.display = 'flex' | |
container.style['flex-direction'] = 'column' | |
container.style['align-items'] = 'center' | |
container.style['justify-content'] = 'center' | |
Array.prototype.forEach.call(document.body.children, function(el) { | |
el.style.display = 'none' | |
}) | |
document.body.appendChild(container) | |
var innerContainer = document.createElement('div') | |
innerContainer.style.width = '95%' | |
container.appendChild(innerContainer) | |
container = innerContainer | |
} | |
while (container.firstChild) container.removeChild(container.firstChild) | |
var els = document.querySelectorAll(selector) | |
els.forEach(function (el) { | |
var child = el.cloneNode(true) | |
child.style.margin = 'auto' | |
container.appendChild(child) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment