<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<title>jessey.net | articles | fixed sidebars</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body {
margin: 0em;
padding: 0em;
background: black none;
color: white;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
}
.invisible {
display: none;
}
#fixed {
position: absolute;
top: 0;
left: 0;
width: 10em;
height: 100%;
background: yellow;
color: black;
}
body > #fixed {
position: fixed;
}
#content {
margin-left: 10em;
padding: 1em;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
line-height: 1.5;
}
code, pre {
font-family: monospace;
font-size: large;
font-weight: bold;
background: transparent none;
color: yellow;
}
.center { text-align: center; }
h1 { font-size: 4em; text-align: right; }
h2 { margin-top: 2em; }
a:link { background: transparent none; color: aqua; }
a:visited { background: transparent none; color: aqua; }
a:active { background: transparent none; color: aqua; }
a:hover { background: transparent none; color: fuchsia; }
#fixed a {
display: block;
text-decoration: none;
padding: 0.5em;
background: navy none;
color: white;
font-weight: bold;
font-size: smaller;
margin-bottom: 2px;
}
#fixed a:hover {
background: red none;
color: white;
}
div.address {
border-top: 2px solid black;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
div.address img {
border: 0px;
text-align: left;
}
-->
</style>
<!--[if IE 6]>
<link rel="stylesheet" href="IE6hack.css" type="text/css" />
<![endif]-->
</head>
<body>
<div id="content">
<h1>fixed sidebars</h1>
<h2>banish evil frames for good?</h2>
<p>The holy grail for many web designers is for there to be some support for the <code>position: fixed</code> style rule. Unfortunately, only the stable versions of Gecko (Mozilla 1.0, Netscape 7.0 or better) and Opera 6.0 support it properly.</p>
<p>In theory, a sidebar on the left of the screen that is fixed with respect to the browser window, or <em>viewport</em> can be created with simple <acronym title="Cascading Style Sheets">CSS</acronym>. Let us consider a simple example. We want a sidebar that is positioned on the left. It is going to be 10em wide and extend from the top to the bottom of the viewport. Everything to the right of the sidebar will scroll like a normal document. The layout code is simple:</p>
<pre>#fixed {
position: absolute;
top: 0;
left: 0;
width: 10em;
height: 100%;
}
body > #fixed {
position: fixed;
}
#content {
margin-left: 10em;
}</pre>
<p>Here we have our fixed sidebar, <code>div.fixed</code> and our scrolling content, <code>#content</code>. The reason the code uses <code>position: absolute</code> and then alters the <code>div</code> with <code>body > div.fixed { position: fixed; }</code> is that it is important to allow for browsers that don't support <code>position: fixed</code> at all.</p>
<h2>but there's a problem</h2>
<p>The most popular browser in the world is Microsoft Internet Explorer. At the time of writing, version 5.x has a 49% market share and version 6.x has a 41% market share. Both of these browsers fail to implement <code>position: fixed</code> correctly. Internet Explorer 6.0 has a special "standards mode" that works if the document you create has a proper <code>DOCTYPE</code> declared. If not, 6.0 reverts to "quirks mode", which effectively emulates version 5.5.</p>
<p>The "standards mode" can be forced to correctly implement <code>position: fixed</code> by using a workaround (commonly referred to as a <em>hack</em>) that uses a proprietary extension that Microsoft has engineered into Internet Explorer called <em>Conditional Comments</em>. First, an external stylesheet must be created with the following code:</p>
<pre>html {
overflow: hidden;
}
body {
height: 100%;
overflow: auto;
}</pre>
<p>For this particular article, I have saved the stylesheet as <a href="IE6hack.css" title="External Stylesheet"><code>IE6hack.css</code></a>. Then I have added the following code, <strong>AFTER</strong> the <code>style</code> tag but still within the <code>head</code> of the page:</p>
<pre><!--[if IE 6]>
<link rel="stylesheet"
href="IE6hack.css"
type="text/css" />
<![endif]--></pre>
<p>Basically, if the browser encountered is Internet Explorer 6.0 (or better), the conditional comment loads the <code>IE6hack.css</code> style sheet. The effect is that Internet Explorer 6.x mimics the correct behavior of <code>position: fixed</code>. Unfortunately, nothing can be done (that I am aware of) for previous version of Internet Explorer unless some sort of scripting is used.</p>
<p>This is not quite the end of the story, however. If the browser window width is resized so that it is narrower than the width of some fixed-width content, such as an image or text within a <code>pre</code> element, the scrollbar will be overlapped and disappear, making it impossible for the user to scroll down the page. A horizontal scrollbar will not appear to help out the user. I have not been able to find a satisfactory solution to this. In this document, I have been forced to ensure anything I've put in <code>pre</code> tags has not been too wide, which is why I was forced to "spread out" the stylesheet reference in the previous code example. Fortunately, because this is part of the hack itself, it will not be a problem for non-Internet Explorer users.</p>
<h2>update!</h2>
<p><strong>09/22/04:</strong> <a href="http://www.seweso.com/blog/" title="Seweso's blog">Wouter Schut</a> has written to me with a solution to the scrollbar problem. Simply by adding <code>width: 100%;</code> to the <code>body</code> declaration in <a href="IE6hack.css" title="External Stylesheet"><code>IE6hack.css</code></a>, the scrollbar remains in place as expected. Thank you, Wouter.</p>
<h2>jazz it up a bit</h2>
<p>For this page, I have jazzed-up the fixed sidebar by placing links in it that take advantage of the <code>display: block</code> property of <acronym title="Cascading Style Sheets">CSS</acronym>. All I've done is apply the following style rules to the links within the fixed <code>div</code>:</p>
<pre>#fixed a {
display: block;
text-decoration: none;
padding: 0.5em;
background: navy none;
color: white;
font-weight: bold;
font-size: smaller;
margin-bottom: 2px;
}
#fixed a:hover {
background: red none;
color: white;
}</pre>
<p>Pretty straightforward, huh? Another advantage to this approach is that it means you can put markup for the fixed <code>div</code> at the end of your code. This means that visually-impaired users who are using a speech device to read the page won't keep reading out all the navigation links each time a new page is loaded.</p>
<h2>information resource</h2>
<p>You can find out more about <a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/ccomment_ovw.asp"
title="Conditional Comments">conditional comments</a> from the <acronym title="Microsoft Developer Network">MSDN</acronym>
<a href="http://msdn.microsoft.com/default.asp" title="MSDN website">website</a>. As always, the best way to understand what is happening in these articles is to view the source code, but remember that this particular article <em>also</em> has a small, attached style sheet to make the Internet Explorer hack work. Thank you for reading.</p>
<div class="address">
<p><a href="http://validator.w3.org/check/referer" title="W3C XHTML validation tool">
<img src="http://www.w3.org/Icons/valid-xhtml11"
alt="XHTML 1.1 icon, indicating validated XHTML 1.1" height="31" width="88"
longdesc="/long.html#xhtml" /></a><span class="invisible"> | </span>
<a href="http://jigsaw.w3.org/css-validator/check/referer" title="W3C/Jigsaw CSS validation tool">
<img src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="CSS icon, indicating validated CSS" width="88" height="31"
longdesc="/long.html#css" /></a></p>
<p>© Simon Jessey, 2002</p>
</div>
</div>
<div id="fixed">
<a href="/" title="Return to the jessey.net home page">jessey.net</a><span class="invisible"> | </span>
<a href="/simon/" title="jessey.net | Simon">Simon</a><span class="invisible"> | </span>
<a href="/simon/articles/" title="jessey.net | Simon | articles">articles</a><span class="invisible"> | </span>
<a href="/simon/articles/006.html" title="Previous article">previous article</a><span class="invisible"> | </span>
<a href="/simon/articles/008.html" title="Next article">next article</a>
</div>
</body>
</html>
http://jessey.net/simon/articles/007.html
http://www.456bereastreet.com/lab/cssframes/
分享到:
相关推荐
Fixed sidebar icons sometimes being invisible on startup API: Added sublime.yes_no_cancel_dialog() API: Added sublime.expand_variables() API: Added Window.extract_variables() API: Added Sheet.view() ...
Title: Outlook 2003 SideBar V2.02 Alpha (*Updated 10/02/2005*) Description: A complete new rebuild of my award winning EyeDropper Control. Now a 100% Dependency free control. Better Drawing, Better ...
{ fixedObj : '#fixed-sidebar', // 滚动窗口时固定哪个框 bottomObj : '#footer', // 固定结束哪个框,当#fixed-sidebar 靠近#footer 时absolute fixedPos : 0, // 在窗口中要固定的位置,如果您的站点顶部导航栏...
fixed content height calculation with overlapping control sidebar fixed collapse/show functions (functions was swapped) fixed custom checkbox padding fixed layout-navbar–fixed on iOS & macOS ...
在这个模板中,侧边栏可能是通过设置`position: fixed`来实现固定在屏幕一侧的效果,这样即使用户滚动页面,侧边栏也会保持可见。 文件名"sidebar-bootstrap-master"暗示这是一个源代码仓库的主分支,可能包含HTML...
CSS方面,可能涉及到的关键样式有`position: fixed`,这将使元素相对于浏览器窗口定位,无论其在文档流中的原始位置如何。`top`属性可以设置固定元素距离顶部的距离,以确保其始终在可视区域内。同时,可能还会用到...
$sidebar.css('position', 'fixed').css('top', settings.topOffset + 'px'); } else if ($(window).scrollTop() $sidebar.offset().top + $sidebar.height() - settings.bottomOffset) { $sidebar.css('position...
var sidebar = $('.fixed-sidebar'); var topOffset = sidebar.offset().top; // 获取菜单初始距离顶部的距离 $(window).scroll(function() { if ($(this).scrollTop() > topOffset) { sidebar.css({position:...
Sidebar remembers which folders are expanded Tweaked window closing behavior when pressing ctrl+w / cmd+w Improved quote auto pairing logic Selected group is now stored in the session Added remember_...
New: Sidebar Toggler Button On Header New: Ajax Datatable Integration New: jQuery Flot Bar Chart Samples New: Button Dropdown Menu with Search box Enhancement: Datepicker with Disabled Past Dates ...
$('.sidebar-left').removeClass('fixed'); } if (scrollTop >= sidebarRightTop) { $('.sidebar-right').addClass('fixed'); } else { $('.sidebar-right').removeClass('fixed'); } }); }); /* CSS类...
sidebar.removeClass('fixed'); // 移除fixed类,恢复原始样式 } }); }); ``` 在上面的代码中,`$(window).scroll()`函数监听滚动事件,`scrollTop`表示当前滚动条的垂直位置。当用户向下滚动超过设定的`top...
sidebar.removeClass('fixed'); } }); ``` 在CSS中,我们添加`.fixed`类来改变侧边栏的位置,使其固定在顶部: ```css #sidebar { position: relative; transition: top 0.3s ease; /* 添加平滑过渡效果 */ } ...
在这段代码中,`.sidebar`是侧边栏的CSS选择器,`addClass('fixed')`和`removeClass('fixed')`会根据滚动位置添加或移除固定定位的样式类。 4. **JavaScript**: JavaScript是网页动态交互的核心,可以实现各种用户...
<script src="fixed_sidebar.js"> ``` 接下来,我们将编写jQuery代码,实现当滚动到页面某个位置时,右侧浮动板块变为固定定位,始终显示在视口右侧: ```javascript $(document).ready(function() { var ...
在IT行业中,侧边栏(Sidebar)是一种常见的网页布局元素,尤其在网站和应用程序设计中极为常见。侧边栏通常位于页面的左侧或右侧,用于提供导航、菜单、设置选项等辅助信息,帮助用户快速访问不同功能或内容。在这...
sidebar.style.position = 'fixed'; sidebar.style.top = '0'; } else { sidebar.style.position = 'static'; // 还原为静态定位 } } window.addEventListener('scroll', stickToTop); ``` 5. **动画效果*...
$('#sidebar').removeClass('fixed'); // 移除该类,让菜单恢复原状 } }); }); // 使用jQuery的.click()方法处理点击事件,实现菜单的伸展和收缩 $('#sidebar-toggle').click(function() { $('#sidebar ul')....
var sidebar = document.querySelector('.floating-sidebar'); sidebar.style.top = scrollTop + 'px'; }); ``` 4. **文字特效**:标签提到"JS特效-文字特效",这意味着可能还需要对悬浮窗口内的文字进行特殊...
New: Sidebar Toggler Button On Header New: Ajax Datatable Integration New: jQuery Flot Bar Chart Samples New: Button Dropdown Menu with Search box Enhancement: Datepicker with Disabled Past Dates ...