[miniProjects] 44_Custom Range Slider

๋ณด๋ฆฌยท2023๋…„ 8์›” 8์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
44/47

44_Custom Range Slider

๐Ÿ’ป ์ฃผ์ œ : 0 ~ 100๊นŒ์ง€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ”์œ„ ๊ฐ’์„ ์กฐ์ •ํ•  ๋•Œ๋งˆ๋‹ค "input" ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”์˜ ์œ„์น˜์™€ ๋‚ด์šฉ์„ ์—…๋ฐ์ดํŠธ ํ•จ.

โœจJS

const range = document.getElementById('range')

range.addEventListener('input', (e) => {
  // ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์•ž์— +๋ฅผ ๋ถ™์ž„
  // e.target์€ input
  const value = +e.target.value
  // e.target์˜ ๋‹ค์Œ ํ˜•์ œ๋Š” label์ž„
  const label = e.target.nextElementSibling

  const range_width = getComputedStyle(e.target).getPropertyValue('width')
  const label_width = getComputedStyle(label).getPropertyValue('width')

  //2๋Š” p,x
  const num_width = +range_width.substring(0, range_width.length - 2)
  const num_label_width = +label_width.substring(0, label_width.length - 2)

  const max = +e.target.max
  const min = +e.target.min

  const left = value * (num_width / max) - num_label_width / 2 + scale(value, min, max, 10, -10)
  label.style.left = `${left}px`


  label.innerHTML = value
})


// https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers
const scale = (num, in_min, in_max, out_min, out_max) => {
    return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  }

https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers

  • ๋ ˆ์ด๋ธ”(label)์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด width๋ฅผ ์•Œ์•„์•ผ ํ•จ. ํ•ด๋‹น ์ฝ”๋“œ์—์„œ "range" ์ž…๋ ฅ ์š”์†Œ์˜ ๊ฐ’์„ ์กฐ์ •ํ•˜๋ฉด, ๋ ˆ์ด๋ธ”์˜ ์œ„์น˜๋ฅผ ํ•ด๋‹น ๊ฐ’์— ๋งž์ถ”์–ด ์กฐ์ •๋˜์–ด์•ผ ํ•œ๋‹ค. ๋ ˆ์ด๋ธ”์ด ์–ด๋””์— ์œ„์น˜ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ์— width๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • range_width์™€ label_width๋ฅผ ๊ฐ€์ ธ์™€์„œ ๊ฐ๊ฐ "range" ์š”์†Œ์™€ "label" ์š”์†Œ์˜ ๋„ˆ๋น„๋ฅผ ๊ตฌํ•œ๋‹ค.
    ์ด๋•Œ ๊ฐ€์ ธ์˜จ ๋„ˆ๋น„ ๊ฐ’์€ "px" ๋‹จ์œ„๋ฅผ ํฌํ•จํ•˜๋ฏ€๋กœ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜์—ฌ num_width์™€ num_label_width ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.

    getComputedStyle

    JavaScript์˜ DOM(Document Object Model)์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ์›น ํŽ˜์ด์ง€์—์„œ ์‹ค์ œ๋กœ ํ‘œ์‹œ๋˜๋Š” ์š”์†Œ์˜ ๊ณ„์‚ฐ๋œ ์Šคํƒ€์ผ์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด CSS ์Šคํƒ€์ผ ๊ทœ์น™์ด ์ ์šฉ๋œ ํ›„์˜ ์š”์†Œ์˜ ์Šคํƒ€์ผ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

  • value, max, min, num_width, num_label_width๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”์˜ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

๐Ÿ˜ถleft ๊ณ„์‚ฐ

  1. value * (num_width / max)
    ํ˜„์žฌ "range" ์ž…๋ ฅ ์š”์†Œ์˜ ๊ฐ’์„ ์ตœ๋Œ€ ๊ฐ’(max)์œผ๋กœ ๋‚˜๋ˆˆ ๋น„์œจ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ๋น„์œจ์€ ์ž…๋ ฅ ๊ฐ’์ด ์ตœ๋Œ€ ๊ฐ’์— ๋Œ€ํ•ด ์–ผ๋งˆ๋‚˜ ํฐ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด ๋น„์œจ์€ 0๋ถ€ํ„ฐ 1 ์‚ฌ์ด์˜ ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค.

  2. num_label_width / 2
    ๋ ˆ์ด๋ธ”์˜ ๋„ˆ๋น„(num_label_width)๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’. ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋ ˆ์ด๋ธ”์ด "range" ์ž…๋ ฅ ์š”์†Œ์˜ ์ค‘๊ฐ„ ์œ„์น˜๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ฐฐ์น˜๋œ๋‹ค.

  3. scale(value, min, max, 10, -10)
    ์ž…๋ ฅ ๊ฐ’์„ ์ž…๋ ฅ ๋ฒ”์œ„(min์—์„œ max)์—์„œ ์ถœ๋ ฅ ๋ฒ”์œ„(10์—์„œ -10)๋กœ ๋ณ€ํ™˜ํ•˜๋Š” scale() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’์˜ ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๐Ÿ‘๐Ÿป"range" ์ž…๋ ฅ ๊ฐ’์— ๋”ฐ๋ผ ๋ ˆ์ด๋ธ”์˜ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ ์œ„์น˜๋ฅผ style.left ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ์ •ํ•จ์œผ๋กœ์จ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๊ฐ’์— ๋”ฐ๋ผ ๋ ˆ์ด๋ธ”์ด ์›€์ง์ด๊ฒŒ ๋œ๋‹ค.

profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€