将 div 保持在视口内或滚动时任意 div

发布于 2024-08-13 08:31:49 字数 497 浏览 3 评论 0原文

我正在尝试找到一个脚本脚本(或创建一个脚本),它可以使垂直导航栏“粘在”我的视口内。虽然这可能不是黑魔法(可以使用位置:固定CSS),但这种方法存在一个问题:如果导航栏长于视口高度,则访问者将无法看到整个导航。所以我希望导航栏不要将视口留在其顶部,但也不要将父容器留在其底部。它应该立即跟随滚动事件,而不需要花哨/弹跳动画。

这可能吗?谷歌没有透露任何对我有用的信息。你可能从slashdot的文章评论导航器或google视频的播放器框知道这种效果。

更新:本质上是对 jQuery 插件的重写 scrollFollow。如果它缺少动画也没有问题,因为我不会使用它们。但它应该能够保留在定义的父容器内而不需要剪切。 cookie 处理也不需要。

I'm trying to find a scriptaculous script (or create one) which keeps a vertical navigation bar "sticky" inside my viewport. While this may not be black magic (one could use the position:fixed css) there is a problem with this approach: If the navigation bar is longer than the viewport height the visitor won't be able to ever see the whole navigation. So I want that the navigation bar to not leave the viewport at its top but neither leave the parent container at its bottom. It should follow scroll events instantly without fancy/bouncing animations.

Is that possible? Google revealed nothing useful to me. You may know this kind of effect from slashdot's article comment navigator or google video's player box.

Update: Essentially that would be a rewrite of the jQuery plugin scrollFollow. It would be no problem if it lacked animations as I won't use them. But it should be able to stay within a defined parent container without clipping. The cookie handling is neither needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-08-20 08:31:49

我知道这确实很旧,但在尝试找到自己的解决方案时偶然发现了

这个链接显示了如何在真正的轻便时尚。基本上你需要的只是下面的代码(当然还有加载的 jQuery)

$(function() {
    var $sidebar   = $("#siderbar"),
        $window    = $(window),
        offset     = $sidebar.offset(),
        topPadding = 15;

    $window.scroll(function() {
        if ($window.scrollTop() > offset.top) {
            $sidebar.stop().animate({
                marginTop: $window.scrollTop() - offset.top + topPadding
            });
        } else {
            $sidebar.stop().animate({
                marginTop: 0
            });
        }
    });
})

如果你不希望它有动画效果,只需像这样替换动画部分

$sidebar.stop().animate({ marginTop: VALUE });

$sidebar.css("marginTop", VALUE);

I know this is really old but stumbled across this whilst trying to find my own solution

This link shows how to do it in a really lightweight fashion. Essentially all you need is the following code (along with jQuery loaded of course)

$(function() {
    var $sidebar   = $("#siderbar"),
        $window    = $(window),
        offset     = $sidebar.offset(),
        topPadding = 15;

    $window.scroll(function() {
        if ($window.scrollTop() > offset.top) {
            $sidebar.stop().animate({
                marginTop: $window.scrollTop() - offset.top + topPadding
            });
        } else {
            $sidebar.stop().animate({
                marginTop: 0
            });
        }
    });
})

If you don't want it to animate simply replace the animation sections like so:

$sidebar.stop().animate({ marginTop: VALUE });

with

$sidebar.css("marginTop", VALUE);
一口甜 2024-08-20 08:31:49

我知道这个问题很老了,但它出现在我的 Google 搜索中,让 div 停留在页面上进行滚动。这个解决方案工作得很好,并且在我的情况下不允许页面无限滚动,就像上面@Chris 的解决方案一样。

它需要在浮动 div 上方添加一个空 div

作为锚点。

在这个例子中,div需要有id="sticky"

解决方案:http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html

JavaScript:

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}

$(function () {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});

CSS:

#sticky.stick {
    position: fixed;
    top: 0;
    z-index: 10000;
}

I know this issue is old but it came up in my Google search for making a div stay on the page for scrolling. This solution works quite well and didn't allow the page to have an infinite scroll in my situation like the solution from @Chris above.

It requires adding an empty div <div id="sticky-anchor"></div> above the floating div to serve as an anchor.

In this example, the div needs to have id="sticky"

Solution: http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html

Javascript:

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}

$(function () {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});

CSS:

#sticky.stick {
    position: fixed;
    top: 0;
    z-index: 10000;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文