Skip to content

Instantly share code, notes, and snippets.

@Mason-McGough
Last active August 16, 2018 18:56
Show Gist options
  • Save Mason-McGough/86817c0c032ba3bdd9f6af3d5ea6f933 to your computer and use it in GitHub Desktop.
Save Mason-McGough/86817c0c032ba3bdd9f6af3d5ea6f933 to your computer and use it in GitHub Desktop.
HTML Stripe Effect With Easy CSS/JS Variables
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>Stripe Effect</title>
<style>
body {
padding: 25px;
}
.container {
width: 500px;
height: 500px;
background-color: #3891A6;
transform: translateZ(0);
border-radius: 50%;
border: #B20039 solid 10px;
}
</style>
</head>
<body>
<div id="radialStripes" class="container"></div>
<script>
var color1 = "#FF1962";
var color2 = "#00FF1A";
var spacing = 40;
var offset = 0;
var tempColor;
var container = document.querySelector(".container");
var style = window.getComputedStyle(container);
var width = parseInt(style.getPropertyValue('width'), 10);
var height = parseInt(style.getPropertyValue('height'), 10);
var diameter = width > height ? width : height;
container.style.setProperty("--color1", color1);
container.style.setProperty("--color2", color2);
var styleStr = radGrad('#radialStripes', '--color1', '--color2', diameter, spacing);
var sheet = document.createElement('style')
sheet.innerHTML = styleStr;
document.body.appendChild(sheet);
function radGrad(selector, color1, color2, diameter, spacing) {
var style = selector
+ ' {background-image: radial-gradient(circle, '
+ 'var(--color1) 0px, '
+ 'var(--color1) calc(var(--offset) + 0px)';
var toggle = false;
for (i = 0; i < diameter; i += spacing) {
ca = toggle ? color1 : color2;
style += ', ' + 'var(' + ca + ') '
+ 'calc(var(--offset) + ' + i.toString() + 'px), '
+ 'var(' + ca + ') '
+ 'calc(var(--offset) + ' + (i + spacing).toString() + 'px)';
toggle = !toggle;
}
style += ');}';
return style;
}
function swapColors() {
tempColor = color2;
color2 = color1;
color1 = tempColor;
container.style.setProperty("--color1", color1);
container.style.setProperty("--color2", color2);
}
function setOffset(value) {
container.style.setProperty("--offset", value + "px");
}
function animate() {
offset++;
if (offset > spacing) {
offset = 0;
swapColors();
}
setOffset(offset);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment