• 회원가입
  • 로그인
  • 구글아이디로 로그인

[DOM_Element] JS - classList 속성 ★★ - 요소의 클래스리스트. (= classList속성 = 클리스리스트속성) (IE10) ※ 상단슬라이드모달 ※ 지정요소에 클래스 추가

목차
  1. className vs. classList.add() 차이 
  2. classList 예제 - 클릭 시 .hz 클래스 추가
  3. classList 예제 - 클릭 시 (보이기/숨기기) 토글 처리
  4. classList 예제 - 위에서 미끄러져 내러오는 모달 (= 상단 슬라이드 모달)


className vs. classList.add() 차이

 

Element.className = 'class names'

기존에 class가 없으면 추가해 넣고, 있으면 기존 것을 변경.

 

Element.classList.add('class names')

기존 class에 새로운 클래스를 추가.

 

 

classList 예제 - 클릭 시 .hz 클래스 추가

 

<style>

.hz {

  width: 300px;

  line-height: 50px;

  background-color: tomato;

  color: white;

  font-size: 25px;

  text-align:center;

}

</style>


<button onclick="homzzang()">클릭</button>


<div id="box">홈짱닷컴 Homzzang.com</div>


<script>

function homzzang() {

  document.getElementById("box").classList.add("hz");

}

</script>

 

결과보기

 

classList 예제 - 클릭 시 (보이기/숨기기) 토글 처리

 

<style>

div {display:none}

div.active {display:block;}

</style>


<button>클릭</button>

<div>홈짱닷컴 Homzzang.com</div>


<script>

const btn = document.querySelector('button');

const div = document.querySelector('div');

btn.addEventListener('click', ()=>{

  div.classList.toggle('active');

});

</script>

 

결과보기

주의: script 부분이 태그 위로 올라가면 작동 X

PS. 엘리 님 강의 https://youtu.be/X91jsJyZofw

 

classList 예제 - 위에서 미끄러져 내러오는 모달 (= 상단 슬라이드 모달)

 

<style>

html, body {

  padding: 0px;

  margin: 0px;

  background: #444;

  font-family: 'Ubuntu', sans-serif;

  color: #FFF;

  width: 100%;

  height: 100%;

}


body {

  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/545665/teddy-kelley-70636.jpg");

  background-position: center;

  background-size: cover;

}


.clearfix *:after {

  content: '';

  clear: both;

  display: table;

}


* {

  box-sizing: border-box;

}


.modal {

  position: absolute;

  top: 0px;

  left: 0px;

  width: 100%;

  height: 100%;

  opacity: 0;

  transition: opacity 0.5s;

  -webkit-animation: 1s hide infinite;

          animation: 1s hide infinite;

  -webkit-animation-delay: 0.5s;

          animation-delay: 0.5s;

}

.modal:target, .modal.force {

  -webkit-transform: translateY(0px);

          transform: translateY(0px);

  opacity: 1;

  -webkit-animation: none;

          animation: none;

}

.modal:target .view, .modal.force .view {

  transition: all 1s eas-in-out;

  -webkit-transform: translateY(0px);

          transform: translateY(0px);

}

.modal .background {

  position: absolute;

  width: 100%;

  height: 100%;

  position: absolute;

  left: 0px;

  top: 0px;

  background: #000;

  opacity: 0.5;

}

.modal .view-container {

  padding: 20px;

}

.modal .view {

  transition: all 1s ease-in-out;

  -webkit-transform: translateY(-100%);

          transform: translateY(-100%);

  position: relative;

  margin: 50px auto;

  max-width: 600px;

  border: 2px solid #FFF;

  border-radius: 2px;

  -webkit-backdrop-filter: blur(8px);

          backdrop-filter: blur(8px);

  background: rgba(0, 0, 0, 0.5);

}

.modal .view .content {

  position: relative;

  min-height: 100px;

  padding: 10px;

}

.modal .view .header {

  padding: 16px 10px;

  border-radius: 3px 3px 0px 0px;

  border-bottom: 2px solid #FFF;

  background: rgba(0, 0, 0, 0.75);

}

.modal .view .header * {

  margin: 0px;

}

.modal .view .header a.close {

  position: absolute;

  right: 10px;

  top: 7px;

  text-decoration: none;

  color: #FFF;

  -webkit-transform: rotateZ(45deg);

          transform: rotateZ(45deg);

  -webkit-transform-style: preserve-3d;

          transform-style: preserve-3d;

}

