简体   繁体   English

Bootstrap 4滚动到长粘性侧栏的底部

[英]Bootstrap 4 scroll to the bottom of long sticky sidebar

I'm trying to achieve Facebook like right side navigation. 我正在尝试像右侧导航一样实现Facebook。 When the sidebar content is taller than the viewport, If you scroll down the sidebar should be scrolled to the very bottom before it's stick to the screen. 当侧边栏内容高于视口时,如果向下滚动,则侧边栏应滚动到最底端,然后再粘贴到屏幕上。

JSFiddle 的jsfiddle

    <div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="header">
                Header
            </div>
        </div>


        <div class="col-md-9 col-sm-9 col-xs-9">
          <div class="main-content">
              Main Content
          </div>
        </div>

        <div class="col-md-3 col-sm-3 col-xs-3">
            <div class="sidebar-right sticky-top">
                Sidebar Content

                <div class="content-bottom">
                      Content
                </div>
            </div>
        </div>

        <div class="col-md-12">
            <div class="footer">
                Footer
            </div>
        </div>
    </div>
</div>

I've come up with a solution to the problem using jQuery to make the sidebar sticky to a negative position based on the viewport and the sidebar's height ( fiddle here ): 我已经提出了一个使用jQuery的问题的解决方案,它基于视口和侧边栏的高度使侧边栏粘在负位置( 此处是小提琴 ):

 function setPosition() { $(".sidebar-right").css({ 'position': 'sticky', 'top': window.innerHeight - $(".sidebar-right").innerHeight() }); } // Set initial positioin setPosition(); // Adjust position when window is resized $(window).resize(setPosition); 
 .header { width: 100%; background-color: green; text-align: center; padding: 15px 20px; color: #fff; } .sidebar-left { background-color: purple; color: #fff; padding: 15px; height: 500px; } .sidebar-right { background-color: purple; color: #fff; padding: 15px; height: 500px; } .main-content { background-color: red; height: 900px; color: #fff; padding: 15px; } .footer { width: 100%; background-color: green; text-align: center; padding: 15px 20px; color: #fff; } .content-bottom { position: absolute; bottom: 0; } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="header"> Header </div> </div> <div class="col-md-9 col-sm-9 col-xs-9"> <div class="main-content"> Main Content </div> </div> <div class="col-md-3 col-sm-3 col-xs-3 sb"> <div class="sidebar-right"> Sidebar Content <div class="content-bottom"> Content </div> </div> </div> <div class="col-md-12"> <div class="footer"> Footer </div> </div> </div> </div> 

If you don't want to use jQuery, you can add the sticky-top class to the element at bottom of your sidebar (it won't look quite right). 如果您不想使用jQuery,则可以将sticky-top类添加到侧边栏底部的元素中(它看起来不太正确)。 Fiddle here (resize the window so the text at the sidebar is longer than the window height). 在此处拨弄小提琴 (调整窗口大小,以便侧边栏的文本长于窗口高度)。

 .header { width: 100%; background-color: green; text-align: center; padding: 15px 20px; color: #fff; } .sidebar-left { background-color: purple; color: #fff; padding: 15px; height: 500px; } .sidebar-right { background-color: purple; color: #fff; padding: 15px; height: 100%; } .main-content { background-color: red; height: 900px; color: #fff; padding: 15px; } .footer { width: 100%; background-color: green; text-align: center; padding: 15px 20px; color: #fff; } .content-bottom { position: absolute; bottom: 0; } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="header"> Header </div> </div> <div class="col-md-9 col-sm-9 col-xs-9"> <div class="main-content"> Main Content </div> </div> <div class="col-md-3 col-sm-3 col-xs-3"> <div class="sidebar-right"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer quis turpis enim. Sed non lacinia lacus. <div class="sticky-top"> Curabitur lorem lorem, efficitur sit amet ex non, viverra porttitor odio. </div> </div> </div> <div class="col-md-12"> <div class="footer"> Footer </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM