适合屏幕底部/顶部的菜单

发布于 2024-11-07 12:51:32 字数 280 浏览 1 评论 0原文

我看到一个很酷的 style/js 函数(我可以告诉它是什么),它在侧面菜单上实现。 您知道当您有一个很长的中心页并且其中一侧/两侧都结束并留下空白时的情况吗?好吧,这个网站实现了这个功能,当用户滚动到侧面菜单结束的地方时 - 菜单获得绝对位置并且不会移动。

我该怎么做?

如果您想查看示例,可以查看此处(仅滚动并查看侧面)

I saw a cool style/js function (I can tell what it is) that implemented on a side menu.
You know the situation when you have a long center page and one of / both of you sides ends and that leave a blank space? Well this site implemented this thing that just when the user scrool to the place where the side menu end - the menu get absolute postion and doesnt move.

How can I do this?

If you want to see an example you can look here (just scroll and look on the sides)

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

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

发布评论

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

评论(2

格子衫的從容 2024-11-14 12:51:32

我相信您可以使用以下方法实现类似的效果: http://www.wduffy.co.uk/blog/keep-element-in-view-while-scrolling-using-jquery/comment-page-1/ (只是使其以 0 作为速度参数而不是 slow 移动,如 example) 并添加有关当前位置是否适合其显示的框的条件(您可以获取框的高度 - 正在移动的菜单在包含菜单的页面或框中 - 通过使用 .height() jQuery 函数)。

编辑:

您引用的页面使用以下 JavaScript 代码来支持您尝试完成的任务:

<script type="text/javascript">
    $(function(){
        var seoHeight =  $('dvIndexSeoMaster').height();
            seoHeight = (seoHeight > 0) ?  seoHeight : 0; 
        var documentHeight = $(document.body).height() - 120 - seoHeight;
        var fixedMode = false;
        var hasFixedClass = false;
        var leftColElm = $sc('dvFixed');
        var leftColPos = leftColElm.offset().top;
        var leftColHeight = leftColElm.height();
        var rightColElm = $('dvIndexMasterRightCol');
        var rightColPos = rightColElm.offset().top;
        var rightColHeight = rightColElm.height();
        function scrollElm(elmPos,elmHeight,objElm, cssClass){
            var fixedMode = false;
            var hasFixedClass = false;
            var windowTop = $(window).scrollTop();
            (windowTop >= elmPos && (windowTop + elmHeight) < documentHeight) ? fixedMode = true : fixedMode = false;
            if( fixedMode){
                $(objElm).addClass(cssClass);
                hasFixedClass = true;
            }else if( (fixedMode == false)){
                $(objElm).removeClass(cssClass);
                hasFixedClass = false;
            }
        };
        $(window).scroll(function(){
            scrollElm(leftColPos,leftColHeight,leftColElm,'make-fixed');
            scrollElm(rightColPos,rightColHeight,rightColElm, 'make-fixed');
        });
    });
</script>

并且 make-fixed CSS 类具有以下定义:

.make-fixed {
    position: fixed;
    top: 0;
    z-index: 200;
}

I believe you can achieve similar effect using this: http://www.wduffy.co.uk/blog/keep-element-in-view-while-scrolling-using-jquery/comment-page-1/ (just making it move with 0 as speed parameter instead of slow, as in the example) and adding conditions about whether the current position fits within the box it is displayed (you can take height of the box - menu being moved on page or box that contains the menu - by using .height() jQuery function).

EDIT:

The page you referenced uses the following JavaScript code to support what you try to accomplish:

<script type="text/javascript">
    $(function(){
        var seoHeight =  $('dvIndexSeoMaster').height();
            seoHeight = (seoHeight > 0) ?  seoHeight : 0; 
        var documentHeight = $(document.body).height() - 120 - seoHeight;
        var fixedMode = false;
        var hasFixedClass = false;
        var leftColElm = $sc('dvFixed');
        var leftColPos = leftColElm.offset().top;
        var leftColHeight = leftColElm.height();
        var rightColElm = $('dvIndexMasterRightCol');
        var rightColPos = rightColElm.offset().top;
        var rightColHeight = rightColElm.height();
        function scrollElm(elmPos,elmHeight,objElm, cssClass){
            var fixedMode = false;
            var hasFixedClass = false;
            var windowTop = $(window).scrollTop();
            (windowTop >= elmPos && (windowTop + elmHeight) < documentHeight) ? fixedMode = true : fixedMode = false;
            if( fixedMode){
                $(objElm).addClass(cssClass);
                hasFixedClass = true;
            }else if( (fixedMode == false)){
                $(objElm).removeClass(cssClass);
                hasFixedClass = false;
            }
        };
        $(window).scroll(function(){
            scrollElm(leftColPos,leftColHeight,leftColElm,'make-fixed');
            scrollElm(rightColPos,rightColHeight,rightColElm, 'make-fixed');
        });
    });
</script>

And the make-fixed CSS class has the following definition:

.make-fixed {
    position: fixed;
    top: 0;
    z-index: 200;
}
浅黛梨妆こ 2024-11-14 12:51:32

即使用户滚动,您也可以使用 CSS position:fixed 属性使元素保持在同一位置:http://www.w3.org/TR/CSS2/visuren.html#fixed-positioning

You can make an element stay in the same place, even as the user scrolls, with the CSS position:fixed property: http://www.w3.org/TR/CSS2/visuren.html#fixed-positioning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文