728x90
1. 기본값
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scoped CSS Variables and JS</title>
</head>
<body>
<h2>Update CSS Variables with <span class='hl'>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>
/*
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;
}
input {
width: 100px;
}
</style>
<script>
</script>
</body>
</html>
2. 결과
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Scoped CSS Variables and JS</title>
</head>
<body>
<h2>Update CSS Variables with <span class="hl">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>
<div class="imgCover">
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500" />
</div>
<style>
/*
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;
}
input {
width: 100px;
}
/* 여기서부터 추가 */
.imgCover {
width: 800px;
height: 500px;
margin: auto;
background-color: white;
filter: blur();
}
</style>
<script>
const imgCover = document.querySelector(".imgCover");
const blur = document.getElementById("blur");
const spacing = document.getElementById("spacing");
const base = document.getElementById("base");
const hl = document.querySelector(".hl");
//spacing 설정
imgCover.style.padding = `${spacing.value}px`;
spacing.oninput = () => {
imgCover.style.padding = `${spacing.value}px`;
};
//blur설정
imgCover.style.filter = `blur(${blur.value}px)`;
blur.oninput = () => {
imgCover.style.filter = `blur(${blur.value}px)`;
};
//background 설정
imgCover.style.backgroundColor = `${base.value}`;
hl.style.color = `${base.value}`;
base.onchange = () => {
imgCover.style.backgroundColor = `${base.value}`;
hl.style.color = `${base.value}`;
};
</script>
</body>
</html>
3. 리뷰
input과 change의 차이점 : input은 실시간으로 값을 받아들이지만 change는 마지막값 즉, 결과값만 받아들인다.
728x90
'개발 > html, css, js' 카테고리의 다른 글
[30일 챌린지 Day-5] flex, classname 추가 활용 (0) | 2022.07.25 |
---|---|
[30일 챌린지 Day-4] 배열,객체 알고리즘 (0) | 2022.07.25 |
[30일 챌린지 Day-2] 시계 만들기 (0) | 2022.07.25 |
[JavaScript] 농구게임 만들기 (0) | 2022.06.21 |
[HTML, JavaScript] 정규표현식 (0) | 2022.06.21 |