๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+48] Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 4์ผ์ฐจ : ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ถ”๊ฐ€

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 11. 28.

 

๊ตญ๋น„์ง€์› D+48

Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 4์ผ์ฐจ

- ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ถ”๊ฐ€ -

 


 

์˜ค๋Š˜ ํ•œ ์ผ

- ์Šคํฌ๋กค์œ„์น˜์— ๋”ฐ๋ผ ํ˜„์žฌ์„น์…˜ ์œ„์น˜์™€ ์„น์…˜์œ„์น˜์— ๋”ฐ๋ฅธ Y์˜ ์ƒ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋„๋ก ์„ค์ •

- section0์— ๋Œ€ํ•œ ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ถ”๊ฐ€ ๋ฐ ํ…Œ์ŠคํŠธ

 

ํŒŒ์ผ ๊ตฌ์„ฑ ๋ฐ ๋‚ด์šฉ

 


• style.css

- ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜์—์„œ ๋‚˜ํƒ€๋‚ฌ๋‹ค ์‚ฌ๋ผ์กŒ๋‹ค๋ฅผ ์œ„ํ•ด์„œ ์ดˆ๊ธฐ์— ๋ถˆํˆฌ๋ช…๋„๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.

.section0-message {
  opacity: 0;		/* ๋ถˆํˆฌ๋ช…๋„ : 0 */
  font-size: 4.5rem;
  font-weight: bold;
  width: 100%;
  height: 6rem;
  left: 0;
  top: 5em;
  text-align: center;
}

 

• index.html

- ๊ฐ๋ฉ”์‹œ์ง€๋งˆ๋‹ค ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ํด๋ž˜์Šค๋ช…์„ ๊ฐ๊ฐ a, b, c, d๋กœ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

<section id="section-0" class="scroll-section">
    <div class="section0-product-name">
      <h1 class="product-name">Real Apple Pro</h1>
    </div>
    <div class="sticky-element section0-message a">
      <p>์˜จ์ „ํžˆ ๋น ์ ธ๋“ค๊ฒŒ ํ•˜๋Š” ๋น›๊น”</p>
    </div>
    <div class="sticky-element section0-message b">
      <!-- <p>๋‚˜์˜ ๊ฑด๊ฐ•์„ ์ฑ„์›Œ์ฃผ๋Š” ํ–ฅ๊ธฐ</p> -->
    </div>
    <div class="sticky-element section0-message c">
      <!-- <p>๊นจ๋—ํ•œ ์ž์—ฐ์˜ ์‹ ์„ ํ•จ</p> -->
    </div>
    <div class="sticky-element section0-message d">
      <!-- <p>์ƒˆ๋กญ๊ฒŒ ์ž…๊ฐ€๋ฅผ ์ฐพ์•„์˜จ ๋งคํ˜น</p> -->
    </div>
</section>

 

• main.js

- ์Šคํฌ๋กค์ด๋ฒคํŠธ ์ถ”๊ฐ€๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

- getPrevSetionHeight : ํ˜„์žฌ ์„น์…˜์˜ ์œ„์— ์žˆ๋Š” ์„น์…˜์˜ ๋†’์ด์˜ ํ•ฉ

- playAnimation : ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋™์ž‘ ํ•จ์ˆ˜

- calcValue : ๋น„์œจ์—๋”ฐ๋ฅธ CSS๊ฐ’์„ ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

// ํ˜„์žฌ ์„น์…˜์˜ ์œ„์— ์žˆ๋Š” ์„น์…˜์˜ ๋†’์ด ํ•ฉ.
const getPrevSectionHeight = function()
{
  // ํ˜„์žฌ ์„น์…˜์ด 0 ==> 0
  // ํ˜„์žฌ ์„น์…˜ 1 ==> section0์˜ ๋†’์ด
  // ํ˜„์žฌ ์„น์…˜ 2 ==> section0์˜ ๋†’์ด + section1์˜ ๋†’์ด
  let prevHeight = 0;

  for(let i = 0; i < currentSection; i++)
  {
      prevHeight = prevHeight + sectionSet[i].height;
  }

  return prevHeight;
}

