最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.scroll events triggering twice - Stack Overflow

programmeradmin3浏览0评论

No matter what method I use to detect scrolling on the page the event is triggered twice. Please see the code for the different methods I have tried.

<body onmousewheel="alert('Why are you alerting twice?')">

or

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

or

window.onscroll = please_scroll;

function please_scroll() {
      alert("Why are you alerting twice?");
    }

I have even tried using $.debounce.

In case it is of any use I will explain what I am trying to do: When the user scrolls the wheel either up or down, the page will animate the scroll to the next full width content div. I have code that is successfully doing this onclick of my menu, but I would also like it to happen as the user scrolls, essentially auto assisting them with scrolling to each part of my page. This is the function I currently have for scrolling:

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

No matter what method I use to detect scrolling on the page the event is triggered twice. Please see the code for the different methods I have tried.

<body onmousewheel="alert('Why are you alerting twice?')">

or

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

or

window.onscroll = please_scroll;

function please_scroll() {
      alert("Why are you alerting twice?");
    }

I have even tried using $.debounce.

In case it is of any use I will explain what I am trying to do: When the user scrolls the wheel either up or down, the page will animate the scroll to the next full width content div. I have code that is successfully doing this onclick of my menu, but I would also like it to happen as the user scrolls, essentially auto assisting them with scrolling to each part of my page. This is the function I currently have for scrolling:

function scrollTo(id){
  // Scroll
  $('html,body').animate({scrollTop: $("#"+id).offset().top - 110},'slow',function(){
    animation_active = "false";
  });
}
Share Improve this question edited Feb 25, 2014 at 15:37 Iceman3000 asked Feb 25, 2014 at 15:13 Iceman3000Iceman3000 1431 gold badge1 silver badge7 bronze badges 2
  • firstly, should be: window.onscroll = please_scroll; Secondly, you shouldn't use alert to debug event – A. Wolff Commented Feb 25, 2014 at 15:17
  • well the mousewheel and other hids like the apple mouses can trigger scroll events more often when being used. you have to use a timeout for that – Alex Commented Feb 25, 2014 at 15:20
Add a comment  | 

1 Answer 1

Reset to default 19

many devices can trigger scroll events which appear to happen once more often. simply use a timeout for that:

var timeout;

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

you can play with the value 50, i recommend something between 50 and 150.

发布评论

评论列表(0)

  1. 暂无评论