# 간단한 애니메이션 만들기

✨ 과제. 버튼 누르면 modal 창 띄우기 (jQuery 사용하기)

{% code title="main.css" %}

```css
.black-bg{
    display: none;
}
.show-modal{
    display: block;
}
```

{% endcode %}

```html
<script>
    $('#loginBtn').on('click', function(){
        $('.black-bg').toggleClass('show-modal');
        $('.black-bg').addClass('show-modal');
     });

     $('#close').on('click', function() {
        $('.black-bg').toggleClass('show-modal');
        $('.black-bg').removeClass('show-modal');
     })
</script>
```

💎간단한 애니메이션 만들기

지난번 UI 만들기는 이런 프로세스로 만들었다.

&#x20;1\. HTML / CSS로 미리 디자인

&#x20;2.필요할 때 호출

애니메이션은&#x20;

&#x20;1\. 시작 스타일 만들기 (class)

&#x20;2\. 최종 스타일 만들기 (class)

&#x20;3\. 원할 때 최종스타일로 변하라고 JS 코드 짜기

&#x20;4\. 시작스타일에 transition 추가

js007.html을 복사해 js008.html 만들기&#x20;

{% code title="js008.html" %}

```html
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">

    <link rel="stylesheet" href="main.css">
    <script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>

</head>
<body>

    <div class="black-bg1">
        <div class="white-bg">
            <h4>로그인하세요</h4>
            <button class="btn btn-danger" id="close">닫기</button>
        </div>
    </div>

    <nav class="navbar ">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Offcanvas navbar</a>
            <button class="navbar-toggler" type="button">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
    </nav>

    <ul class="list-group" id="list1">
        <li class="list-group-item">An item</li>
        <li class="list-group-item">A second item</li>
        <li class="list-group-item">A third item</li>
        <li class="list-group-item">A fourth item</li>
        <li class="list-group-item">And a fifth one</li>
    </ul>

    <button id="loginBtn" type="button" class="btn btn-primary">
        버튼
    </button>

  
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>
</html>
```

{% endcode %}

black-bg 클래스를 black-bg1 클래스로 변경하고 css 추가

{% code title="main.css" %}

```css
.black-bg1 {
    width : 100%;
    height : 100%;
    position : fixed;
    background : rgba(0,0,0,0.5);
    z-index : 5;
    padding: 30px;
    visibility: hidden;
    opacity: 0;
}

.show-modal1 {
    visibility: visible;
    opacity: 1;
}
```

{% endcode %}

이렇게 <mark style="color:blue;background-color:orange;">1.시작스타일</mark>과 <mark style="color:purple;background-color:orange;">2.최종스타일</mark>을 작성함

기존의 display는 애니메이션이 잘 동작하지 않기 때문에&#x20;

비슷한 역할을 하는 visibility: hidden 을 사용함

opacity는 투명도 조절 속성 (0은 투명 1은 불투명) 0과 1사이의 값으로 투명도 조절 가능

<mark style="color:purple;background-color:orange;">3. 원할 때 최종스타일로 변하라고 JS 코드 짜기</mark>

```javascript
  <script>
     $('#loginBtn').on('click', function(){
        $('.black-bg1').addClass('show-modal1');

     });

     $('#close').on('click', function() {
        $('.black-bg1').removeClass('show-modal1');
     })
 </script>

```

<mark style="color:purple;background-color:orange;">4. 시작스타일에 transition 추가</mark>

```css
.black-bg1 {
    width : 100%;
    height : 100%;
    position : fixed;
    background : rgba(0,0,0,0.5);
    z-index : 5;
    padding: 30px;
    visibility: hidden;
    opacity: 0;
    transition: all 5s;
}
```

transition의 시간을 조절해서 애니메이션 길이 변경 가능

✨ 실습. 기존에 만들었던 navbar를 애니메이션을 사용해 아래로 펼쳐지게 만들어보자.

```css
.list-group {
    height: 0;
    overflow: hidden;
    transition: height 2s;
}
.lg-open{
    height: 250px;
}
```

```html
<script>
    $('.navbar-toggler').on('click', function() {
        $('.list-group').toggleClass('lg-open');
    });
</script>

```

{% embed url="<https://webclub.tistory.com/581>" %}
CSS3 애니메이션 예시
{% endembed %}

{% embed url="<https://codepen.io/pen/>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leeans-dev-book.gitbook.io/docs/lecture/javascript/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
