window.scroll事件触发两次

Ice*_*000 12 javascript jquery scroll custom-scrolling

无论我使用什么方法来检测页面上的滚动,事件都会被触发两次.请参阅我尝试过的不同方法的代码.

<body onmousewheel="alert('Why are you alerting twice?')">
Run Code Online (Sandbox Code Playgroud)

要么

<script src="js/jquery-1.8.3.min.js"></script>
<script>
$(window).scroll(function(){
    alert("Why are you alerting twice?");
});
</script>
Run Code Online (Sandbox Code Playgroud)

要么

window.onscroll = please_scroll;

function please_scroll() {
      alert("Why are you alerting twice?");
    }
Run Code Online (Sandbox Code Playgroud)

我甚至尝试过使用$ .debounce.

如果它有任何用处,我会解释我要做的事情:当用户向上或向下滚动滚轮时,页面会将滚动设置为下一个全宽度内容div的动画.我有一些代码可以成功地单击我的菜单,但我也希望它发生在用户滚动时,基本上是自动帮助他们滚动到我的页面的每个部分.这是我目前用于滚动的功能:

function scrollTo(id){
  // Scroll
  $('html,body').animate({scrollTop: $("#"+id).offset().top - 110},'slow',function(){
    animation_active = "false";
  });
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 15

许多设备可以触发似乎再次发生的滚动事件.只需使用超时:

var timeout;

$(window).scroll(function() {
    clearTimeout(timeout);  
    timeout = setTimeout(function() {
        // do your stuff
    }, 50);
});
Run Code Online (Sandbox Code Playgroud)

你可以玩50的价值,我推荐50到150之间的东西.