我正试图获得一个可转换的侧边栏:
没有侧边栏:
>> | Main text
| goes here
使用侧边栏:
H1 << | Main text
H2 | goes here
(以下是行为示例:http://pythonhosted.org/cloud_sptheme - 请参阅侧栏上的按钮。使用其代码库并不容易,因为它不是基于引导程序的)
我从一个使用Bootstrap 2和jQuery 1.8的工作示例开始:jsfiddle,SO question,并尝试使其在Bootstrap 3上运行。这是一个代码(code on bootply):
的javascript:
$.asm = {};
$.asm.panels = 1;
function sidebar(panels) {
$.asm.panels = panels;
if (panels === 1) {
$('#content').removeClass('col-md-9');
$('#content').addClass('col-md-12');
$('#sidebar1').removeClass('show');
$('#sidebar1').addClass('hide');
} else if (panels === 2) {
$('#content').removeClass('col-md-12');
$('#content').addClass('col-md-9');
$('#sidebar1').removeClass('hide');
$('#sidebar1').addClass('show');
}
}
$('#toggleSidebar').click(function() {
if ($.asm.panels === 1) {
$('#toggleSidebar').addClass('fa-backward');
$('#toggleSidebar').removeClass('fa-forward');
return sidebar(2);
} else {
$('#toggleSidebar').removeClass('fa-backward');
$('#toggleSidebar').addClass('fa-forward');
return sidebar(1);
}
})
的CSS:
#toggleSidebar {
position:fixed;
display:block;
left:0;
top:45px;
color:#779DD7;
padding:2px 4px;
}
hide {
display: none;
}
show {
display: inherit;
}
HTML:
<div class="container">
<div class="row">
<div class="col-md-3 hide" id="sidebar1">
<div id="sidebar" class="bs-sidebar nav bs-sidenav pre-scrollable" role="complementary">
Toc goes here.
</div>
</div>
<div class="col-md-12" id="content">
Main text goes here.
</div>
</div>
<a id="toggleSidebar" href="#toggleSidebar1" class="fa fa-forward"</a>
</div>
修改:
关闭画布不是我需要的:它不保留地平线空间,它将div移动到右边,并且div离开屏幕。将col-md-9
发送到col-md-12
然后回来 - 是我能想到的最好的问题。这应该是简单的javascript,我不擅长。
答案 0 :(得分:2)
我认为你正在寻找一个“离开画布”的侧边栏。以下是从左侧滑入的2个画布外Bootstrap示例..
http://www.bootstrapzero.com/bootstrap-template/off-canvas-sidebar http://www.bootstrapzero.com/bootstrap-template/facebook
这两个示例都使用媒体查询来检测浏览器并相应地放置侧边栏。
答案 1 :(得分:2)
看看http://getbootstrap.com/examples/offcanvas/。这是一个由bootstrap本身制作的offcanvas示例。
答案 2 :(得分:0)
哦,我解决了它:
JS:
$(document).ready(function(){
$.asm = {};
$.asm.panels = 2;
$('#toggleSidebar').click(function(){
if ($.asm.panels === 1) {
$('#toggleSidebar span').attr({'class': 'glyphicon glyphicon-backward'});
$('#content').attr({'class': 'col-md-9'});
$('#sidebar1').show();
$.asm.panels = 2;
} else {
$('#toggleSidebar span').attr({'class': 'glyphicon glyphicon-forward'});
$('#content').attr({'class': 'col-md-12'});
$('#sidebar1').hide();
$.asm.panels = 1;
}
});
});
的CSS:
#toggleSidebar {
position:fixed;
display:block;
left:0;
top:45px;
color:#779DD7;
padding:2px 4px;
}
HTML:
<div class="container">
<div class="row">
<div class="col-md-3" id="sidebar1">
<div id="sidebar" class="bs-sidebar nav bs-sidenav pre-scrollable" role="complementary">
Toc goes here.
</div>
</div>
<div class="col-md-9" id="content">
Main text goes here.
</div>
</div>
<button type="button" id="toggleSidebar" class="btn btn-primary"><span class="glyphicon glyphicon-backward"></span></button>
</div>