IE6 fixed的完美解決方案
更新時(shí)間:2011年03月31日 00:38:02 作者:
IE7已經(jīng)支持position:fixed了,而我們的IE6呢?還繼續(xù)使用js事件?消耗資源,破壞結(jié)構(gòu),畫面閃耀。
第一種方法:純CSS
目前網(wǎng)上有的比較多的是這種解決方案(純CSS):
html{overflow:hidden;}
body{height:100%;overflow:auto;}
#rightform form{position:absolute;right:30px;top50px;}
這個(gè)方法有一個(gè)bug未解決:在IE6下會(huì)把所有position:absolute都變成“浮動(dòng)”的元素;還有使用js方法滾動(dòng)滾動(dòng)條時(shí)會(huì)出現(xiàn)對(duì)象閃爍,如下方法結(jié)合了CSS和js的辦法,解決了以上的問題。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>IE6 fixed</title>
<style type="text/css">
*
{
margin: 0px;
padding: 0px;
}
body {
_background-image: url(about:blank); /*用瀏覽器空白頁面作為背景*/
_background-attachment: fixed; /* prevent screen flash in IE6 確保滾動(dòng)條滾動(dòng)時(shí),元素不閃動(dòng)*/
}
#topNav {
width: 980px;
z-index: 100; /*設(shè)置浮動(dòng)層次*/
overflow: visible;
position: fixed;
top: 50px; /* 其他瀏覽器下定位,在這里可設(shè)置坐標(biāo)*/
_position: absolute; /*IE6 用absolute模擬fixed*/
_top: expression(documentElement.scrollTop + 50 + "px"); /*IE6 動(dòng)態(tài)設(shè)置top位置*/
/* documentElement.scrollTop 設(shè)置浮動(dòng)元素始終在瀏覽器最頂,可以加一個(gè)數(shù)值達(dá)到排版效果 */
background-color:#0000FF;
height: 31px;
}
.show{
position:absolute;
top:500px;
left:400px;
border:#ff0000 1px solid;
}
</style>
</head>
<body>
<div class="jd_menu" id="topNav">1111</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<div class="show">show</div>
</body>
</html>
只實(shí)現(xiàn)了垂直方向的fixed。若要實(shí)現(xiàn)水平方向的fixed,設(shè)置_left:expression(documentElement.scrollLeft + "px");
利用給<body>設(shè)置固定的背景,防止?jié)L動(dòng)條滾動(dòng)時(shí)的閃動(dòng);如果<body>中要設(shè)置滾動(dòng)的背景而產(chǎn)生沖突,可以把代碼寫在html選擇器里面,如:
html {
_background-image: url(about:blank);
_background-attachment: fixed; /* prevent screen flash in IE6 */
}
body {
background-image: url(1.jpg);
background-attachment: scroll;
}
第二種方法 JavaScript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{ margin:0px; padding:0px; font-size:12px; line-height:22px;}
p{ margin:0px; padding:36px;}
.float2{ position:absolute; padding:26px; border:#999999 3px solid; background-color:#3399FF; right:200px; top:200px;}
.fixed{ position:absolute; right:60px; top:100px; border:#666666 3px solid; background-color:#CCCCCC; padding:26px;}
</style>
<script language="javascript" type="text/javascript">
window.onload=function(){
var n=100; //top值
var obj=document.getElementById("fixed"); //position:fixed對(duì)象
window.onscroll=function(){obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';}
window.onresize=function(){obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';}
}
</script>
<title>position_fixed測(cè)試</title>
</head>
<body>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<div class="float2">浮動(dòng)的內(nèi)容2</div>
<div class="fixed" id="fixed">我的位置是固定的!拉動(dòng)滾動(dòng)條看效果。</div>
</body>
</html>
ie6對(duì)postion:fixed的完美解決方案
今天去一老外站看到了這他站上的十分平滑但卻沒有js,好奇,原來。。巧妙啊,分享下,相對(duì)而言比較節(jié)省資源。但效果好,使用方便,兼顧w3c。哈哈
<!-- compliance patch for microsoft browsers -->
<!--[if lt IE 7]><link rel="stylesheet" href="ie-stuff.css" type="text/css" media="screen"/><![endif]-->
ie-stuff.css
#footer {
position: absolute;
bottom: auto;
clear: both;
top:expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop
+(documentElement.clientHeight-this.clientHeight) - 1
: document.body.scrollTop
+(document.body.clientHeight-this.clientHeight) - 1);
}
目前網(wǎng)上有的比較多的是這種解決方案(純CSS):
復(fù)制代碼 代碼如下:
html{overflow:hidden;}
body{height:100%;overflow:auto;}
#rightform form{position:absolute;right:30px;top50px;}
這個(gè)方法有一個(gè)bug未解決:在IE6下會(huì)把所有position:absolute都變成“浮動(dòng)”的元素;還有使用js方法滾動(dòng)滾動(dòng)條時(shí)會(huì)出現(xiàn)對(duì)象閃爍,如下方法結(jié)合了CSS和js的辦法,解決了以上的問題。
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>IE6 fixed</title>
<style type="text/css">
*
{
margin: 0px;
padding: 0px;
}
body {
_background-image: url(about:blank); /*用瀏覽器空白頁面作為背景*/
_background-attachment: fixed; /* prevent screen flash in IE6 確保滾動(dòng)條滾動(dòng)時(shí),元素不閃動(dòng)*/
}
#topNav {
width: 980px;
z-index: 100; /*設(shè)置浮動(dòng)層次*/
overflow: visible;
position: fixed;
top: 50px; /* 其他瀏覽器下定位,在這里可設(shè)置坐標(biāo)*/
_position: absolute; /*IE6 用absolute模擬fixed*/
_top: expression(documentElement.scrollTop + 50 + "px"); /*IE6 動(dòng)態(tài)設(shè)置top位置*/
/* documentElement.scrollTop 設(shè)置浮動(dòng)元素始終在瀏覽器最頂,可以加一個(gè)數(shù)值達(dá)到排版效果 */
background-color:#0000FF;
height: 31px;
}
.show{
position:absolute;
top:500px;
left:400px;
border:#ff0000 1px solid;
}
</style>
</head>
<body>
<div class="jd_menu" id="topNav">1111</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<div class="show">show</div>
</body>
</html>
只實(shí)現(xiàn)了垂直方向的fixed。若要實(shí)現(xiàn)水平方向的fixed,設(shè)置_left:expression(documentElement.scrollLeft + "px");
利用給<body>設(shè)置固定的背景,防止?jié)L動(dòng)條滾動(dòng)時(shí)的閃動(dòng);如果<body>中要設(shè)置滾動(dòng)的背景而產(chǎn)生沖突,可以把代碼寫在html選擇器里面,如:
復(fù)制代碼 代碼如下:
html {
_background-image: url(about:blank);
_background-attachment: fixed; /* prevent screen flash in IE6 */
}
body {
background-image: url(1.jpg);
background-attachment: scroll;
}
第二種方法 JavaScript
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{ margin:0px; padding:0px; font-size:12px; line-height:22px;}
p{ margin:0px; padding:36px;}
.float2{ position:absolute; padding:26px; border:#999999 3px solid; background-color:#3399FF; right:200px; top:200px;}
.fixed{ position:absolute; right:60px; top:100px; border:#666666 3px solid; background-color:#CCCCCC; padding:26px;}
</style>
<script language="javascript" type="text/javascript">
window.onload=function(){
var n=100; //top值
var obj=document.getElementById("fixed"); //position:fixed對(duì)象
window.onscroll=function(){obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';}
window.onresize=function(){obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';}
}
</script>
<title>position_fixed測(cè)試</title>
</head>
<body>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<p>測(cè)試內(nèi)容.........</p>
<div class="float2">浮動(dòng)的內(nèi)容2</div>
<div class="fixed" id="fixed">我的位置是固定的!拉動(dòng)滾動(dòng)條看效果。</div>
</body>
</html>
ie6對(duì)postion:fixed的完美解決方案
今天去一老外站看到了這他站上的十分平滑但卻沒有js,好奇,原來。。巧妙啊,分享下,相對(duì)而言比較節(jié)省資源。但效果好,使用方便,兼顧w3c。哈哈
<!-- compliance patch for microsoft browsers -->
<!--[if lt IE 7]><link rel="stylesheet" href="ie-stuff.css" type="text/css" media="screen"/><![endif]-->
ie-stuff.css
復(fù)制代碼 代碼如下:
#footer {
position: absolute;
bottom: auto;
clear: both;
top:expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop
+(documentElement.clientHeight-this.clientHeight) - 1
: document.body.scrollTop
+(document.body.clientHeight-this.clientHeight) - 1);
}
相關(guān)文章
小程序?qū)崿F(xiàn)左滑刪除的效果的實(shí)例代碼
這篇文章主要介紹了小程序?qū)崿F(xiàn)左滑刪除的效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10解決bootstrap中使用modal加載kindeditor時(shí)彈出層文本框不能輸入的問題
這篇文章主要介紹了解決bootstrap中使用modal加載kindeditor時(shí)彈出層文本框不能輸入的問題,需要的朋友可以參考下2017-06-06Js模塊打包exports require import的用法和區(qū)別
這篇文章主要介紹了Js模塊打包exports require import的用法和區(qū)別,對(duì)模塊打包感興趣的同學(xué),可以參考下2021-05-05微信小程序 調(diào)用微信授權(quán)窗口相關(guān)問題解決
這篇文章主要介紹了微信小程序 調(diào)用微信授權(quán)窗口相關(guān)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07js 動(dòng)態(tài)添加元素(div、li、img等)及設(shè)置屬性的方法
下面小編就為大家?guī)硪黄猨s 動(dòng)態(tài)添加元素(div、li、img等)及設(shè)置屬性的方法。小編覺得聽不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07Three.Js實(shí)現(xiàn)顏色自定義變換效果實(shí)例
這篇文章主要給大家介紹了關(guān)于Three.Js實(shí)現(xiàn)顏色自定義變換效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02JS實(shí)現(xiàn)頁面鼠標(biāo)點(diǎn)擊出現(xiàn)圖片特效
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)頁面鼠標(biāo)點(diǎn)擊出現(xiàn)圖片特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08