Sticky (Fixed) SideNav Layout with CSS


完成圖

構成的structure

HTML tag

1
2
3
4
<div class="container">
	<div id="sidenav><!--Fixed Sidenav Goes Here--></div>
	<div id="content"><!--Content Goes Here--></div>
</div>

利用簡單的技巧,
把sidenav 的加上position 和fix的properties.
position: fixed;
float: left;
CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.container {
	width: 980px;
	margin: 0 auto;
	overflow: hidden;
	background: url(container_stretch.gif);
	font-size: 1.2em;
	position: relative;
}
#sidenav {
	width: 300px;
	position: fixed; /*--Fix the sidenav to stay in one spot--*/
	float: left; /*--Keeps sidenav into place when Fixed positioning fails--*/
}
#content {
	float: right; /*--Keeps content to the right side--*/
	width: 640px;
	padding: 0 20px 20px;
}

可惜ie6不支援fixed position property
所以要workaround..

1
2
3
4
5
*html #sidenav {
	position: absolute;
	left: expression( ( 0   ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) )   'px' );
	top: expression( ( 0   ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) )   'px' );
}


Building the SideNav – HTML/CSS
加入SideNav 的content。
html:

1
2
3
4
5
6
7
8
9
10
11
<a href="/"><img src="logo.gif" alt="" /></a>
	<h2 class="categories">Categories</h2>
	<ul>
        	<li><a href="#">Link</a></li>
        	<li><a href="#">Link</a></li>
        </ul>
        <h2 class="sites">Other Sites</h2>
        <ul>
        	<li><a href="#">Link</a></li>
        	<li><a href="#">Link</a></li>
	</ul>

css: level 2 的headings tag 是轉換了成為background image。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#sidenav h2 {
	text-indent: -99999px; /*--Push the text off of the page--*/
	height: 41px;
	padding: 0; margin: 15px 0 5px;
	background-position:  20px top; /*--Set position of each heading background--*/
}
h2.categories {background: url(h2_categories.gif) no-repeat ;} /*--Background image for "category" heading--*/
h2.sites {background: url(h2_othersites.gif) no-repeat ;} /*--Background image for "other sites" heading--*/
#sidenav ul {
	margin: 0; padding: 0 20px 30px 20px;
	list-style: none;
	background: url(sidenav_hr.gif) no-repeat right bottom; /*--thin break line underneath the navigation--*/
}
#sidenav ul li{
	margin: 0; padding:  0;
	display: inline; /*--Fixes IE6 bug of double margin--*/
}
#sidenav ul li a{
	display: block;
	margin: 0; padding:  5px 0 5px 15px;
	background: url(sidenav_arrow.gif) no-repeat left center;
	text-decoration: none;
	color: #333;
}
#sidenav ul li a:hover {
	color: #999;
}

原文還提及,怎樣用jquery 都一個fix 當左面的sidenav 太大。
不過chowky 認為不太重要~~所以有興趣自己去看看吧

License: License Free
Source: http://designm.ag/tutorials/sticky-sidenav-layout/
Demo: http://www.sohtanaka.com/web-design/examples/sticky-nav/

  • Share/Bookmark
  1. No comments yet.
(will not be published)

Anti-Spam Protection by WP-SpamFree