问题
I am trying to create a side bar with the appearance similar to the following image.
http://i.imgur.com/AkFeEoh.png
As you can see in the image, I want a sidebar that is by default open and if the user closes it a cookie will save whether or not the sidebar is open. The sidebar being opened or closed would also effect the size of the page content, which I tried to show with %'s in the mockup I made.
I have been searching around for something similar, but the closest I can find is the following jsfiddle made for another user who wanted something similar... but not quite the same.
jsfiddle.net/7ZBQa
can anyone please help me with this, any help would be much appreciated =)
回答1:
required result with responsive design
HTML
<div class=" sidebar-at-left" id="main">
<div id="content">Content</div>
<div id="sidebar">Sidebar</div>
<a href="#" id="separator"></a>
<div class="clearer"> </div>
</div>
CSS
#content,#sidebar {
line-height: 300px;
text-align: center;
border: 1px solid;
}
#sidebar {
background-color: #DEF;
border-color: #BCD;
display: none;
}
#content {
background-color: #EFE;
border-color: #CDC;
width: 97%;
}
.use-sidebar #content {width: 64%;}
.use-sidebar #sidebar {
display: block;
width: 32%;
}
.sidebar-at-left #sidebar {margin-right: 1%;}
.sidebar-at-right #sidebar {margin-left: 1%;}
.sidebar-at-left #content, .use-sidebar.sidebar-at-right #sidebar, .sidebar-at-right #separator {float: right;}
.sidebar-at-right #content, .use-sidebar.sidebar-at-left #sidebar, .sidebar-at-left #separator {float: left;}
#separator {
background-color: #000;
border: 1px solid #CCC;
display: block;
outline: none;
width: 1%;
margin-top: 145px;
}
.use-sidebar #separator {
background: #000;
border-color: #FFF;
width:1%;
}
#separator:hover {
border-color: #ABC;
background: #DEF;
}
jQuery
<script>
$(document).ready(function(){
// Variables
var objMain = $('#main');
// Show sidebar
function showSidebar(){
objMain.addClass('use-sidebar');
}
// Hide sidebar
function hideSidebar(){
objMain.removeClass('use-sidebar');
}
// Sidebar separator
var objSeparator = $('#separator');
objSeparator.click(function(e){
e.preventDefault();
if ( objMain.hasClass('use-sidebar') ){
hideSidebar();
}
else {
showSidebar();
}
}).css('height', objSeparator.parent().outerHeight() + 'px');
});
</script>
DEMO
来源:https://stackoverflow.com/questions/23043344/jquery-sliding-side-bar-right-to-left-with-resizing-content