๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Lect & Tip/javascript, jQuery

javascript classList.toggle๋กœ jquery addClass, removeClass, toggleClass ๊ตฌํ˜„ํ•˜๊ธฐ / ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์˜์—ญ ํ™•์žฅ

by ๋‚ฏ์„ ๊ณต๊ฐ„2019 2022. 11. 2.

๋ชฉ์ฐจ

    Javascript classList.toggle๋กœ jQuery addClass, removeClass, toggleClass ๊ตฌํ˜„ํ•˜๊ธฐ / ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์˜์—ญ ํ™•์žฅ

    ์„œ๋ก 

    Javascript์™€ React์—์„œ ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋‚ด์šฉ์ด ํ™•์žฅ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

    ์ด๋ฅผ ์œ„ํ•ด์„œ jQuery๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜์ง€๋งŒ, React์—์„œ๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ref์™€ useState๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…์—์„œ๋Š” classList.toggle ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ jQuery์˜ addClass, removeClass, toggleClass๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์˜์—ญ์„ ํ™•์žฅํ•˜๋Š” ์˜ˆ์ œ๋„ ํ•จ๊ป˜ ์ œ์‹œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    classList.toggle๋กœ addClass, removeClass, toggleClass ๊ตฌํ˜„ํ•˜๊ธฐ

    React์—์„œ classList.toggle ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ jQuery์˜ addClass, removeClass, toggleClass๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. classList๋Š” DOM ์š”์†Œ์˜ ํด๋ž˜์Šค๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. toggle ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ œ๊ฑฐํ•˜๊ณ , ์—†์œผ๋ฉด ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค๋ฅผ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ํ† ๊ธ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    import React, { useState } from 'react';
    import './styles.css';
    
    const ExtendClass = (e) => {
      e.target.parentElement.classList.toggle("extend");
    };
    
    const ExampleComponent = () => {
      return (
        <div className="person-detail-form-body">
          <h5>๋„์„œ</h5>
          <div className="extender-wrap">
            <ul className="history-list medium-list">
              <li>๊ธ€๊ธ€๊ธ€</li>
              <li>๊ธ€๊ธ€๊ธ€</li>
              <li>๊ธ€๊ธ€๊ธ€</li>
              <li>๊ธ€๊ธ€๊ธ€</li>
              <li>๊ธ€๊ธ€๊ธ€</li>
              <li>๊ธ€๊ธ€๊ธ€</li>
            </ul>
            <button
              className="btn-extender"
              onClick={(e) => {
                ExtendClass(e);
              }}
            ></button>
          </div>
        </div>
      );
    };

    CSS๋กœ ์˜์—ญ ํ™•์žฅ ์ œ์–ดํ•˜๊ธฐ

    ์œ„ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋œ CSS๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. extender-wrap ํด๋ž˜์Šค๋Š” max-height๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ๋‚ด์šฉ์„ ์ˆจ๊ธฐ๊ณ  ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

    .extender-wrap {
      margin-bottom: 20px;
      &:not(.extend) {
        & > ul {
          max-height: 100px;
          overflow: hidden;
        }
      }
      & > ul {
        margin-bottom: 5px;
      }
    }

    ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ๋””์ž์ธ

    ๋ฒ„ํŠผ์€ ๋ถ€๋ชจ ์š”์†Œ์˜ ์ž์‹์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ, ๋ฒ„ํŠผ์˜ ์ด๋ฒคํŠธ์—์„œ ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ ํด๋ž˜์Šค๋ฅผ ํ† ๊ธ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„ํŠผ์˜ ํ™”์‚ดํ‘œ๋Š” CSS transform ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋’ค์ง‘์–ด ์ค๋‹ˆ๋‹ค.

    .btn-extender {
      width: 100%;
      display: flex;
      border: 0 none;
      justify-content: center;
      align-items: center;
      &::after {
        font-family: sans-serif;
        font-size: 1.05rem;
        content: "ใ€‰";
        transform: rotate(90deg);
      }
      .extend &::after {
        transform: rotate(-90deg);
      }
    }

    ๋งˆ๋ฌด๋ฆฌ

    ์œ„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด classList.toggle ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ jQuery์˜ addClass, removeClass, toggleClass๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, React์—์„œ ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์˜์—ญ์„ ํ™•์žฅํ•˜๋Š” ์˜ˆ์ œ๋„ ์ œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ๊ธฐ์ˆ ์„ ํ™œ์šฉํ•˜์—ฌ ๋” ๋‹ค์–‘ํ•œ ์›น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

    ์ฐธ๊ณ  ์ž๋ฃŒ

    • MDN Web Docs: Element.classList
     

    Element: classList property - Web APIs | MDN

    The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list.

    developer.mozilla.org

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€