• 카테고리

    질문 & 답변
  • 세부 분야

    웹 개발

  • 해결 여부

    미해결

다시 질문 드립니다 ㅠㅠ CSS 키프레임 애니메이션 활용한 실전 예제 제작 01(원형 크기 변경 로딩 애니메이션)

24.03.05 17:23 작성 조회수 79

0

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>도형 로딩 애니메이션</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  
  <div class="loading">
    <span></span>
    <span></span>
    <span></span>
  </div>

</body>
</html>

 

/* Google Web Font */
@import url('http://fonts.googleapis.com/css?family=Raleway&display=swap');

body {
    font-family: 'Raleway', sans-serif;
    line-height: 1.5em;
    margin: 0;
    font-weight: 300;
    display: inline;
    justify-content: center;
    align-items: center;
    height: 100vh;
}
a {
    text-decoration: none;
}

.loading {}
.loading span {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: gray;
    border-radius: 50%;
    animation: loading 1s linear infinite;
}
.loading span:nth-child(1) {
    animation-duration: 0s;
    background-color:crimson;
}
.loading span:nth-child(2) {
    animation-duration: 0.2s;
    background-color:dodgerblue;
}
.loading span:nth-child(3) {
    animation-duration: 0.4s;
    background-color:royalblue;
}

@keyframes loading {
    0% {
        opacity: 0;
        transform: scale(0.5);
    }
    50% {
        opacity: 1;
        transform: scale(0.5);
    }
    100% {
        opacity: 0;
        transform: scale(0.5);
    }
}

이렇게 작성했는데 go live 화면에선

 

화면 캡처 2024-03-05 172212.jpg

 

이렇게 뜨고 애니메이션이 안나와요.. ㅠㅠ 뭐가 문제일까요??? 몇주동안 안되요..

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.03.07

안녕하세요, 인프런 AI 인턴입니다. SH님, 제시해 주신 코드와 설명을 바탕으로 문제를 분석해 보겠습니다.

CSS에서 키프레임 애니메이션은 정의한 @keyframes 규칙에 따라 요소의 스타일을 애니메이션으로 변경할 수 있습니다. 제공하신 스크린샷을 볼 수는 없으나 설명을 바탕으로 확인할 수 있는 몇 가지 문제점을 지적하고 수정 방안을 제안드리겠습니다.

우선 .loading 클래스에 대한 스타일 정의가 비어 있는 상태입니다. 이 클래스는 세 개의 span을 감싸고 있는 요소로, 해당 요소 중앙에 위치하도록 하는 스타일이 필요합니다. display: flex;, justify-content: center;, align-items: center; 속성을 사용하여 중앙 정렬을 할 수 있습니다.

또한, .loading span 요소들에 적용된 animation-duration이 첫 번째 요소에서 0s로 설정되어 있으므로, span 요소에 애니메이션이 적용되지 않습니다. 애니메이션 지속 시간은 기본적으로 1s로 설정하셨으나, 첫 번째 span에 대해 0s로 설정하면 애니메이션이 발생하지 않으므로 이를 1s 이상으로 설정해야 합니다.

다음은 수정된 .loading.loading span:nth-child(1)에 대한 스타일입니다:

.loading {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%; /* 부모 요소인 body에 대하여 100% 높이를 차지하도록 설정. */
}
.loading span:nth-child(1) {
    animation-duration: 1s; /* 애니메이션 지속 시간 수정 */
    background-color: crimson;
}

위의 수정을 통해 웹 페이지에 나타나는 세 개의 원형 요소(loading animation)가 중앙에 위치하고, 첫 번째 span 요소에도 애니메이션이 적절하게 적용되어 애니메이션 효과가 나타나야 합니다.

이후에도 수정했음에도 애니메이션이 나타나지 않는다면, 웹 브라우저의 개발자 도구를 통해 실시간으로 CSS 스타일과 애니메이션의 동작 여부를 확인하며 문제를 디버깅하는 것이 도움이 될 것입니다.

마지막으로 animation-duration은 각 요소마다 지연(delay)를 다르게 설정하여 시간 순서대로 애니메이션이 나타나도록 할 수 있으며, animation-delay 속성을 사용합니다. 예를 들면, 다음과 같습니다:

.loading span:nth-child(1) {
    animation-delay: 0.1s;
}
.loading span:nth-child(2) {
    animation-delay: 0.2s;
}
.loading span:nth-child(3) {
    animation-delay: 0.3s;
}

도움이 되셨길 바라며, 추가 질문이 있으시면 언제든지 문의해 주세요.