// 防抖函数
/* 可以把多个顺序地调用合并成一次,即规定事件被触发的次数 */
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
// 节流函数
/* 只允许函数在x毫秒内执行一次 */
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);
}
};
}
var navcommonScrollFuc = debounce(function () {
let left = $(document).scrollLeft();
console.log(left)
$(".navcommon_wrap").css('left', (-left) + 'px');
}, 500, false)
$(window).scroll(navcommonScrollFuc)