-
Notifications
You must be signed in to change notification settings - Fork 0
/
02 - CSS Variables.html
95 lines (77 loc) · 2.37 KB
/
02 - CSS Variables.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scoped CSS Variables and JS</title>
</head>
<body>
<h2><span class="color">Update</span> CSS <span class="color">Variables</span> with <span class='h1'>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/800x500">
<style>
:root{
--base:#ffc600;
--spacing:10px;
--blur:10px;
}
img{
padding:var(--spacing);
background:var(--base);
filter:blur(var(--blur));
}
.h1{
color:var(--base);
}
span.color{
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');
// querySelctorAll하면 nodeList로 나오는데 정확히는 배열은 아니다
// 나중에 배열로 바꿔야 하겠지만 지금은 필요없고, 차이점은 노드리스트는 배열에 비해 메소드가 제한적이다
function handleUpdate(){
// console.log(this.value);
//const test = this; //함수안에서 this는 window?라고 알고 있는데 input인듯 ..bind../?
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));
// addEventListener연습
// document.querySelector('img').addEventListener('click',
// function (){
// this.style.display = 'none';
// }
// )
</script>
</body>
</html>