我是一个初学者开发人员,我希望使用Bootstrap构建画布外的侧面板。切换它的按钮将在我的顶部导航中显示为菜单项。我发现的问题是,在线上用于此类内容的大多数代码(例如https://codepen.io/j_holtslander/pen/XmpMEphttp://www.jasny.net/bootstrap/examples/navmenu-reveal/)都是用于构建侧面导航的。我的导航将改为常规的固定顶部导航,而画布外的面板将是div,其内容将所有内容(包括固定顶部导航)推送到侧面。

最初,我认为我的代码看起来像这样:

HTML:

<div id="main-content-wrapper">
   <nav class="navbar navbar-default" role="navigation">
    <ul>
     <li>Some menu item</li>
     <li><!--button to toggle side panel here--></li>
    </ul>
   </nav>

   <!--Site content goes here-->

</div>
<div id="side-content-wrapper"><!--Side panel content here--></div>


但是我不知道Javascript / JQuery和CSS会如何寻找,以及如何使侧面板推送主要内容,包括单击按钮时左侧的导航栏。就像我说的,我发现的示例有可插入的侧面导航栏,但我要放在侧面的div不是导航栏。

任何线索将不胜感激!

谢谢

最佳答案

导航栏标题内有一个汉堡图标来更改品牌名称

<a class="navbar-brand" href="#">
    <span class="glyphicon glyphicon-menu-hamburger" onclick="openSideNav()"></span>
</a>


并在您体内的某处添加侧板

<div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
</div>


然后添加样式以默认隐藏sidenav并仅在单击汉堡包时打开

.sidenav {
height: 100%; /* 100% Full-height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 50px;
left: 0;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}


最后使用javascript切换侧边栏

function openSideNav() {
    document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}

关于javascript - 如何制作Bootstrap侧面板/div(不是菜单),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41195511/

10-11 12:56