<h2>애니메이션 컨테이너 만들기</h2>
  <p><button onclick="myMove()">나를 클릭하세요.</button></p>
  <div id="container">
    <div id="animate">b</div>
 </div>
 <style>
    #container{
        width: 400px;
        height: 400px;
        position: relative;          
        background: yellow;
    }
    #animate{
        width:50px;
        height: 50px;
        position: absolute;
        background: red;
    }
</style>

    <script>
        function myMove(){
            let id=null;
            const elem=document.getElementById("animate");
            let pos = 0;
            clearInterval(id);
            id = setInterval(frame,5);
            function frame(){
                if(pos==350){
                    clearInterval(id);
                }else{
                    pos++;
                    elem.style.top=pos+"px";
                    elem.style.left=pos+"px";
                }
            }
        }
       
    </script>

'JAVASCRIPT > 예제들' 카테고리의 다른 글

input 빈칸시 경고창띄우기  (0) 2022.08.28
select 의 옵션 value 선택하여 img 속성넣기 문제  (0) 2022.08.27
정규표현식 2  (0) 2022.08.25
정규 표현식 예제1  (0) 2022.08.25
대소문자 변경예제  (0) 2022.08.25

+ Recent posts