Last active
December 1, 2015 02:49
-
-
Save johnpmorris/2ccea3d2bf93ff6cdf23 to your computer and use it in GitHub Desktop.
Javascript that draws an interactive grid to test if the document to be rendered complies with facebook's 20% text to image rule. Click the squares with text.
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
<style type="text/css"> | |
@media screen { | |
body { margin: 0; padding: 0; } | |
#twenty-percent-grid { position: absolute; left: 0; right: 0; bottom: 0; top: 0; overflow: auto; } | |
#twenty-percent-grid .grid-cell { float: left; width: 20vw; margin-left: -1px; margin-top: -1px; margin-bottom: -1px; margin-right: -1px; height: 20vh; border: 1px solid rgba(0, 225, 0, 0.4); } | |
#twenty-percent-grid .grid-cell:hover { background: rgba(0, 225, 0, 0.01); } | |
#twenty-percent-grid .grid-cell.js-active { background: rgba(0, 225, 0, 0.25); } | |
#twenty-percent-grid.warning .grid-cell { border: 1px solid rgba(225, 0, 0, 0.4); } | |
#twenty-percent-grid.warning .grid-cell:hover { background: rgba(225, 0, 0, 0.1); } | |
#twenty-percent-grid.warning .grid-cell.js-active { background: rgba(225, 0, 0, 0.25); } | |
} | |
</style> | |
<script type="text/javascript"> | |
document.addEventListener("DOMContentLoaded", drawGrid); | |
function drawGrid() { | |
var showGrid = true | |
if (showGrid){ | |
var insertGrid = document.createElement('div') | |
insertGrid.setAttribute("id", "twenty-percent-grid"); | |
document.body.appendChild(insertGrid); | |
for (i = 0; i < 25; i++ ) { | |
var grid = document.getElementById('twenty-percent-grid') | |
var cell = document.createElement('div') | |
cell.className = 'grid-cell'; | |
grid.appendChild(cell); | |
addSelectListeners(); | |
} | |
} | |
} | |
function addSelectListeners() { | |
var cells = document.getElementsByClassName('grid-cell') | |
for(var i=0;i<cells.length;i++){ | |
cells[i].addEventListener('click', selectCell, false); | |
} | |
} | |
function selectCell() { | |
setActiveCell(this); | |
} | |
function setActiveCell(cell) { | |
var activeCells = document.getElementsByClassName('js-active') | |
if (cell.classList.contains('js-active')) { | |
cell.classList.remove('js-active') | |
} | |
else { | |
cell.classList.add('js-active') | |
} | |
checkWarning(activeCells); | |
} | |
function checkWarning(cellsToCheck) { | |
var grid = document.getElementById('twenty-percent-grid') | |
if (cellsToCheck.length > 5) { | |
grid.className = 'warning' | |
} | |
else if (grid.classList.contains('warning')) { | |
grid.classList.remove('warning') | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment