I'm

Developer, Designer, Photographer

GET IN TOUCH

  • Buk-gu, Busan, Republic of Korea
  • ungdoli0916@naver.com
  • Kakao ID : Ungdoli
Your message has been sent. Thank you!
(즐거웅코드) 제이쿼리 스크롤 업/다운 방향 체크하기

· 즐거'웅' 코드 (Source)/HTML & CSS & JAVASCRIPT
2021. 9. 20. 14:30

반응형
jQuery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
 
<script>
    var last_scrollTop = 0;
 
    $(window).scroll(function () {
        var tmp = $(this).scrollTop();
        if (tmp > last_scrollTop) {
            // scroll down event
        } else {
            // scroll up event
        }
        last_scrollTop = tmp;
    });
</script>
예제 소스
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
 
<style>
    body {
        height: 3000px;
    }
 
    p {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
</style>
 
<p class="test_obj"></p>
 
<script>
    var last_scrollTop = 0;
 
    $(window).scroll(function () {
        var tmp = $(this).scrollTop();
        if (tmp > last_scrollTop) {
            $(".test_obj").text("scroll down");
        } else {
            $(".test_obj").text("scroll up");
        }
        last_scrollTop = tmp;
    });
</script>
구현 예시
반응형