scroll抖动问题

时间:2023-03-09 05:12:47
scroll抖动问题

参考软文: http://www.cnblogs.com/coco1s/p/5499469.html

     function throttle(func, wait, mustRun) {
var timeout,
startTime = new Date(); return function() {
var context = this,
args = arguments,
curTime = new Date(); clearTimeout(timeout);
// 如果达到了规定的触发时间间隔,触发 handler
if(curTime - startTime >= mustRun){
func.apply(context,args);
startTime = curTime;
// 没达到触发间隔,重新设定定时器
}else{
timeout = setTimeout(func, wait);
}
};
};
// 实际想绑定在 scroll 事件上的 handler
function realFunc(){
var wHeight = $(window).scrollTop();
var imgTop = $("p").offset().top;
if(wHeight > imgTop){
$("img").addClass("posFixed");
}else{
$("img").removeClass("posFixed");
}
}
// 采用了节流函数
window.addEventListener('scroll',throttle(realFunc,300,1000));