滚动时固定标题

时间:2019-06-18 05:25:15

标签: javascript jquery css

我有一个功能,可以在网站标题之前固定标题。一旦达到特定点,我将其设置为position:fixed。但是由于某种原因,它会抖动回到postion:relative

由于某种原因,它没有达到预期的顶部,这可能是什么问题?

var stickyHeaderTop = $('.tab1').offset().top;
$(window).scroll(function(){
    var width = $(window).width();
    if( ($(window).scrollTop() > stickyHeaderTop-60) && (width>1200) ) {
        console.log('im here');
        $('.tab1').css({position: 'fixed', top: '60px', width: left,zIndex:'999'});
        $('.tab2').css({position: 'fixed', top: '60px', width: right});
        $('.tab3').css({position: 'fixed', top: '60px', width: thirdCol,background: '#ffffff',zIndex:'99999'});
    } else {
        console.log('nooo,, i cant stick im here');
        $('.tab1').css({position: 'relative', top: '0px'});
        $('.tab2').css({position: 'relative', top: '0px'});
        $('.tab3').css({position: 'relative', top: '0px'});
    }
});

执行此块时,在im here的其余打印时间中,仅在日志nooo,, i cant stick im here被打印一次。

0 个答案:

没有答案
相关问题