JavaScript30天挑战 Day3 —— CSS Variables

JavaScript30天挑战 Day3 —— CSS Variables

Artificial-Fool Lv3

在原课程的基础上添加了border-radius选项,并且scale会随border-radius变动;

添加了一键重置功能,点击右上角的JS会将四个值重置到初始状态。

demo

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CSS Variables</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>
Update CSS Variables With
<span onclick="resetNumbers()" class="h1">JS</span>
</h1>

<div class="controls">
<label for="spacing">Spacing:</label>
<input
type="range"
name="spacing"
min="0"
max="100"
value="8"
data-sizing="px"
/>

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

<label for="radius">Border-radius:</label>
<input
type="range"
name="radius"
min="0"
max="50"
value="0"
data-sizing="%"
/>

<label for="base">Base color:</label>
<input type="color" name="base" value="#ceffad" />
</div>
<img src="../assets/bg3.jpg" alt="" />

<script src="script.js"></script>
</body>
</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
* {
margin: 0px;
padding: 0;
box-sizing: border-box;
}
:root {
/* --base: #00fff0; */
--base: #ceffad;
--spacing: 10px;
--blur: 0px;
--radius: 0px;
--scale: 1;
}

body {
background: #162e3f;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: Arial, Helvetica, sans-serif;
color: white;
}
img {
margin-top: 30px;
padding: var(--spacing);
background: var(--base);
filter: blur(var(--blur));
border-radius: var(--radius);
transform: scale(var(--scale));
width: 400px;
height: 250px;
}

h1 {
color: white;
margin: 7px;
}

.h1 {
color: var(--base);
}

input {
margin: 7px;
}

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
const inputs = document.querySelectorAll('input');
// console.log(inputs[0].value);

function handleUpdate() {
const suffix = this.dataset.sizing || '';
console.log(this.value);
document.documentElement.style.setProperty(
`--${this.name}`,
`${this.value + suffix}`
);
if (this.name === 'radius')
document.documentElement.style.setProperty(
'--scale',
`${1 - this.value / 500}`
);
}

inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));

//点击标题重置
function resetNumbers() {
console.log('reset');
inputs[0].value = '8';
inputs[1].value = '0';
inputs[2].value = '0';
inputs[3].value = '#ceffad';
document.documentElement.style.setProperty('--base', '#ceffad');
document.documentElement.style.setProperty('--blur', '0px');
document.documentElement.style.setProperty('--spacing', '8px');
document.documentElement.style.setProperty('--scale', '1');
document.documentElement.style.setProperty('--radius', '0px');
}

  • Title: JavaScript30天挑战 Day3 —— CSS Variables
  • Author: Artificial-Fool
  • Created at : 2024-12-09 07:36:51
  • Updated at : 2024-12-09 08:11:34
  • Link: https://space.bilibili.com/636023121/2024/12/09/jschallenge3/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
JavaScript30天挑战 Day3 —— CSS Variables