Animated Rainbow Nyan Cat

๐Ÿ”Ž 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