遇到问题的网址
https://jerryc.me/posts/20bbe8ff/
在手机上(我测试的safari)当我向上快速滑动时,原本page-header应该是在银幕外的,但是不知道为什么,现在page-header出现在银幕内,一闪一闪。。直接用scroll 不用throttle时,运行正常
function throttle(func, wait, mustRun) {
var timeout
var startTime = new Date()
return function () {
var context = this
var args = arguments
var curTime = new Date()
clearTimeout(timeout)
if (curTime - startTime >= mustRun) {
func.apply(context, args)
startTime = curTime
} else {
timeout = setTimeout(func, wait)
}
}
};
// main of scroll
$(window).scroll(throttle(function (event) {
var currentTop = $(this).scrollTop()
var isUp = scrollDirection(currentTop)
if (currentTop > 56) {
if (isUp) {
$('#page-header').hasClass('visible') ? $('#page-header').removeClass('visible') : console.log()
} else {
$('#page-header').hasClass('visible') ? console.log() : $('#page-header').addClass('visible')
}
$('#page-header').addClass('fixed')
} else {
if (currentTop === 0) {
$('#page-header').removeClass('fixed').removeClass('visible')
}
}
}, 50, 100))
css
throttle 函数写错了