代码之家  ›  专栏  ›  技术社区  ›  Paul Designer

固定收割台,卡在主固定收割台下方

  •  2
  • Paul Designer  · 技术社区  · 12 年前

    我一直在与这个网站上的其他一些开发人员一起解决一个固定标题问题。

    我现在已经在这里更新了小提琴 http://jsfiddle.net/f95sW/

    问题

    1) 向下滚动页面时,黄色块需要与红色块对齐。

    请查看代码和演示,如有任何帮助,将不胜感激。

    var offset = $(".sticky-header").offset();
    var sticky = document.getElementById("sticky-header")
    var additionalPixels = 50;
    
    $(window).scroll(function () {
    
        if ($('body').scrollTop() > offset.top + additionalPixels) {
            $('.sticky-header').addClass('fixed');
        } else {
            $('.sticky-header').removeClass('fixed');
        }
    
    
    });
    
    2 回复  |  直到 12 年前
        1
  •  3
  •   j08691    12 年前

    两个问题。你没有包括固定类,所以我在这个中添加了它:

    .fixed{
        position: fixed;
        top:52px;
    }
    

    jsFiddle example

    但更重要的是,你需要将你的数学改为:

    if ($('body').scrollTop() > offset.top - additionalPixels) {
    
        2
  •  0
  •   nilobarp    12 年前

    看起来你错过了固定课程

    .fixed{
        position: fixed;
        top: 0; left:0;
    }
    

    这是最新的小提琴 http://jsfiddle.net/f95sW/2/