html - 停止侧边栏在 y 轴上滚动

标签 html css twitter-bootstrap

我的代码中有一个侧边栏,它来自 here .当页面内容过长时,body会滚动,侧边栏不会滚动。

Example fiddle

这就是我希望我的工作方式,但是因为我有标题,所以我看到了这种讨厌的效果 here .

HTML:

<title>Simple Sidebar - Start Bootstrap Template</title>
<body>
    <!--Header-->
    <div id="header-content-wrapper" class="as-header-wrapper">
        <div class="container-fluid">
            <div id="header" class="row">
                <div class="col-md-7 panel">
                    <span>This is heading text!!!</span>
                </div>
            </div>
        </div>
    </div>
    <div id="wrapper" class="">
        <!-- Sidebar -->
        <div id="sidebar-wrapper">
            <ul class="sidebar-nav">
                <li class="sidebar-brand"> <a href="#">
                        Start Bootstrap
                    </a>

                </li>
                <li> <a href="#">Dashboard</a>

                </li>
                <li> <a href="#">Shortcuts</a>

                </li>
                <li> <a href="#">Overview</a>

                </li>
                <li> <a href="#">Events</a>

                </li>
                <li> <a href="#">About</a>

                </li>
                <li> <a href="#">Services</a>

                </li>
                <li> <a href="#">Contact</a>

                </li>
            </ul>
        </div>
        <!-- /#sidebar-wrapper -->
        <!-- Page Content -->
        <div id="page-content-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                         <h1>Simple Sidebar</h1>

                        <p>This template has a responsive menu toggling system. The menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will appear/disappear. On small screens, the page content will be pushed off canvas.</p>
                        <p>Make sure to keep all page content within the <code>#page-content-wrapper</code>.</p> <a href="#menu-toggle" class="btn btn-default" id="menu-toggle">Toggle Menu</a>

                        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
                      Hellow
                    </div>
                </div>
            </div>
        </div>
        <!-- /#page-content-wrapper -->
    </div>
    <!-- /#wrapper -->
    <!-- jQuery -->
    <script src="js/jquery.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
    <!-- Menu Toggle Script -->
    <script>
        $("#menu-toggle").click(function(e) {
            e.preventDefault();
            $("#wrapper").toggleClass("toggled");
        });
    </script>
</body>
</html>

CSS:

.as-header-wrapper{
    overflow-x: hidden;
}
/*!
 * Start Bootstrap - Simple Sidebar HTML Template (http://startbootstrap.com)
 * Code licensed under the Apache License v2.0.
 * For details, see http://www.apache.org/licenses/LICENSE-2.0.
 */

/* Toggle Styles */
 #wrapper {
    padding-left: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
#wrapper.toggled {
    padding-left: 250px;
}
#sidebar-wrapper {
    z-index: 1000;
    position: fixed;
    left: 250px;
    width: 0;
    height: 100%;
    margin-left: -250px;
    overflow-y: auto;
    background: #000;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
#wrapper.toggled #sidebar-wrapper {
    width: 250px;
}
#page-content-wrapper {
    width: 100%;
    position: absolute;
    padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
    position: absolute;
    margin-right: -250px;
}
/* Sidebar Styles */
 .sidebar-nav {
    position: absolute;
    top: 0;
    width: 250px;
    margin: 0;
    padding: 0;
    list-style: none;
}
.sidebar-nav li {
    text-indent: 20px;
    line-height: 40px;
}
.sidebar-nav li a {
    display: block;
    text-decoration: none;
    color: #999999;
}
.sidebar-nav li a:hover {
    text-decoration: none;
    color: #fff;
    background: rgba(255, 255, 255, 0.2);
}
.sidebar-nav li a:active, .sidebar-nav li a:focus {
    text-decoration: none;
}
.sidebar-nav > .sidebar-brand {
    height: 65px;
    font-size: 18px;
    line-height: 60px;
}
.sidebar-nav > .sidebar-brand a {
    color: #999999;
}
.sidebar-nav > .sidebar-brand a:hover {
    color: #fff;
    background: none;
}
@media(min-width:768px) {
    #wrapper {
        padding-left: 250px;
    }
    #wrapper.toggled {
        padding-left: 0;
    }
    #sidebar-wrapper {
        width: 250px;
    }
    #wrapper.toggled #sidebar-wrapper {
        width: 0;
    }
    #page-content-wrapper {
        padding: 20px;
        position: relative;
    }
    #wrapper.toggled #page-content-wrapper {
        position: relative;
        margin-right: 0;
    }
}

如果我可以防止侧边栏随内容滚动(比如在 sidebar-wrapper 中设置 position: absolute;)并保持侧边栏的高度与内容,我认为可以正常工作。

感谢任何建议!

最佳答案

需要在 css 表中进行一些修复......

#wrapper {
    padding-left: 0;
    overflow-y:none; //THIS -> ADD
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
    max-width:100%;  //THIS -> ADD
}
#wrapper.toggled #page-content-wrapper {
    position: absolute;
    margin-right:-250px; //THIS -> REMOVE
}

编辑:

经过几次测试,这是您尝试执行的操作:http://jsfiddle.net/DTcHh/4759/

但是当您在小屏幕上时,几乎没有什么问题需要通过样式来解决...菜单样式似乎发生了变化(从黑色背景变为白色背景)和一些其他问题...

这都是关于 css“位置”和大小的……但我认为这是一个很好的起点。

关于html - 停止侧边栏在 y 轴上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28694786/

相关文章:

javascript - 如何使用 Google 应用脚本一次上传多个文件到 Google Drive?

javascript - 将超链接从用户(文本)转换为图像

html - hr标签的自定义CSS

javascript - Web 原型(prototype)制作 HTML CSS 和 Javascript 的 Playground

angularjs - Bootstrap 弹出框在 AngularJs ng-repeat 中不起作用

javascript - Bootstrap 弹出窗口仅从一个链接打开

html - 使用 Ruby 和 Nokogiri 抓取 HTML 表格

html - 多文本应该在悬停状态下一起改变

css - 绝对定位的图像突破包装 div

javascript - 是否可以从远程文件更改 BootstrapDialog 标题和其他属性?