Contents

Develop
2015.04.14 18:48

[js] 스크롤 이벤트 막기

조회 수 1020 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
웹 개발을 진행하다 보면 유저 인터페이스 구성을 위해 마우스 휠에 의한 스크롤을 막아야 할 경우가 생긴다.
이럴 경우 자바스크립트의 jquery를 이용하여 간단하게 제어할 수 있다.
아래의 소스 코드를 확인해보자.
(※버튼을 이용해 직접 테스트 해볼 수 있다.)
<script type="text/javascript">
    $(window).on("mousewheel.disableScroll DOMMouseScroll.disableScroll touchmove.disableScroll", function(e) {
        e.preventDefault();
        return;
    });
    $(window).on("keydown.disableScroll", function(e) {
        var eventKeyArray = [32, 33, 34, 35, 36, 37, 38, 39, 40];
        for (var i = 0; i < eventKeyArray.length; i++) {
            if (e.keyCode === eventKeyArray [i]) {
                e.preventDefault();
                return;
            }
        }
    });
</script>

<script type="text/javascript">
     $(window).off(".disableScroll");
</script>

위에서 확인한 바와같이 jquery의 on/off 펑션을 이용하여 mousewheel, DOMMouseScroll, touchmove 이벤트 활성/비활성화 시키고 있다.
jquery의 on/off 펑션에 대해서는 jquery의 api를 참고하면 될 듯 하다.
간단하게 설명하자면 .on(이벤트, 핸들러)의 형태로 호출하며 이벤트 파라미터의 경우 단일 혹은 공백문자 구분으로 복수지정이 가능하며 이벤트명 또는 이벤트명과 네임스페이스의 결합형태 로 지정할 수 있다.
위의 소스에서는 mousewheel이라는 이벤트명에 disableScroll이라는 네이스페이가 결합된 형태이다.
네임스페이스는 도트문자[.]로 구분하며 여러계층으로 지정이 가능하다.
해제 펑션의 경우 .off(이벤트)의 형태로 호출하며 이벤트 파라미터는 on과 마찬가지로 지정하면 된다.
제거 시에는 위의 소스에서처럼 네임스페이스로 호출하게 되면 해당 네임스페이스 포함 하위 네임스페이스를 제거가 가능하므로 호출별로 별도로 이벤트 컨트롤이 가능하다.

예제에서 DOMMouseScroll이벤트는 파이어폭스 계열에서는 mousewheel이벤트가 작동하지 않으므로 해당 이벤트로 DOMMouseScroll를 추가해 주어야 하며 touchmove는 태블릿 피시에서의 터치 이벤트에 관여한다.
별도로 호출하고 있는 keydown의 경우는 pageup등의 페이지를 스크롤할 수 있는 키의 이벤트에 관여하여 이벤트를 핸들링한다.

위의 코드를 참고하면 페이지 스크롤을 일시적으로 블럭 시켰다가 다시 해제할 수 있으므로 UI구성시에 도움이 될듯 하다.
<script type="text/javascript">
    var $reiphiel = {};
    $reiphiel.disableScroll= function() {
        $(window).on("mousewheel.disableScroll DOMMouseScroll.disableScroll touchmove.disableScroll", function(e) {
            e.preventDefault();
            return;
        });
        $(window).on("keydown.disableScroll", function(e) {
            var eventKeyArray = [32, 33, 34, 35, 36, 37, 38, 39, 40];
                for (var i = 0; i < eventKeyArray .length; i++) {
                if (e.keyCode === eventKeyArray [i]) {
                    e.preventDefault();
                    return;
                }
            }
        });
    };
    $reiphiel.enableScroll= function() {
        $(window).off(".disableScroll");
    };
</script>


[출처] http://reiphiel.tistory.com/58


?

  1. [ios] Touch ID 적용 샘플 코드 (예제)

    Date2015.02.23 CategoryDevelop Byhooni Views678
    Read More
  2. [ios] UDID 사용 제한에 따른 대안들

    Date2014.03.13 CategoryDevelop Byhooni Views3974
    Read More
  3. [ios] UDID와 UUID (디바이스의 Unique Identifier)

    Date2013.04.23 CategoryDevelop Byhooni Views27351
    Read More
  4. [ios] UIAlertView 초간단 샘플 ㅎㅎ

    Date2013.10.14 CategoryDevelop Byhooni Views46128
    Read More
  5. [ios] UIButton multi-line iOS7

    Date2014.01.09 CategoryDevelop Byhooni Views11440
    Read More
  6. [ios] UIColor 지정에서 RGB define ㅎㅎ

    Date2013.04.23 CategoryDevelop Byhooni Views22921
    Read More
  7. [ios] UILabel top alignㅎㅎ

    Date2013.04.23 CategoryDevelop Byhooni Views22846
    Read More
  8. [ios] UITableView 특정 Row만 Update

    Date2014.04.08 CategoryDevelop Byhooni Views4787
    Read More
  9. [ios] UIView에서 상위 UIViewController 가져오기

    Date2013.09.27 CategoryDevelop Byhooni Views20166
    Read More
  10. [ios] UIWebView 캐쉬 삭제

    Date2014.04.08 CategoryDevelop Byhooni Views3671
    Read More
  11. [ios] UIWebView 쿠키 유지

    Date2014.01.16 CategoryDevelop Byhooni Views11709
    Read More
  12. [ios] UIWebView를 이용한 로컬 HTML 파일 표시

    Date2015.01.02 CategoryDevelop Byhooni Views1248
    Read More
Board Pagination Prev 1 ... 41 42 43 44 45 46 47 48 49 50 ... 98 Next
/ 98