2016-08-30 4 views
1

마우스 휠을 사용하여 페이지의 배경을 선택하고 있습니다. 나는 단지 1000ms 후에 mousewheel 이벤트를 트리거하기를 원합니다. 그래서 나는 debounce 기능을 사용하고 있습니다.디버깅 기능은 마우스 휠에서 e.preventDefault가 더 이상 작동하지 않음을 의미합니다.

내가 debounce 함수를 추가하기 전에 e.preventDefault()을 사용하면 스크롤이 작동하지 않게되었습니다. 그러나 이제는 debounce 함수를 추가 했으므로 더 이상 작동하지 않으며 사용자가 페이지를 다시 스크롤 할 수 있습니다.

아래 코드를 참조하십시오.

$(document).ready(function() { 
    $(document).on('mousewheel DOMMouseScroll',debounce(function(e){ 
     e.preventDefault(); 
     //code to change the background image 
    }, 1000)) 
}); 

function debounce(fn, delay) { 
    var timer = null; 
    return function() { 
    var context = this, args = arguments; 
    clearTimeout(timer); 
    timer = setTimeout(function() { 
     fn.apply(context, args); 
    }, delay); 
    }; 

답변

0

다음과 같이 그것을 만들기 :

$(document).ready(function() { 
    var changeBackground = debounce(function(e){ 
     //code to change the background image 
    }, 1000) 
    $(document).on('mousewheel DOMMouseScroll',debounce(function(e){ 
     e.preventDefault(); 
     changeBackground(e); 
    }) 
}); 
관련 문제