๐ position: sticky
์คํฌ๋กค ํ ๋ ํ๋ฉด์ ๋์ค๊ฒ ๋๋ฉด ๊ณ ์ ์ํฌ ๋ ์ฌ์ฉ
์์ ๊ฐ์ ํ๋ฉด์ผ๋ก ์ด๋ฏธ์ง๊ฐ ๋์ฌ๋, ์ด๋ฏธ์ง๋ฅผ ๊ณ ์ ์ํค๋๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค.
html
<body style="background : grey; height : 3000px">
<div class="grey">
<div class="image">
<img src="../img/js.jpg" width="100%">
</div>
<div style="clear : both"></div>
<div class="text">Javascript with young-ki</div>
</div>
</body>
css
.grey {
background: lightgrey;
height: 2000px;
margin-top: 500px;
}
.text {
float: left;
width: 300px;
}
.image {
float: right;
width: 400px;
position: sticky;
top: 100px;
}
See the Pen Untitled by ์ก๊ธฐ์ (@edcasphx-the-bold) on CodePen.
๐ sticky ํ์ฉ
์์ ๊ฐ์ ํ๋ฉด์ ๋ง๋ค์ด์ค ๊ฒ์ด๋ค.
html
<body style="background : grey; height : 3000px">
<div class="card-background">
<div class="card-box">
<img src="../img/food1.jpg">
</div>
<div class="card-box">
<img src="../img/food2.jpg">
</div>
<div class="card-box">
<img src="../img/food3.jpg">
</div>
</div>
</body>
css
.card-background {
height: 3000px;
margin-top: 800px;
margin-bottom: 1600px;
}
.card-box img {
display: block;
margin: auto;
max-width: 80%;
}
.card-box {
position: sticky;
top: 400px;
margin-top: 200px;
transition : all 0.05s;
}
sticky๋ฅผ ํ์ฉํ๋ค.
js
window.addEventListener('scroll', function () {
var totalH = document.documentElement.scrollTop; // ์คํฌ๋กค๋ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
console.log(totalH);
var cardBox = document.querySelectorAll('.card-box');
if (cardBox) {
let y = -1 / 500 * totalH + 110 / 50; //๊ณ์ฐ์
let z = (-1 / 5000) * totalH + 565 / 500;
cardBox[0].style.opacity = y;
cardBox[0].style.transform = `scale(${z})`;
}
if (totalH > 1000) {
let y = -1 / 500 * (totalH - 500) + 110 / 50;
let z = (-1 / 5000) * (totalH-500) + 565 / 500;
cardBox[1].style.opacity = y;
cardBox[1].style.transform = `scale(${z})`;
}
});
์นด๋๊ฐ 3๊ฐ์ด๋ฏ๋ก ์ฒซ๋ฒ์งธ, ๋๋ฒ์งธ๋ง ์คํฌ๋กค ํ ์ ์ฌ๋ผ์ง๋๋ก ๊ตฌํํ๋ค. ๋ค์ ํ๋์ฝ๋ฉํ์ผ๋, ์ด๋ฅผ ํ์ฉํด์ ์นด๋๊ฐ ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ๋ ์์ฑํ ์ ์์ ๊ฒ์ด๋ค.
๐ก ์ค์์ดํ ๊ธฐ๋ฅ
๐ mouse
mousedown : ๋ง์ฐ์ค ๋ฒํผ ๋๋ ์๋
mouseup : ๋ง์ฐ์ค ๋ฒํผ ๋ ์ ๋
mousemove : ์์ ์์์ ๋ง์ฐ์ค๋ฅผ ์ด๋ํ ๋
๐ ๋ง์ฐ์ค ๋๋๊ทธํด ๋ค์ ์ด๋ฏธ์ง๋ก ์ด๋
์์ง์ธ ๊ฑฐ๋ฆฌ๊ฐ 100์ด ๋์ง ์์ผ๋ฉด ์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ณ , ์์ง์ธ ๊ฑฐ๋ฆฌ๊ฐ 100 ์ด๊ณผ์ผ ๊ฒฝ์ฐ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ณด์ฌ์ค๋ค.
html
<div style="overflow: hidden">
<div class="slide-container">
<div class="slide-box">
<img src="img/food1.jpg">
</div>
<div class="slide-box">
<img src="img/food2.jpg">
</div>
<div class="slide-box">
<img src="img/food3.jpg">
</div>
</div>
</div>
js
let start = 0;
let ifPress = false;
let moveX = 0;
document.querySelectorAll('.slide-box')[0].addEventListener('mousedown',function(e){
start=e.clientX;
ifPress=true;
})
document.querySelectorAll('.slide-box')[0].addEventListener('mousemove',function(e){
if (ifPress){
moveX=e.clientX - start;
document.querySelectorAll('.slide-box')[0].style.transform = `translateX(${moveX}px)`;
}
})
document.querySelectorAll('.slide-box')[0].addEventListener('mouseup',function(e){
ifPress=false;
console.log(moveX);
if(moveX<-100){
document.querySelector(".slide-container").style.transform =
"translateX(-100vw)";
}else{
document.querySelector(".slide-container").style.transform =
"translateX(0vw)";
}
See the Pen Untitled by ์ก๊ธฐ์ (@edcasphx-the-bold) on CodePen.
๐ touch
๋ชจ๋ฐ์ผ๋ก ๊ตฌํํ ์์ touch๋ฅผ ์ด์ฉํ๋ค.
touchstart ํฐ์น ์์์
touchmove ํฐ์น ์ค์ผ ๋ ๊ณ์
touchend ํฐ์น ์ข
๋ฃ์
๐ ๋ชจ๋ฐ์ผ๋ก ๊ตฌํํ๊ธฐ
๊ฐ์ ๊ธฐ๋ฅ์ ๋ชจ๋ฐ์ผ๋ก ํฐ์นํ์ ๋๋ ๋์ผํ๊ฒ ์๋ํ๊ฒ ํ ๊ฒ์ด๋ค.
js
let start_m = 0;
let ifPress_m = false;
let moveX_m = 0;
document.querySelectorAll('.slide-box')[0].addEventListener('touchstart',function(e){
start_m=e.touches[0].clientX;
ifPress_m=true;
})
document.querySelectorAll('.slide-box')[0].addEventListener('touchmove',function(e){
if (ifPress_m){
moveX_m=e.touches[0].clientX - start_m;
document.querySelectorAll('.slide-box')[0].style.transform = `translateX(${moveX_m}px)`;
}
})
document.querySelectorAll('.slide-box')[0].addEventListener('touchend',function(e){
ifPress_m=false;
console.log(moveX_m);
if(moveX_m<-100){
document.querySelector(".slide-container").style.transform =
"translateX(-100vw)";
}else{
document.querySelector(".slide-container").style.transform =
"translateX(0vw)";
}
})
๊ธฐ์กด์ e.clientX๋ฅผ e.touches[0].clientX ์ด๋ ๊ฒ ์์ ํ๋ค.
์ ์๋ํ์๋ค.
๐ switch
let num = 2
switch (num){
case 1 :
alert('1์
๋๋ค');
break
case 2 :
alert('2์
๋๋ค');
break
default :
alert('1๋๋2๊ฐ ์๋๋๋ค')
}
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฌ์ฉ๋๋ค. break๋ฅผ ๊ฑธ์ด์ค์ผ ๋ค์ ์ผ์ด์ค๋ก ๋์ด๊ฐ์ง ์๋๋ค.
'๐ Front-End Study with me' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ES6 ์ ๋ฆฌ โก (3) | 2024.06.12 |
---|---|
ES6 ์ ๋ฆฌ โ (1) | 2024.06.08 |
Javascript ์ ๋ฆฌ โฅ (0) | 2024.05.17 |
Javascript ์ ๋ฆฌ โค (0) | 2024.05.01 |
Javascript ์ ๋ฆฌ โฃ (0) | 2024.04.22 |