滚动时准确触发

时间:2017-12-04 13:07:34

标签: javascript onscroll

我有一个简单的滚动事件,它检查何时达到滚动量。我遇到的问题通常是不会被解雇。

var viewH = document.documentElement.clientHeight
var siteMargin = 40
var slideHeight = viewH * 0.8 - siteMargin
var slideCnt = 1
var slidesArr = document.querySelectorAll(".slide");
var lastSlide = 0; 

document.addEventListener("scroll", function() {

var doc = document.documentElement;
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

var st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop) {
     // scroll down
    if (top == slideHeight * slideCnt) {
       // do something
    }
} else {
    // scrolling up
}
lastScrollTop = st;
}, false);

我尝试过更改为:

 if (top >= slideHeight * slideCnt) {
   // do something
   }

然后它多次发射......

演示:https://codepen.io/garethj/pen/QOzmeO?editors=1010

0 个答案:

没有答案