Visitar URL original
03 · soyaine/JavaScript30@b769822 · GitHub
Skip to content

Commit b769822

Browse files
committed
03
1 parent 6751508 commit b769822

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Scoped CSS Variables and JS</title>
6+
</head>
7+
<body>
8+
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
9+
10+
<div class="controls" rel-controlbox>
11+
<label for="spacing">Spacing:</label>
12+
<input type="range" name="padding" min="10" max="200" value="10" data-sizing="px">
13+
14+
<label for="blur">Blur:</label>
15+
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
16+
17+
<label for="base">Base Color</label>
18+
<input type="color" name="color" value="#ffc600">
19+
20+
<div class="codeBox">
21+
<code>
22+
img{
23+
padding: <span id="js-padding">10</span>px;
24+
filter: blur(<span id="js-blur">0</span>px);
25+
background:<span id="js-color">#ffc600</span>;
26+
}
27+
</code>
28+
</div>
29+
</div>
30+
31+
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
32+
33+
<style>
34+
35+
/*
36+
misc styles, nothing to do with CSS variables
37+
*/
38+
:root{
39+
--padding: 10px;
40+
--blur: 0px;
41+
--color: #ffc600;
42+
}
43+
img{
44+
padding: var(--padding);
45+
filter: blur(var(--blur));
46+
background: var(--color);
47+
}
48+
49+
body {
50+
text-align: center;
51+
}
52+
53+
body {
54+
background: #193549;
55+
color: white;
56+
font-family: 'helvetica neue', sans-serif;
57+
font-weight: 100;
58+
font-size: 50px;
59+
}
60+
61+
.controls {
62+
margin-bottom: 50px;
63+
}
64+
65+
input {
66+
width:100px;
67+
}
68+
</style>
69+
70+
<script>
71+
72+
/*
73+
业务需求:
74+
拖动滑块、选择颜色,
75+
1、实时改变对应元素的样式,img使用:root中定义的变量,直接改变根节点的数据即可
76+
2、实时改变code中的代码
77+
*/
78+
const controls = document.querySelectorAll("[rel-controlbox] input"),
79+
handle = function(){
80+
const prefix = this.dataset.sizing || '',
81+
name = this.name,
82+
value = this.value;
83+
document.documentElement.style.setProperty(`--${name}`, value + prefix);
84+
document.getElementById(`js-${name}`).innerHTML = value;
85+
};
86+
controls.forEach((item)=>{
87+
// 鼠标滑动->实时改变
88+
item.addEventListener("mousemove", handle, false);
89+
// 改变值
90+
item.addEventListener("change", handle,false);
91+
})
92+
</script>
93+
94+
</body>
95+
</html>

0 commit comments

Comments
 (0)