我只是有一个关于去抖动的一般性问题。我在页面上的不同位置有三个菜单,当它们在滚动时到达距离窗口顶部 85px 的位置时,它们会变得固定。当它们到达顶部时,它们会分层重叠。我目前对每个功能都有一个功能,并且希望尽可能地进行优化。我的阅读表明 .offset.top 计算非常繁琐。
我的问题是:我是不是想多了,在这种情况下有必要去抖动吗?如果我的解释是正确的,那么三个偏移量计算会在滚动时不断执行。任何人都可以建议优化或解释为什么它不是必需的。
谢谢你。
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop0 = $('.fixed_heading_shop').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop0-85) {
$('.fixed_heading_shop').css({position: 'fixed', top: '85px'});
$('.ghost_div0').css({display: 'block'});
} else {
$('.fixed_heading_shop').css({position: 'relative', top: '0px'});
$('.ghost_div0').css({display: 'none'});
}
});
});
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop1 = $('.fixed_heading_pricetable').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop1-85 ) {
$('.fixed_heading_pricetable').css({position: 'fixed', top: '85px'});
$('.ghost_div1').css({display: 'block'});
} else {
$('.fixed_heading_pricetable').css({position: 'relative', top: '0px'});
$('.ghost_div1').css({display: 'none'});
}
});
});
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop2 = $('.fixed_heading_layout').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop2-85) {
$('.fixed_heading_layout').css({position: 'fixed', top: '85px'});
$('.ghost_div2').css({display: 'block'});
} else {
$('.fixed_heading_layout').css({position: 'relative', top: '0px'});
$('.ghost_div2').css({display: 'none'});
}
});
});