문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:javascript:javascript_note:classlist_property [2021/06/08 14:13] emblim98 [Example] |
wiki:javascript:javascript_note:classlist_property [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 398: | 줄 398: | ||
| 요소가 " | 요소가 " | ||
| <code javascript> | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 500px; | ||
| + | height: 50px; | ||
| + | border: 1px solid black; | ||
| + | } | ||
| + | .anotherClass { | ||
| + | background-color: | ||
| + | padding: 25px; | ||
| + | } | ||
| + | |||
| + | .thirdClass { | ||
| + | text-align: center; | ||
| + | font-size: 25px; | ||
| + | color: navy; | ||
| + | margin-bottom: | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | |||
| + | <div id=" | ||
| + | I am a DIV element with three classes.</ | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | < | ||
| + | in Internet Explorer 9 and earlier versions.</ | ||
| + | |||
| + | <p id=" | ||
| + | <!-- true --> | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementById(' | ||
| + | document.getElementById(' | ||
| + | } | ||
| + | /* 버튼을 클릭하면 id=" | ||
| + | 있으니까 true를 반환한다.. */ | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||
| + | |||
| + | ====Example==== | ||
| + | 요소에 " | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 500px; | ||
| + | height: 50px; | ||
| + | border: 1px solid black; | ||
| + | } | ||
| + | |||
| + | .anotherClass { | ||
| + | background-color: | ||
| + | padding: 25px; | ||
| + | } | ||
| + | |||
| + | .thirdClass { | ||
| + | text-align: center; | ||
| + | font-size: 25px; | ||
| + | color: navy; | ||
| + | margin-bottom: | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | If so, remove " | ||
| + | |||
| + | <div id=" | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | < | ||
| + | in Internet Explorer 9 and earlier versions.</ | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementById(' | ||
| + | |||
| + | if (x.classList.contains(' | ||
| + | x.classList.remove(' | ||
| + | } else { | ||
| + | alert(' | ||
| + | } | ||
| + | } | ||
| + | /* 버튼을 클릭하면 mystyle이 들어있는지 확인하고, | ||
| + | <div id=" | ||
| + | anotherClass를 제거해서 <div id=" | ||
| + | 배경색과 패딩이 사라짐 */ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Example==== | ||
| + | 클래스 사이의 토글 기능으로 드롭다운 버튼을 생성합니다.\\ | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .dropbtn { | ||
| + | background-color: | ||
| + | color: white; | ||
| + | padding: 16px; | ||
| + | font-size: 16px; | ||
| + | border: none; | ||
| + | cursor: pointer; | ||
| + | } | ||
| + | |||
| + | .dropbtn: | ||
| + | .dropbtn: | ||
| + | background-color: | ||
| + | } | ||
| + | |||
| + | .dropdown { | ||
| + | position: relative; | ||
| + | display: inline-block; | ||
| + | } | ||
| + | |||
| + | .dropdown-content { | ||
| + | display: none; | ||
| + | position: absolute; | ||
| + | background-color: | ||
| + | min-width: 160px; | ||
| + | overflow: auto; | ||
| + | box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2); | ||
| + | } | ||
| + | |||
| + | .dropdown-content a { | ||
| + | color: black; | ||
| + | padding: 12px 16px; | ||
| + | text-decoration: | ||
| + | display: block; | ||
| + | } | ||
| + | |||
| + | .dropdown-content a:hover { | ||
| + | background-color: | ||
| + | } | ||
| + | |||
| + | .show { | ||
| + | display: block; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | |||
| + | <div class=" | ||
| + | <button id=" | ||
| + | <div id=" | ||
| + | <a href="# | ||
| + | <a href="# | ||
| + | <a href="# | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | // Get the button, and when the user clicks on it, execute myFunction | ||
| + | document.getElementById(' | ||
| + | myFunction(); | ||
| + | }; | ||
| + | |||
| + | /* myFunction toggles between adding and removing the show class, | ||
| + | which is used to hide and show the dropdown content */ | ||
| + | function myFunction() { | ||
| + | document.getElementById(' | ||
| + | } | ||
| + | /* 버튼을 클릭하면 <div id=" | ||
| + | <div id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Fallback Example: add==== | ||
| + | IE9 및 이전 버전에서 %%classList.**add()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 300px; | ||
| + | height: 50px; | ||
| + | background-color: | ||
| + | color: white; | ||
| + | font-size: 25px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p>In this example, we check if the browser supports the classList.add() method. | ||
| + | If not, use the className property instead to achieve the same result | ||
| + | (for IE9 and earlier).</ | ||
| + | |||
| + | < | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | <div id=" | ||
| + | |||
| + | <p id=" | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x, name, arr; | ||
| + | x = document.getElementById(' | ||
| + | |||
| + | if (x.classList) { | ||
| + | x.classList.add(' | ||
| + | } else { | ||
| + | name = ' | ||
| + | arr = x.className.split(' | ||
| + | if (arr.indexOf(name) == -1) { | ||
| + | x.className += ' ' + name; | ||
| + | } | ||
| + | } | ||
| + | console.log(x.classList); | ||
| + | console.log(x.classList.add(' | ||
| + | console.log(x.className); | ||
| + | console.log(x.className.split(' | ||
| + | console.log(myDIV); | ||
| + | console.log(x); | ||
| + | console.log(name); | ||
| + | console.log(arr); | ||
| + | console.log((arr = x.className)); | ||
| + | console.log(arr.indexOf(name)); | ||
| + | console.log(arr.indexOf(name) == -1); // true | ||
| + | } | ||
| + | /* indexOf() 메서드는 검색할 값이 발생하지 않으면 -1을 반환합니다. */ | ||
| + | /* split() 메서드는 문자열을 하위 문자열 배열로 분할하는데 사용되며 새 배열을 반환합니다. */ | ||
| + | /* | ||
| + | function myFunction() { | ||
| + | document.getElementById(' | ||
| + | } | ||
| + | */ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Fallback Exmaple: remove==== | ||
| + | IE9 및 이전 버전에서 %%classList.**remove()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 300px; | ||
| + | height: 50px; | ||
| + | background-color: | ||
| + | color: black; | ||
| + | font-size: 25px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p>In this example, we check if the browser supports the classList.remove() method. | ||
| + | If not, the regular expression works as a fallback to achieve the same result | ||
| + | (for IE9 and earlier).</ | ||
| + | |||
| + | < | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | <div id=" | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementById(' | ||
| + | if (x.classList) { | ||
| + | x.classList.remove(' | ||
| + | } else { | ||
| + | x.className = x.className.replace(/ | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | <p>In JavaScript, the boolean value of undefined is false</ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Fallback Example: contains==== | ||
| + | IE9 및 이전 버전에서 %%classList.**contains()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 300px; | ||
| + | height: 50px; | ||
| + | background-color: | ||
| + | color: white; | ||
| + | font-size: 25px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p>In this example, we check if the browser supports the classList.contains() method. If not, the regular expression works as a fallback to achieve the same result (for IE9 and earlier).</ | ||
| + | |||
| + | < | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | <div id=" | ||
| + | |||
| + | <p id=" | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementById(' | ||
| + | |||
| + | if (x.classList) { | ||
| + | alert(x.classList.contains(' | ||
| + | } else { | ||
| + | alert(/ | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | <p>In JavaScript, the boolean value of undefined is false</ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Fallback Example: toggle==== | ||
| + | IE9 및 이전 버전에서 %%classList.**toggle()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | .mystyle { | ||
| + | width: 300px; | ||
| + | height: 50px; | ||
| + | background-color: | ||
| + | color: white; | ||
| + | font-size: 25px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p> | ||
| + | In this example, we check if the browser supports the classList.toggle() method. | ||
| + | If not, use the className property together with other JS properties and methods | ||
| + | to achieve the same result (for IE9). | ||
| + | </p> | ||
| + | |||
| + | < | ||
| + | the same result (for IE9)</ | ||
| + | |||
| + | <button onclick=" | ||
| + | |||
| + | <div id=" | ||
| + | |||
| + | <p id=" | ||
| + | |||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementById(' | ||
| + | |||
| + | if (x.classList) { | ||
| + | x.classList.toggle(' | ||
| + | } else { | ||
| + | var classes = x.className.split(' | ||
| + | var i = classes.indexOf(' | ||
| + | |||
| + | if (i >= 0) classes.splice(i, | ||
| + | else classes.push(' | ||
| + | x.className = classes.join(' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | <p>In JavaScript, the boolean value of undefined is false</ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====Example==== | ||
| + | 스티키 네비게이션 바 (sticky navigation bar) 만들기\\ | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | body { | ||
| + | margin: 0; | ||
| + | font-size: 28px; | ||
| + | } | ||
| + | |||
| + | .header { | ||
| + | background-color: | ||
| + | padding: 30px; | ||
| + | text-align: center; | ||
| + | } | ||
| + | |||
| + | #navbar { | ||
| + | overflow: hidden; | ||
| + | background-color: | ||
| + | } | ||
| + | |||
| + | #navbar a { | ||
| + | float: left; | ||
| + | display: block; | ||
| + | color: #f2f2f2; | ||
| + | text-align: center; | ||
| + | padding: 14px 16px; | ||
| + | text-decoration: | ||
| + | font-size: 17px; | ||
| + | } | ||
| + | |||
| + | #navbar a:hover { | ||
| + | background-color: | ||
| + | color: #000; | ||
| + | } | ||
| + | |||
| + | #navbar a.active { | ||
| + | background-color: | ||
| + | color: white; | ||
| + | } | ||
| + | |||
| + | .content { | ||
| + | padding: 16px; | ||
| + | } | ||
| + | |||
| + | .sticky { | ||
| + | position: fixed; | ||
| + | top: 0; | ||
| + | width: 100%; | ||
| + | } | ||
| + | |||
| + | .sticky + .content { | ||
| + | padding-top: | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | <body onscroll=" | ||
| + | <div class=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <div id=" | ||
| + | <a href=" | ||
| + | <a href=" | ||
| + | <a href=" | ||
| + | </ | ||
| + | |||
| + | <div class=" | ||
| + | < | ||
| + | < | ||
| + | <p> | ||
| + | Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, | ||
| + | maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, | ||
| + | id agam omnis evertitur eum. Affert laboramus repudiandae nec et. | ||
| + | Inciderint efficiantur his ad. Eum no molestiae voluptatibus. | ||
| + | </p> | ||
| + | <p> | ||
| + | Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, | ||
| + | maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, | ||
| + | id agam omnis evertitur eum. Affert laboramus repudiandae nec et. | ||
| + | Inciderint efficiantur his ad. Eum no molestiae voluptatibus. | ||
| + | </p> | ||
| + | <p> | ||
| + | Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, | ||
| + | maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, | ||
| + | id agam omnis evertitur eum. Affert laboramus repudiandae nec et. | ||
| + | Inciderint efficiantur his ad. Eum no molestiae voluptatibus. | ||
| + | </p> | ||
| + | <p> | ||
| + | Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, | ||
| + | maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, | ||
| + | id agam omnis evertitur eum. Affert laboramus repudiandae nec et. | ||
| + | Inciderint efficiantur his ad. Eum no molestiae voluptatibus. | ||
| + | </p> | ||
| + | <p> | ||
| + | Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, | ||
| + | maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, | ||
| + | id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur | ||
| + | his ad. Eum no molestiae voluptatibus. | ||
| + | </p> | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | // Get the navbar | ||
| + | var navbar = document.getElementById(' | ||
| + | |||
| + | // Get the offset position of the navbar | ||
| + | var sticky = navbar.offsetTop; | ||
| + | |||
| + | // Add the sticky class to the navbar when you reach its scroll position. | ||
| + | Remove the sitcky class when you leave the scroll position. | ||
| + | function myFunction() { | ||
| + | if (window.pageYOffset >= sticky) { | ||
| + | navbar.classList.add(' | ||
| + | } else { | ||
| + | navbar.classList.remove(' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||