전 글에서 설명한 영화 검색 사이트 구현에 로딩화면을 구현해보는 태스크가 있었다.
오늘은 이를 구현한 과정에 대해 써보겠다.
먼저 자바스크립트 코드
function showLoading() {
document.getElementsByClassName('layerPopup')[0].style.display = 'block';
}
function hideLoading() {
document.getElementsByClassName('layerPopup')[0].style.display = 'none';
}
CSS
.layerPopup {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
z-index: 1000;
justify-content: center;
align-items: center;
margin: 0 0 0 0;
}
.spinner {
position: absolute;
top: 50%;
left: 50%;
border: 8px solid #f3f3f3;
/* Light grey */
border-top: 8px solid #3498db;
/* Blue */
border-radius: 50%;
width: 60px;
height: 60px;
animation: spinner 2s linear infinite;
}
@keyframes spinner {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
CSS에서 모양을 만들어주고 이를 회전시켜서 우리가 익숙한 동그라미가 회전하면서
로딩이 진행되고 있는 듯한 모습을 구현해놓고 이를 자바스크립트에서 함수를 실행시켜서
그 화면이 나오게 했다가 안 나오게 하는 방식으로 구현했다.
함수는 검색할 때 클릭 이벤트가 시작될 때 보여주고 검색 결과가 나왔을 때 꺼지도록 하였다.
이미지 파일 같은 것을 안쓰고 css만으로 해당 화면을 만들 수 있는 것이 신기했다.