// messageA_opacity_out : [0, 1, {start: 0.05, end: 0.14}]
const calcValue = function(values)
{
  let result = 0;
  let rate = 0;
  const height = sectionSet[currentSection].height;

  let partStart;  // start ์Šคํฌ๋กค๊ฐ’
  let partEnd;    // end ์Šคํฌ๋กค๊ฐ’ 
  let partHeihgt; // end - start

  if(values.length === 2)
  {
      // 1. ์Šคํฌ๋กค ๋น„์œจ์„ ๊ตฌํ•œ๋‹ค.
      rate = sectionYOffset / height;

       // 2. ๋น„์œจ์— ๋”ฐ๋ฅธ ์‹ค์ œ ์ ์šฉํ•œ CSS๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค.
      result = (rate * (values[1] - values[0])) + values[0];
  }

  else if(values.length === 3)
  {
      // step01) ๋ชจ๋“  ๊ฐ’๋“ค์„ ๋น„์œจ์ด ์•„๋‹Œ ์‹ค์ œํ™” ํ•œ๋‹ค.
      partStart =  values[2].start * height;  
      partEnd = values[2].end * height;
      partHeihgt = partEnd - partStart;

      // ์„ค์ •ํ•œ ๋ฒ”์œ„๊ฐ’์„ ๋ฒ—์–ด๋‚œ ๊ฐ’์„ 0๋˜๋Š” 1๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.
      if(sectionYOffset < partStart)
      {
          result = values[0];
      }
      else if(sectionYOffset > partEnd)
      {
          result = values[1];
      }
      else
      {
          // step02) sectionYOffset์—์„œ partStart๊ฐ’์„ ๋นผ๋‚ด์–ด ์‹ค์ œ ๋ฒ”์œ„๋‚ด์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๊ณ 
          // ๋ฒ”์œ„๊ฐ„์˜ ๋น„์œจ์„ ๊ตฌํ•œ๋‹ค.
          rate = (sectionYOffset - partStart) / partHeihgt;

          // step03) ๊ฐ’์„ cssํ™” ํ•˜์—ฌ result์•ˆ์— ๋„ฃ์–ด์ค€๋‹ค.
          result = (rate * (values[1] - values[0])) + values[0];
      }
  }

  return result;
}

const playAnimation = function()
{
  let value;

  let scrollRate = sectionYOffset / sectionSet[currentSection].height;
  let objects = sectionSet[currentSection].objs;

  switch(currentSection)
  {
      case 0:
          
          if(scrollRate <= 0.25)
          {
              if(scrollRate <= sectionSet[currentSection].vals.messageA_opacity_out[2].end)
              {
                  value = calcValue(sectionSet[currentSection].vals.messageA_opacity_out);
              }
              else if(scrollRate > sectionSet[currentSection].vals.messageA_opacity_out[2].end)
              {
                  value = calcValue(sectionSet[currentSection].vals.messageA_opacity_in);
              }
              console.log('message A๋ฅผ ์• ๋‹ˆ๋ฉ”์ด์…˜');
          }
          else if((scrollRate > 0.25) && (scrollRate <= 0.5))
          {
              value = calcValue(sectionSet[currentSection].vals);    
              console.log('message B๋ฅผ ์• ๋‹ˆ๋ฉ”์ด์…˜');
          }
          else if((scrollRate > 0.5) && (scrollRate <= 0.75))
          {
              value = calcValue(sectionSet[currentSection].vals);    
              console.log('message C๋ฅผ ์• ๋‹ˆ๋ฉ”์ด์…˜');
          }
          else if((scrollRate > 0.75) && (scrollRate <= 1))
          {
              value = calcValue(sectionSet[currentSection].vals);    
              console.log('message D๋ฅผ ์• ๋‹ˆ๋ฉ”์ด์…˜');
          }

          // 1. messageA์˜ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. --> ๋ฌดํ•œ๋ฐ˜๋ณต ์šฐ๋ ค ์œ„์—์„œํ•œ๋‹ค.

          break;
      
      case 1:
          // 2. ์Šคํฌ๋กค ๊ฐ’์— ๋”ฐ๋ผ ์ ์šฉํ•ด์•ผํ•  CSS๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค.
          // 3. ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ ์šฉํ•œ๋‹ค.
          break;
      
      default:
          break;
      
      
  }

  objects.messageA.style.opacity = value;
}

 


๊ฒฐ๊ณผํ™”๋ฉด

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€