문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:javascript:javascript_note:classlist_property [2021/06/08 17:16] emblim98 [Fallback Exmaple: remove] |
wiki:javascript:javascript_note:classlist_property [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 686: | 줄 686: | ||
| IE9 및 이전 버전에서 %%classList.**contains()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | IE9 및 이전 버전에서 %%classList.**contains()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| <code javascript> | <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</ | ||
| + | </ | ||
| </ | </ | ||
| 줄 692: | 줄 728: | ||
| IE9 및 이전 버전에서 %%classList.**toggle()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | IE9 및 이전 버전에서 %%classList.**toggle()**%% 메서드를 사용하는 경우를 위한 크로스-브라우저 솔루션: | ||
| <code javascript> | <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</ | ||
| + | </ | ||
| </ | </ | ||
| 줄 698: | 줄 780: | ||
| 스티키 네비게이션 바 (sticky navigation bar) 만들기\\ | 스티키 네비게이션 바 (sticky navigation bar) 만들기\\ | ||
| <code javascript> | <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(' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||