210410 JavaScript jQuery MouseMove 연습

ITisIT210·2021년 4월 12일
0

jQuery

목록 보기
40/142
post-thumbnail
<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <style>
                * {
                    margin: 0;
                    padding: 0;

                }
                .wrap {
                    width: 500px;
                    height: 500px;
                    background-color: #ccc;
                    margin: 0 auto;
                    position: relative;
                }
                p {
                    width: 50px;
                    height: 50px;
                    background-color: red;
                    border-radius: 50%;
                    position: absolute;
                }
            </style>
    </head>
    <boby>
        <div class="wrap">
            <p></p>
        </div>
        <script src="js/jquery-3.6.0.min.js"></script> 
        <script src="js/jquery-ui.min.js"></script> 
        
        <script>
            // offsetX, offsetY : 이벤트 대상을 기준으로 마우스 커서 좌표
            // screenX, screenY : 모니터 기준으로 마우스 커서 좌표
            // pageX, pageY : 문서 전체를 기준으로 마우스 커서 좌표
            // clientX, clientY : 브라우저 페이지(윈도우)를 기준으로 마우스 커서 좌표

            $(".wrap").on("mousemove", function(e) { // 선택자 위에서 마우스가 움직일 때마다 이벤트가 발생
                console.log(e);
                var x = e.offsetX;
                var y = e.offsetY;

                $("p").css({
                    "left" : x,
                    "top" : y
                });
            });
        </script>
    </boby>
    </html>
profile
Engineering is the closest thing to magic that exists in the world.

0개의 댓글