代码之家  ›  专栏  ›  技术社区  ›  kbanman

CSS半固定元素?

  •  3
  • kbanman  · 技术社区  · 15 年前

    它是一个按钮或类似的东西,坐在它的位置靠近屏幕顶部,然后当你向下滚动它停留在屏幕上。

    现在我想起来了,它一定是javascript驱动的,但是看起来很自然。

    有没有人知道一个网站有这样的功能或如何做到这一点的信息?

    编辑
    不,不只是 position:fixed 或者使用javascript永久浮动。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Community CDub    8 年前

    感谢durilai指出这一点: How to make an element slide with the viewport as it scrolls?

    事实证明,就在这里(问题编辑页面)我看到了这个。“如何格式化”框位于编辑框的右侧,并与页面的其余部分一起移动,但变为 position:fixed 当它应该被滚动出视图时。

    var scrollerTopMargin = $("#scroll-container").offset().top;
    $(window).scroll(function(){
        var c = $(window).scrollTop();
        var d = $("#scroll-container");
        if (c > scrollerTopMargin) {
            d.css({ position: "fixed", top: "0px"   });
        }
        else if (c <= scrollerTopMargin) 
        {
            d.css({ position: "relative", top: ""   });
        }
    });