I just have a general question about debouncing. I have three menus at different positions on the page that when they reach a position 85px from the top of the window on scroll they become fixed. They are layered to overlap as they reach the top. I currently have a function for each and am looking to optimise as much as possible. My reading indicates the .offset.top calculation is quite taxing.
My question is: Am I overthinking it and is it necessary to debounce in this case? If my interpretation is right the three offset calculations are performed constantly on scroll. Can anyone suggest an optimisation or alterntively explain why it is not neccesary.
Thank you.
$(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'});
}
});
});
See Question&Answers more detail:os