03 - CSS Variables and JS
February 19, 2017 ยท View on GitHub
This is a JavaScript practice with JavaScript30 by Wes Bos without any frameworks, no compilers, no boilerplate, and no libraries.
03 - CSS Variables and JS

view demo here
data- attribute, :root, CSS Variables definition var(--xxx), filter: blur(), change event and mousemove event
-
datasetproperty allows to custom data attributes likedata-xxxon the element, either in HTML or in the DOM. It's a map of DOMString, one entry for each custom data attribute. -
:rootselector matches the document's root element is always the html element and it's also where we declare the variable for the base element in HTML. -
once we declare CSS Variables, then we can add it to our specific elements, like
imgbelow, check how to declare it here. -
CSS Variable declare syntax is
--, just like$in SASS.
:root {
--spacing: 10px;
}
img {
padding: var(--spacing);
}
-
CSS
filterprovides such asblur,bightnessand so on, take a look at it here. -
NodeList v.s. Array : NodeList is NOT an Array. You can open the
protoin dev tool and see its methods, there areforEach(),keys()..., and Array's prototype hasmap(),pop()...etc.
Handling suffix with dataset
use dataset to deal with suffix px by adding data-sizing: px as an attribute on input element.
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
and the get the suffix by dataset.sizing via JS
const suffix = this.dataset.sizing || '';
and don't forget a condition with || '' for <input type=color> which has no px.
Changing CSS property via JS
document.documentElement is the root element in JS, so we can change the global CSS variables by JS is just setProperty to style like so:
document.documentElement.style.setProperty('--base', '#000');