@
kisnows 在 mac 上触摸板上多次滚动还是有问题, 下面代码是另一个 fullpage 插件的做法
//
https://github.com/alvarotrigo/fullPage.js // Gets the average of the last `number` elements of the given array.
function getAverage (elements, number ) {
var sum = 0;
//taking `number` elements from the end to make the average, if there are not enought, 1
var lastElements = elements.slice (Math.max (elements.length - number, 1 ));
for (var i = 0; i < lastElements.length; i++) {
sum = sum + lastElements[i]
}
return Math.ceil (sum/number );
}
var prevtime = new Date ().getTime ();
$(document ).on ('mousewheel wheel DOMMouseScroll', function (e ) {
e.preventDefault ()
var curtime = new Date ().getTime ();
var value = e.wheelDelta || -e.deltaY || -e.detail;
var delta = Math.max (-1, Math.min (1, value ));
//Limiting the array to 150 (lets not waste memory!)
if (scrollings.length > 149 ) {
scrollings.shift ()
}
//keeping record of the previous scrollings
scrollings.push (Math.abs (value ))
//time difference between the last scroll and the current one
var timeDiff = curtime-prevtime;
prevtime = curtime;
//haven't they scrolled in a while?
//(enough to be consider a different scrolling action to scroll another section )
if (timeDiff > 200 ) {
//emptying the array, we dont care about old scrollings for our averages
scrollings = [];
}
if (canScroll ) {
var averageEnd = getAverage (scrollings, 10 );
var averageMiddle = getAverage (scrollings, 70 );
var isAccelerating = averageEnd >= averageMiddle;
//to avoid double swipes...
if (isAccelerating ) {
//scrolling down?
if (delta < 0 ) {
hash ('down')
//scrolling up?
} else {
hash ('up')
}
}
}
})