.modal .view .header a.close:before {

  content: '+';

  font-size: 28px;

}

.modal .view .header a.close:hover {

  opacity: 0.5;

}


.button {

  margin: 10px;

  padding: 10px 20px;

  border: 2px solid #FFF;

  border-radius: 3px;

  background: transparent;

  color: white;

  font-size: 14px;

  font-weight: 600;

  text-decoration: none;

  display: inline-block;

}


.button.primary {

  background: rgba(255, 255, 255, 0.2);

}


.button:hover {

  background: rgba(255, 255, 255, 0.5);

}


.pull-right {

  float: right;

  margin-left: 0px;

}


@-webkit-keyframes hide {

  0%, 100% {

    -webkit-transform: translateY(-100%);

            transform: translateY(-100%);

  }

}


@keyframes hide {

  0%, 100% {

    -webkit-transform: translateY(-100%);

            transform: translateY(-100%);

  }

}

</style>

 


 

<a class="button" href="#modal">Open</a>

<div class="modal clearfix" id="modal">

  <div class="background"></div>

  <div class="view-container">

    <div class="view">

      <div class="header">  

        <p>Hi! You opened up this modal!</p><a class="close" href="#"></a>

      </div>

      <div class="content"> 

        <p>Please hit save or cancel.  Nothing will happen!</p>

      </div>

      <div class="footer"><a class="button pull-right" href="#">Cancel</a><a class="button primary pull-right" href="#">Save</a></div>

    </div>

  </div>

</div>

 



<script>

var modal = document.querySelector('#modal');

setTimeout(function(){

   modal.classList.add('force');

}, 2000);

setTimeout(function(){

   modal.classList.remove('force');

}, 3000);

</script>

 

결과보기

https://codepen.io/sean_codes/pen/XMdgEr

 


분류 제목
DOM_Document JS - implementation 속성 - DOMimplementation (돔구현객체)를 Document…
DOM_Document JS - importNode() 메서드 ★ - 다른문서의 노드복사해서 가져오기 (IE9 이상) ※ 아이프레임…
DOM_Document JS - inputEncoding 속성 - 입력인코딩문자셋 (= 입력인코딩언어셋) 반환 (IE9 이상)
DOM_Document JS - lastModified 속성 - 최종수정시간 (= 마지막수정시간) 반환 [읽기전용]
DOM_Document JS - links 속성 - href 속성 갖은 (앵커태그 + 에어리어태그) 링크태그집합
DOM_Document JS - normalize() 메서드 - 공백제거후, 인접텍스트노드 합치기 (document경우)
DOM_Document JS - normalizeDocument() 메서드 - normalize() 메소드와 유사 (지원 X)
DOM_Document JS - open() 메서드 - 출력모드 열기
DOM_Document JS - querySelector() 메서드 ★ - 쿼리선택자 (= 문서 안 지정 선택자 중 첫번째 것만 =…
DOM_Document JS - querySelectorAll() 메서드 ★★★ - 일치요소 모두 선택 (= querySelecto…
DOM_Document JS - readyState 속성 - 현재웹문서 로딩상태 반환 (= readyState속성 = 레디스테이트속…
DOM_Document JS - referrer 속성 ★ - 접속경로 (= 리퍼러) 반환
DOM_Document JS - removeEventListener() 메서드 ★ - 이벤트핸들러를 웹문서에서 제거 (IE9 이상)
DOM_Document JS - renameNode() 메서드 - Element 및 Attr 유형의 노드 이름 변경 (지원 X)
DOM_Document JS - scripts -모든 스크립트태그 모음
DOM_Document JS - strictErrorChecking 속성 - 엄격한오류검사 설정/반환 (= 엄격한에러체크 = 스트릭…
DOM_Document JS - title - 타이틀 (=문서제목) 설정/반환
DOM_Document JS - document.URL 속성 - 웹문서주소 (= document.URL속성 = 다큐먼트유알엘속성/도…
DOM_Document JS - write() 메서드 - 내용쓰기 (= 내용입력) : 줄바꿈 X
DOM_Document JS - writeln() 메서드 - 내용쓰기 (= 내용입력) : 줄바꿈 X (= writeln메서드 = 라…
26/67
목록
찾아주셔서 감사합니다. Since 2012