<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pivot Code &#187; hack</title>
	<atom:link href="http://www.pivotcode.com/tag/hack/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pivotcode.com</link>
	<description>欢迎来我的小思想绽放的地方……</description>
	<lastBuildDate>Thu, 26 Aug 2010 02:19:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>IE6固定定位position属性fixed BUG</title>
		<link>http://www.pivotcode.com/ie6-position-fixed-bug/</link>
		<comments>http://www.pivotcode.com/ie6-position-fixed-bug/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 04:53:25 +0000</pubDate>
		<dc:creator>刘 铭森</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[BUG]]></category>
		<category><![CDATA[fixed]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[relative]]></category>
		<category><![CDATA[renren]]></category>
		<category><![CDATA[人人网]]></category>
		<category><![CDATA[固定定位]]></category>
		<category><![CDATA[定位]]></category>
		<category><![CDATA[相对定位]]></category>
		<category><![CDATA[绝对定位]]></category>

		<guid isPermaLink="false">http://www.pivotcode.com/?p=430</guid>
		<description><![CDATA[当需要把一个元素永远固定在浏览器的一个位置的时候，那么就需要用到position的fixed属性。然后用top和left设置元素的位置。 这个效果也应用在人人网的个人首页底部的工具栏。 常去人人网的朋友肯定会发现，如果用IE6浏览器访问人人网的话，这个工具条以前是不存在的。后来，人人已经通过js实现此功能，但是它的效果和正常浏览器下访问是不同的，即每当拖动滚动条它就会隐藏，当停止时再出现。所以，值得肯定的是IE6不支持这个position的fixed属性。 这个问题也一直都在困扰着我，因为我一直都在想把这个效果实现在我的一个项目上，而又避开使用javascript。 后来在‘幸福收藏夹’博客，找到了解决方案——利用position:absolute定位在IE6下实现固定定位的效果，以下是实现原理： fixed元素的绝对位置相对于HTML元素，滚动条则是基于body元素而不属于html元素。 当我们拖动滚动条滚动的时候，内容都会随窗口滚动，这时滚动的是body元素。HTML元素被浏览器判定为相对定位的根级元素，而我们定义的position:absolute元素也在HTLML元素当中，属于HTML元素的子元素。因此我们所定义的absolute元素会根据HTML元素的大小而进行绝对定位。如果我们未曾对HTML元素设置样式的情况下，它的大小是随内容的大小而变的,这样一来如果我们在其中使用绝对定位，并不能实现我们想得到的固定定位的效果。因此我们需要给我们需要绝对定位元素的相对定位盒模型指定一个绝对大小，即窗口当前的大小。但是body元素仍需保持其原有大小，否则就没有滚动条了。这时我们需要将超出HTML元素的内容隐藏掉，就可以让HTML元素永远保持在一个固定大小了。 代码如下： html&#123;overflow:hidden;&#125; // Important（必须）。 body&#123;overflow:auto;height:100%&#125; 代码规定了html将只有当前窗口的大小，溢出的内容隐藏。但是body仍会随高度自动变化。于是我们就可以利用position:absolute来把元素固定在窗口任意位置了。例如仿人人网定位元素在底边： 示例的完整代码： &#60;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&#62; &#60;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&#62; &#60;head&#62; &#60;meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=utf-8&#34; /&#62; &#60;title&#62;ie6-position-fixed-bug&#60;/title&#62; &#60;style&#62; * { padding:0; margin:0; } html{_overflow:hidden;}/*IE6hack,必须*/ body{height:100%;overflow:auto;} #footer .bottom { width:580px; height:20px; background-color:#ddd; border:1px solid #aaa; position:fixed; _position:absolute;/*IE6hack,必须*/ bottom:0; left:50%; margin-left:-290px; } &#60;/style&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>当需要把一个元素永远固定在浏览器的一个位置的时候，那么就需要用到position的fixed属性。然后用top和left设置元素的位置。</p>
<p>这个效果也应用在人人网的个人首页底部的工具栏。</p>
<p>常去人人网的朋友肯定会发现，如果用IE6浏览器访问人人网的话，这个工具条以前是不存在的。后来，人人已经通过js实现此功能，但是它的效果和正常浏览器下访问是不同的，即每当拖动滚动条它就会隐藏，当停止时再出现。所以，值得肯定的是IE6不支持这个position的fixed属性。</p>
<p>这个问题也一直都在困扰着我，因为我一直都在想把这个效果实现在我的一个项目上，而又避开使用javascript。<br />
<span id="more-430"></span><br />
后来在‘幸福收藏夹’博客，找到了解决方案——利用position:absolute定位在IE6下实现固定定位的效果，以下是实现原理：</p>
<blockquote><p>fixed元素的绝对位置相对于HTML元素，滚动条则是基于body元素而不属于html元素。<br />
当我们拖动滚动条滚动的时候，内容都会随窗口滚动，这时滚动的是body元素。HTML元素被浏览器判定为相对定位的根级元素，而我们定义的position:absolute元素也在HTLML元素当中，属于HTML元素的子元素。因此我们所定义的absolute元素会根据HTML元素的大小而进行绝对定位。如果我们未曾对HTML元素设置样式的情况下，它的大小是随内容的大小而变的,这样一来如果我们在其中使用绝对定位，并不能实现我们想得到的固定定位的效果。因此我们需要给我们需要绝对定位元素的相对定位盒模型指定一个绝对大小，即窗口当前的大小。但是body元素仍需保持其原有大小，否则就没有滚动条了。这时我们需要将超出HTML元素的内容隐藏掉，就可以让HTML元素永远保持在一个固定大小了。</p></blockquote>
<p>代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">html<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span> // Important（必须）。
body<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>height<span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>代码规定了html将只有当前窗口的大小，溢出的内容隐藏。但是body仍会随高度自动变化。于是我们就可以利用position:absolute来把元素固定在窗口任意位置了。例如仿人人网定位元素在底边：</p>
<p><iframe width='580' height='300' src='http://www.pivotcode.com/demo/ie6-position-fixed-bug.html'></iframe></p>
<p>示例的完整代码：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;ie6-position-fixed-bug&lt;/title&gt;
&lt;style&gt;
* {
	padding:0;
	margin:0;
}
html{_overflow:hidden;}/*IE6hack,必须*/
body{height:100%;overflow:auto;}
#footer .bottom {
	width:580px;
	height:20px;
	background-color:#ddd;
	border:1px solid #aaa;
	position:fixed;
	_position:absolute;/*IE6hack,必须*/
	bottom:0;
	left:50%;
	margin-left:-290px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;h1&gt;测试定位&lt;/h1&gt;
占位文字
&lt;/div&gt;
&lt;div id=&quot;footer&quot;&gt;
  &lt;div class=&quot;bottom&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>注：本文参考自幸福收藏夹</p>
<h2  class="related_post_title">相关日志</h2><ul class="related_post"><li><a href="http://www.pivotcode.com/ie-6-overflow-element/" title="也谈IE6下block元素float多出文字BUG">也谈IE6下block元素float多出文字BUG</a></li><li><a href="http://www.pivotcode.com/microsoft-expression-web-3-superpreview/" title="体验篇：Microsoft Expression Web 3 SuperPreview全攻略">体验篇：Microsoft Expression Web 3 SuperPreview全攻略</a></li><li><a href="http://www.pivotcode.com/css-text-shadow/" title="css文字阴影">css文字阴影</a></li><li><a href="http://www.pivotcode.com/ie6-img-max-widht/" title="IE6下图像的最大宽度">IE6下图像的最大宽度</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pivotcode.com/ie6-position-fixed-bug/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
