JS/JS 공부

[JS] mosemove 마우스가 움직이면 아이콘도 이동

congs 2023. 4. 24. 21:56

 

 

Document
star

 

 

 

 

 
 
<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>


    <style>

        body{

            background-color: black;

        }

        .icon{

            position: absolute;

            top: 50%;

            left: 50%;

            transform: translate(-50%, -50%);

        }

        .material-symbols-outlined{

            color: white;

            font-size: 4rem;

        }

    </style>

</head>

<body>

    <!-- 마우스가 움직이면 아이콘이 따라 움직이게 만들어보기 -->

    <div class="icon">

        <span class="material-symbols-outlined">

            star

            </span>

    </div>

   
<script>

        document.body.addEventListener('mousemove', (e)=>{

           console.log(e);

           document.querySelector('.icon').style.left = e.pageX +'px';  //px만큼 움직임

           document.querySelector('.icon').style.top = e.pageY +'px';   //px만큼 움직임

        })


   
</script>

</body>

</html>