FrontEnd/HTML | JS | CSS

[Vanilla JS] html, css, js ์Šฌ๋ผ์ด๋” ๊ตฌํ˜„ํ•˜๊ธฐ

ddonghyeo 2023. 9. 2. 00:02

HTML

  <div class="slider_bar">
    <div class="slider_button">
      <span id="slider_per">0%</span>
      <img src="{src}" id="slider" />
    </div>
  </div>

๊ฐ„๋‹จํ•˜๊ฒŒ slider bar ์œ„์— button์„ ๋†“์•˜๋‹ค. img๋Š” button ์•ˆ์— ์œ„์น˜ํ•œ๋‹ค.
span ์š”์†Œ๋Š” ๋ˆŒ๋ €์„ ๋•Œ ๋ช‡ ํผ์„ผํŠธ์ธ์ง€ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•จ์ด๋‹ค.



CSS

.slider_bar {
  display: flex;
  position: fixed;
  right: 5%;
  top: 35%;
  bottom: 20%;
  width: 2%;
  height: 40%;
  background: linear-gradient(to bottom, 
    rgba(176, 196, 238, 1) 20%,
    rgba(176, 196, 238, 0.75) 35%,
    rgba(176, 196, 238, 0.5) 60%,
    rgba(176, 196, 238, 0.25) 85%,
    rgba(176, 196, 238, 0) 100%);
  border-radius: 20px;
  justify-content: center;
}

.slider_bar img {
  height: 30px;
  width: 30px;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;
}

.slider_bar span {
  display: none;
  position: absolute;
  left: -50px;
  width: 40px;
  height: 30px;
  border-radius: 10px;
  color: #FFFFFF;
  background-color: #8e8989;
  padding: 0 auto;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  transition-duration: all 2s;
}

.slider_button {
  display: flex;
  width: 40px;
  height: 40px;
  position: absolute;
  bottom: 0;
  border-radius: 50%;
  background-color: #FFFFFF;
  text-align: center;
  font-weight: 900;
  color: gray;
  line-height: 7px;
  justify-content: center;
  align-items: center;

  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none
}

css์˜ ์ค‘์š”ํ•œ ์ ์€, slider button์˜ bottom๊ฐ’์„ ์ฃผ์–ด์„œ slider bar์—์„œ ์–ผ๋งˆ๋‚˜ ๋–จ์–ด์ ธ ์žˆ๋ƒ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜๋จธ์ง€๋Š” ์ž์œ ๋กญ๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.



JS

var slider_button = document.getElementsByClassName("slider_button")[0];
var percent = document.getElementById('slider_per');


//ํ„ฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ
slider_button.addEventListener("touchstart", (e) => {
  percent.style.display = "flex";  // ํ„ฐ์น˜๊ฐ€ ์‹œ์ž‘๋˜๋ฉด ํผ์„ผํŠธ ๋ณด์ด๊ธฐ
  startPoint = e.touches[0].pageY; // ํ„ฐ์น˜๊ฐ€ ์‹œ์ž‘๋˜๋Š” ์œ„์น˜ ์ €์žฅ
});




//ํ„ฐ์น˜๊ฐ€ ์›€์ง์ผ ๋•Œ
slider_button.addEventListener("touchmove", (e) => {

  //์›๋ž˜ bottom ๊ฐ’
  let origin = parseInt(getComputedStyle(slider_button).bottom.slice(0, -2));
  //์ตœ๊ณ  ๋†’์ด
  let max_height = parseInt(getComputedStyle(slider_bar).height);
  //์‹œ์ž‘ ์ง€์ ๊ณผ ์‚ฌ์šฉ์ž ํ„ฐ์น˜์™€์˜ ์‚ฌ์ด ๊ฐ’
  diff = parseInt(startPoint - e.touches[0].pageY);

  //
  if (((origin + (diff - pre)) > 0) && ((origin + (diff - pre)) <= max_height)) {
    slider_button.style.bottom = origin + (diff - pre) + "px"; // ์›๋ž˜ ๊ฐ’ + (์ฐจ์ด - ์ด์ „ ๊ฐ’)
    // + ์Šฌ๋ผ์ด๋”๊ฐ€ ์˜ฌ๋ผ๊ฐˆ ๋•Œ ์ƒ๊ธธ ๋ณ€ํ™”๋ฅผ ์—ฌ๊ธฐ์—
  }

  pre = diff; // ์ด์ „ ๊ฐ’ ์ €์žฅ

  // bottom ๊ฐ’
  let bottom_height = parseInt(getComputedStyle(slider_button).bottom);
  // ํผ์„ผํŠธ ๊ฐ’ ๋ณ€๊ฒฝ
  percent.innerText = parseInt(bottom_height / parseInt(max_height) * 100) + "%";
});


//ํ„ฐ์น˜๊ฐ€ ๋๋‚ฌ์„ ๋•Œ
slider_button.addEventListener("touchend", (e) => {
  pre = 0;   //์ด์ „ ๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
  percent.style.display = "none"; //ํผ์„ผํŠธ ๊ฐ’ ๊ฐ€๋ฆฌ๊ธฐ
});
  1. ํ„ฐ์น˜๊ฐ€ ์‹œ์ž‘๋˜๋ฉด ์‹œ์ž‘๋œ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.
  2. ํ„ฐ์น˜๊ฐ€ ์›€์ง์ด๋ฉด, ํ„ฐ์น˜ ์‹œ์ž‘ ์ ๊ณผ ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ํ„ฐ์น˜ํ•˜๊ณ  ์žˆ๋Š” Y๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ๊ทธ ์ฐจ์ด๋ฅผ ์ €์žฅํ•œ๋‹ค.
  3. ๋‹ค์Œ์„ ๊ฒ€์‚ฌํ•œ๋‹ค :
    3-1. ์›๋ž˜ bottom ๊ฐ’ + (์ฐจ์ด - ์ด์ „ ๊ฐ’) ์ด 0๋ณด๋‹ค ํฐ์ง€
    3-2. ์›๋ž˜ bottom ๊ฐ’ + (์ฐจ์ด - ์ด์ „ ๊ฐ’) ์ด ์ตœ๊ณ  ๋†’์ด๋ณด๋‹ค ์ž‘์€ ์ง€
    -> ํ•œ ๋งˆ๋””๋กœ ์Šฌ๋ผ์ด๋” ๋ฒ„ํŠผ์ด ์žˆ์„ ์œ„์น˜๊ฐ€ ์Šฌ๋ผ์ด๋” ๋ฐ” ์•ˆ์— ์žˆ์–ด๋„ ๋˜๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค.
  4. ๊ฒ€์‚ฌ๊ฐ€ ํ†ต๊ณผ๋˜๋ฉด ์Šฌ๋ผ์ด๋” ๋ฒ„ํŠผ์˜ bottom ๊ฐ’์„ ์›๋ž˜ ๊ฐ’ + (์ฐจ์ด - ์ด์ „ ๊ฐ’) px๋งŒํผ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  5. ์ด์ „ ๊ฐ’ ๋ณ€์ˆ˜์— ํ˜„์žฌ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.
  6. ํ„ฐ์น˜๊ฐ€ ๋๋‚˜๋ฉด, ์ด์ „ ๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ํผ์„ผํŠธ ๊ฐ’์„ ๊ฐ€๋ฆฐ๋‹ค.

๋‚ด ๊ฒฝํ—˜ ์ƒ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๊ฐ€์žฅ ๊น”๋”ํ•˜๊ฒŒ ์›€์ง์ด๊ณ  ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿ˜‚