scrollTop() / scrollLeft()

scrollTop() 메서드는 브라우저의 스크롤바가 수직/수평으로 이동한 위칫값을 불러오거나 변경할 때 사용합니다.

$("선택자").scrollTop(); $("선택자").scrollLeft();

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQurey</title>
    <style>
        body {height: 10000px; width: 10000px;}
        div {width: 150px; height: 150px; color: #C7254E; background-color: #F9F2F4; border-radius: 50%; border: 1px dashed #A51A3D; text-align: center; line-height: 150px;} 
        .scrollTop {position: fixed; left: 30px; top: 30px;}
        .scrollTop::before {content: 'scrollTop ';}
        .scrollLeft {position: fixed; left: 200px; top: 30px;}
        .scrollLeft::before {content: 'scrollLeft ';}
    </style>
</head>
<body>
    <div class="scrollTop">0</div>
    <div class="scrollLeft">0</div>
    <!-- script -->
    <script src="jquery.min_1.12.14.js"></script>
    <script>
        $(window).scroll(function(){
            const scrollTop = $(window).scrollTop();
            const scrollLeft = $(window).scrollLeft();
            $(".scrollTop").text(parseInt(scrollTop));
            $(".scrollLeft").text(parseInt(scrollLeft));
        });
    </script>
</body>
</html>

Last updated

Was this helpful?