Skip to content

Commit e12b229

Browse files
committed
03 - CSS Variables: remove jQuery deps, refactor to 1 line function
1 parent 1cb0606 commit e12b229

3 files changed

Lines changed: 28 additions & 64 deletions

File tree

03 - CSS Variables/index.html

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Scoped CSS Variables and JS</title>
6+
<link rel="stylesheet" href="style.css">
67
</head>
78
<body>
89
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
@@ -14,68 +15,13 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
1415
<label for="blur">Blur:</label>
1516
<input id="blur" type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
1617

17-
<label for="base">Base Color</label>
18-
<input id="base" type="color" name="base" value="#ffc600">
18+
<label for="color">Base Color</label>
19+
<input id="color" type="color" name="color" value="#ffc600">
1920
</div>
2021

2122
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2223

23-
<style>
24-
25-
/*
26-
misc styles, nothing to do with CSS variables
27-
*/
28-
29-
:root {
30-
--base: red;
31-
--spacing: 10px;
32-
--blur: 0;
33-
}
34-
35-
body {
36-
text-align: center;
37-
background: #193549;
38-
color: white;
39-
font-family: 'helvetica neue', sans-serif;
40-
font-weight: 100;
41-
font-size: 50px;
42-
}
43-
44-
.controls {
45-
margin-bottom: 50px;
46-
}
47-
48-
input {
49-
width:100px;
50-
}
51-
52-
img {
53-
padding: var(--spacing);
54-
background: var(--base);
55-
filter: blur(var(--blur));
56-
}
57-
58-
.hl {
59-
color: var(--base);
60-
filter: blur(var(--blur));
61-
}
62-
63-
</style>
64-
65-
<script type="text/javascript">
66-
67-
const inputs = document.querySelectorAll('input');
68-
69-
function handleChange(){
70-
const sizing = this.dataset.sizing||'';
71-
document.documentElement.style.setProperty(`--${this.name}`,this.value + sizing);
72-
}
73-
74-
inputs.forEach(input => input.addEventListener('change',handleChange));
75-
inputs.forEach(input => input.addEventListener('mousemove',handleChange));
76-
77-
78-
</script>
24+
<script src="main.js"></script>
7925

8026

8127
</body>

03 - CSS Variables/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Created by ale on 05/01/18.
3+
*/
4+
5+
window.addEventListener("DOMContentLoaded",function () {
6+
7+
function getSizing(elem){
8+
return elem.dataset.sizing || '';
9+
}
10+
11+
function setCssVar(elem){
12+
document.documentElement.style.setProperty(`--${elem.name}`,elem.value + getSizing(elem));
13+
}
14+
15+
document.querySelectorAll('input').forEach(input => {input.addEventListener('input',(e)=>{ setCssVar(e.target) })})
16+
17+
18+
});

03 - CSS Variables/style.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
:root {
6-
--base-color: red;
6+
--color: red;
77
--main-font: "Arial", sans-serif;
8-
--main-padding: 10px;
9-
--main-blur: 10px;
8+
--spacing: 10px;
9+
--blur: 10px;
1010
}
1111

1212
body {
@@ -27,9 +27,9 @@ input {
2727
}
2828

2929
img {
30-
background-color: var(--base-color);
31-
padding: var(--main-padding);
32-
filter: blur(var(--main-blur));
30+
background-color: var(--color);
31+
padding: var(--spacing);
32+
filter: blur(var(--blur));
3333

3434
}
3535

0 commit comments

Comments
 (0)