Created
July 2, 2017 05:11
-
-
Save shuaibird/286760b5c57e1f324f34372a9c8d338f to your computer and use it in GitHub Desktop.
Draw a grid using css & js
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
body, | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
.line { | |
background-color: #efefef; | |
} | |
.line--horizontal { | |
position: absolute; | |
height: 1px; | |
width: 100%; | |
} | |
.line--vertical { | |
position: fixed; | |
width: 1px; | |
height: 100%; | |
} | |
svg, | |
img { | |
z-index: 5; | |
position: relative; | |
} |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>GRID</title> | |
<link href="grid.css" rel="stylesheet" /> | |
</head> | |
<body> | |
<div id="grid"></div> | |
</body> | |
<script src="grid.js"></script> | |
</html> |
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
(() => { | |
const drawLines = (size, orientation, interval = 10) => { | |
const drawLine = (() => { | |
const styleProp = (orientation === 'vertical') ? 'left' : 'top' | |
return (position = 0) => | |
` | |
<div | |
class="line line--${orientation}" | |
style="${styleProp}: ${position}px" | |
> | |
</div> | |
` | |
})() | |
const lineCount = Math.floor(size / interval) | |
return [...Array(lineCount)].reduce((acc, cur, index) => | |
acc + drawLine(interval * (index + 1)) | |
, drawLine()) | |
} | |
const grid = document.querySelector('#grid') | |
const horizontalLines = drawLines(document.body.scrollHeight, 'horizontal') | |
const verticalLines = drawLines(window.outerWidth, 'vertical') | |
grid.innerHTML = verticalLines + horizontalLines | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment