[자바스크립트] 마우스 커서따라 움직이는 요소

[자바스크립트] 마우스 커서따라 움직이는 요소

QA

[자바스크립트] 마우스 커서따라 움직이는 요소

본문

3660915809_1614159285.167.jpg

 

아래 소스를 적용해서 만들어진 상태인데요.

왜 스크롤을 내리면 스크롤 높이만큼 '마우스를 따라다니는 요소'가 벌어지는 걸까요?

 

 

[code]

<!-- 마우스 커서 -->
<div class="cursor_wrap">
    <div class="cursor"><span class="circle"></span></div>
</div>

<script>
    $(window).on("mousemove", (e) => {
        $(".cursor").css({
            top: e.pageY + "px",
            left: e.pageX + "px"
        });
        $('a, select, label, input, button, .tabs li').hover(function() {
            $('.cursor').addClass('mouse_hover');
        }, function(){
            $('.cursor').removeClass('mouse_hover');
        });
    });

 

/* 
    마우스 커서 
*/
.cursor_wrap {position: absolute; left: 0; top: 0; width: 30px; height: 30px; pointer-events: none; z-index: 10000; }
.cursor_wrap .cursor {position: fixed; }
.cursor_wrap .cursor .circle { display: block; opacity: 0.15; background-color: none; width: 30px; height: 30px; border-radius: 50%; -webkit-transition: 0.3s ease-in-out; transition: 0.3s ease-in-out; transform: translate(-50%, -50%) matrix(1, 0, 0, 1, 0, 0);}
.cursor_wrap .cursor.mouse_hover .circle { background-color: rgb(127, 127, 127);  transform: translate(-50%, -50%) matrix(3, 0, 0, 3, 0, 0); }

 

 

</script>

 

 

[code]

이 질문에 댓글 쓰기 :

답변 2

스크롤된 값을 빼줘야 하는데 깜빡한 모양이네요

top: (e.pageY - $(window).scrollTop()) + "px",
left: (e.pageX - $(window).scrollLeft()) + "px"

 


<body style="width: 5000px; height: 5000px;">
<!-- 마우스 커서 -->
<div class="cursor_wrap">
    <div class="cursor"><span class="circle"></span></div>
</div>
<script>
    $(window).on("mousemove", function(e) {
        $(".cursor").css({
            top: (e.pageY - $(window).scrollTop()) + "px",
            left: (e.pageX - $(window).scrollLeft()) + "px"
        });
        $('a, select, label, input, button, .tabs li').hover(function() {
            $('.cursor').addClass('mouse_hover');
        }, function(){
            $('.cursor').removeClass('mouse_hover');
        });
    });
</script>
<style>
.cursor_wrap {position: absolute; left: 0; top: 0; width: 30px; height: 30px; pointer-events: none; z-index: 10000; }
.cursor_wrap .cursor {position: fixed; }
.cursor_wrap .cursor .circle { display: block; opacity: 0.15; background-color: red; width: 30px; height: 30px; border-radius: 50%; -webkit-transition: 0.3s ease-in-out; transition: 0.3s ease-in-out; transform: translate(-50%, -50%) matrix(1, 0, 0, 1, 0, 0);}
.cursor_wrap .cursor.mouse_hover .circle { background-color: rgb(127, 127, 127);  transform: translate(-50%, -50%) matrix(3, 0, 0, 3, 0, 0); }
</style>
</body>
답변을 작성하시기 전에 로그인 해주세요.
전체 31
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT