Day 1: Playing with CSS Variables with JS

April 12th, 2018

This was my first day of JavaScript30 and I was kind of nervous. I watched his tutorial video twice beforehand on two separate occasions, and then when I finally sat down to do it at 5:00, I planned to recreate what I saw and fill in the gaps with my own knowledge.

This didn’t work out well… This is as far as a got from memory.

javascript code

I ended up having to watch the video all the way through multiple times to just copy what Wes did after he did it. Basically this was a very rough introduction. To be fair though, I had never seen “const”, “querySelectorAll”, these two lines “||”, or “–” let alone a lot of other concepts used here.

Here’s the final code:

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Scoped CSS Variables and JS</title>
</head>
<body>
<h2>Update CSS Variables with <span class=’hl’>JS</span></h2>

<div class=”controls”>
<label for=”spacing”>Spacing:</label>
<input id=”spacing” type=”range” name=”spacing” min=”10″ max=”200″ value=”10″ data-sizing=”px”>

<label for=”blur”>Blur:</label>
<input id=”blur” type=”range” name=”blur” min=”0″ max=”25″ value=”10″ data-sizing=”px”>

<label for=”base”>Base Color</label>
<input id=”base” type=”color” name=”base” value=”#ffc600″>
</div>

<img src=”https://source.unsplash.com/7bwQXzbF6KE/800×500″>

<style>
:root {
–base: #ffc600;
–spacing: 10px;
–blur: 10px;
}

img {
padding: var(–spacing);
background: var(–base);
filter: blur(var(–blur));
}

.hl {
color: var(–base);
}

/*
misc styles, nothing to do with CSS variables
*/

body {
text-align: center;
background: #193549;
color: white;
font-family: ‘helvetica neue’, sans-serif;
font-weight: 100;
font-size: 50px;
}

.controls {
margin-bottom: 50px;
}

a {
color: var(–base);
text-decoration: none;
}

input {
width:100px;
}
</style>

<script>
const inputs = document.querySelectorAll(‘.controls input’);

function handleUpdate() {
const suffix = this.dataset.sizing || ”;
document.documentElement.style.setProperty(`–${this.name}`, this.value + suffix);
}

inputs.forEach(input => input.addEventListener(‘change’, handleUpdate));
inputs.forEach(input => input.addEventListener(‘mousemove’, handleUpdate));
</script>

</body>
</html>

And for a better visual, check out the actual product!