总结--IE6,IE7,IE8,火狐都支持:js/css 底部固定, 底部固定漂浮导航效果
2011-07-29 10:20
651 查看
在做项目的过程中遇到一个这样的问题,控制层在底部固定,最初用Position:fixed 是可以,但IE6不支持,最后在论坛上发了贴,得到了如下三种方法:
第一:net_lover 的回答,他是用JS来实现的,通过判断不同的浏览器
第二:cewin9999 的回答,他是通过CSS来实现的
<style type="text/css">
body{ background-color:#1f8bbb; }
.float_con{ background:url(../images/footer_bg.gif) repeat-x; height:28px; text-align:center; color:#fff; font:bold 12px/28px Tahoma, Geneva, sans-serif; bottom:0px; position:fixed; width:500px; background-color:#fff;color:#000;}
* html .float_con /* IE6 底部固定 */{position:absolute;bottom:auto;top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}
</style>
<div class="float_con" id="float_con">adddddddddddddddddddddsd</div>
第三:同事帮忙在网上找的代码,也是通过JS来实现的
/*
*target 要固定的元素对象,也可以是元素的id
*pos:object/string 指定固定到的位置,类型为object时,使用json方式如{right:200,bottom:50} ,为string时可选参数如下:
*cc,正中间,lc 左边,rc 右边
*lt 左上角,ct 上边,rt 右上角
*lb 左下角,cb 底部,rb 右下角
*/
var fixPosition = function(target, pos) {
this.target = this.g(target);
this.pos = pos;
this.init(); //
};
fixPosition.prototype = {
isFixed: !window.ActiveXObject || (navigator.userAgent.indexOf("MSIE 6") == -1 && document.compatMode == "CSS1Compat"),
ae: function(e, call) {
if (window.addEventListener) window.addEventListener(e, call, false);
else window.attachEvent("on" + e, call);
},
g: function(id) { return typeof (id) == "string" ? document.getElementById(id) : id; },
setPos: function() {//设置位置
var de = document.compatMode == "CSS1Compat" ? document.documentElement : document.body;
if (typeof (this.pos) == "string") {//
if (this.isFixed) {
switch (this.pos.charAt(0)) {
case "l":
this.target.style.left = "0px";
break;
case "r":
this.target.style.right = "0px";
break;
default:
this.target.style.left = (de.clientWidth - this.target.clientWidth) / 2 + "px";
break;
}
switch (this.pos.charAt(1)) {
case "t":
this.target.style.top = "0px";
break;
case "b":
this.target.style.bottom = "0px";
break;
default:
this.target.style.top = (de.clientHeight - this.target.clientHeight) / 2 + "px";
break;
}
} else {
switch (this.pos.charAt(0)) {
case "l":
this.target.style.left = de.scrollLeft + "px";
break;
case "r":
this.target.style.left = de.scrollLeft + de.clientWidth - this.target.clientWidth + "px";
break;
default:
this.target.style.left = de.scrollLeft + ((de.clientWidth - this.target.clientWidth) / 2) + "px";
break;
}
switch (this.pos.charAt(1)) {
case "t":
this.target.style.top = de.scrollTop + "px";
break;
case "b":
this.target.style.top = de.scrollTop + de.clientHeight - this.target.clientHeight + "px";
break;
default:
this.target.style.top = de.scrollTop + ((de.clientHeight - this.target.clientHeight) / 2) + "px";
break;
}
}
} else {
if (this.isFixed) {
for (var p in this.pos)
this.target.style[p] = this.pos[p] + "px";
} else {
for (var p in this.pos) {
switch (p.toLowerCase()) {
case "left":
this.target.style.left = de.scrollLeft + this.pos[p] + "px";
break;
case "right":
this.target.style.left = de.scrollLeft + de.clientWidth - this.target.clientWidth - this.pos[p] + "px";
break;
case "top":
this.target.style.top = de.scrollTop + this.pos[p] + "px";
break;
case "bottom":
this.target.style.top = de.scrollTop + de.clientHeight - this.target.clientHeight - this.pos[p] + "px";
break;
}
}
}
}
},
init: function() {
if (!this.pos)
throw Error("Invalid arguments [pos].");
this.target.style.position = this.isFixed ? "fixed" : "absolute";
var timer, o = this;
this.ae("resize", function() {//支持fixed的浏览器窗体大小改变时也重置位置,防止中间无法居中
clearTimeout(timer);
timer = setTimeout(function() { o.setPos(); }, 30);
});
if (!this.isFixed) {//滚动
this.ae("scroll", function() {
//clearTimeout(timer);
//timer=setTimeout(function() { o.setPos(); },30);
o.setPos();
});
}
this.setPos();
},
clearPos: function() {
this.target.style.left = "";
this.target.style.top = "";
this.target.style.right = "";
this.target.style.bottom = "";
}
}
PS:三种方法用一个简单的层来测试都OK,但是加入项目中最好用的却是第三种,虽然第三种看起来最麻烦最复杂~~不知道为什么第一种和第二种用层测试OK,但加入项目中在IE6里面还是不支持~~在此总结一下,或许下次能派上用场呢!!
PPS:http://hi.baidu.com/sunnyzhishui/blog/item/e3ce7e63e49ea6790d33faff.html (js 底部固定, 底部固定漂浮导航,无JS纯CSS定义)
http://wenwen.soso.com/z/q244593710.htm(IE6下让DIV固定显示在浏览器底部并水平位置居中如何做?)
注:这是在网上其他地方找的方法,虽然我没实现效果,也收集起来放一起吧~!或许可以给其他人提供借鉴呢!!
第一:net_lover 的回答,他是用JS来实现的,通过判断不同的浏览器
第二:cewin9999 的回答,他是通过CSS来实现的
<style type="text/css">
body{ background-color:#1f8bbb; }
.float_con{ background:url(../images/footer_bg.gif) repeat-x; height:28px; text-align:center; color:#fff; font:bold 12px/28px Tahoma, Geneva, sans-serif; bottom:0px; position:fixed; width:500px; background-color:#fff;color:#000;}
* html .float_con /* IE6 底部固定 */{position:absolute;bottom:auto;top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}
</style>
<div class="float_con" id="float_con">adddddddddddddddddddddsd</div>
第三:同事帮忙在网上找的代码,也是通过JS来实现的
/*
*target 要固定的元素对象,也可以是元素的id
*pos:object/string 指定固定到的位置,类型为object时,使用json方式如{right:200,bottom:50} ,为string时可选参数如下:
*cc,正中间,lc 左边,rc 右边
*lt 左上角,ct 上边,rt 右上角
*lb 左下角,cb 底部,rb 右下角
*/
var fixPosition = function(target, pos) {
this.target = this.g(target);
this.pos = pos;
this.init(); //
};
fixPosition.prototype = {
isFixed: !window.ActiveXObject || (navigator.userAgent.indexOf("MSIE 6") == -1 && document.compatMode == "CSS1Compat"),
ae: function(e, call) {
if (window.addEventListener) window.addEventListener(e, call, false);
else window.attachEvent("on" + e, call);
},
g: function(id) { return typeof (id) == "string" ? document.getElementById(id) : id; },
setPos: function() {//设置位置
var de = document.compatMode == "CSS1Compat" ? document.documentElement : document.body;
if (typeof (this.pos) == "string") {//
if (this.isFixed) {
switch (this.pos.charAt(0)) {
case "l":
this.target.style.left = "0px";
break;
case "r":
this.target.style.right = "0px";
break;
default:
this.target.style.left = (de.clientWidth - this.target.clientWidth) / 2 + "px";
break;
}
switch (this.pos.charAt(1)) {
case "t":
this.target.style.top = "0px";
break;
case "b":
this.target.style.bottom = "0px";
break;
default:
this.target.style.top = (de.clientHeight - this.target.clientHeight) / 2 + "px";
break;
}
} else {
switch (this.pos.charAt(0)) {
case "l":
this.target.style.left = de.scrollLeft + "px";
break;
case "r":
this.target.style.left = de.scrollLeft + de.clientWidth - this.target.clientWidth + "px";
break;
default:
this.target.style.left = de.scrollLeft + ((de.clientWidth - this.target.clientWidth) / 2) + "px";
break;
}
switch (this.pos.charAt(1)) {
case "t":
this.target.style.top = de.scrollTop + "px";
break;
case "b":
this.target.style.top = de.scrollTop + de.clientHeight - this.target.clientHeight + "px";
break;
default:
this.target.style.top = de.scrollTop + ((de.clientHeight - this.target.clientHeight) / 2) + "px";
break;
}
}
} else {
if (this.isFixed) {
for (var p in this.pos)
this.target.style[p] = this.pos[p] + "px";
} else {
for (var p in this.pos) {
switch (p.toLowerCase()) {
case "left":
this.target.style.left = de.scrollLeft + this.pos[p] + "px";
break;
case "right":
this.target.style.left = de.scrollLeft + de.clientWidth - this.target.clientWidth - this.pos[p] + "px";
break;
case "top":
this.target.style.top = de.scrollTop + this.pos[p] + "px";
break;
case "bottom":
this.target.style.top = de.scrollTop + de.clientHeight - this.target.clientHeight - this.pos[p] + "px";
break;
}
}
}
}
},
init: function() {
if (!this.pos)
throw Error("Invalid arguments [pos].");
this.target.style.position = this.isFixed ? "fixed" : "absolute";
var timer, o = this;
this.ae("resize", function() {//支持fixed的浏览器窗体大小改变时也重置位置,防止中间无法居中
clearTimeout(timer);
timer = setTimeout(function() { o.setPos(); }, 30);
});
if (!this.isFixed) {//滚动
this.ae("scroll", function() {
//clearTimeout(timer);
//timer=setTimeout(function() { o.setPos(); },30);
o.setPos();
});
}
this.setPos();
},
clearPos: function() {
this.target.style.left = "";
this.target.style.top = "";
this.target.style.right = "";
this.target.style.bottom = "";
}
}
PS:三种方法用一个简单的层来测试都OK,但是加入项目中最好用的却是第三种,虽然第三种看起来最麻烦最复杂~~不知道为什么第一种和第二种用层测试OK,但加入项目中在IE6里面还是不支持~~在此总结一下,或许下次能派上用场呢!!
PPS:http://hi.baidu.com/sunnyzhishui/blog/item/e3ce7e63e49ea6790d33faff.html (js 底部固定, 底部固定漂浮导航,无JS纯CSS定义)
http://wenwen.soso.com/z/q244593710.htm(IE6下让DIV固定显示在浏览器底部并水平位置居中如何做?)
注:这是在网上其他地方找的方法,虽然我没实现效果,也收集起来放一起吧~!或许可以给其他人提供借鉴呢!!
相关文章推荐
- 黄聪:DIV+CSS建站经验总结,不同版本IE下CSS布局bug问题(IE5、IE6、IE7、IE8、火狐Firefox兼容)
- 纯CSS下拉式导航菜单,支持IE6、IE7、Firefox,没有用到任何JavaScript,修改方便,而且效果也不错。
- <转>js 底部固定, 底部固定漂浮导航,无JS纯CSS定义
- 【转】 IE6 IE7 IE8 css bug兼容性解决方法总结归纳
- IE6 IE7 IE8 css bug兼容性解决方法总结归纳
- IE6 IE7 IE8 css bug兼容性解决方法总结归纳
- 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
- 发现非常好用的js插件 让IE6支持css3,让 IE7、IE8 都支持CSS3
- CSS对IE6、IE7、IE8支持详细的易用的参考
- IE6 、IE7 、 IE8 的 CSS 、 JS 兼容
- JS实现兼容IE6、IE7、IE8的图片上传前预览效果
- IE6、IE7、IE8的CSS、JS兼容
- IE6、IE7、IE8的CSS、JS兼容
- IE6、IE7、IE8浏览器下的CSS、JS兼容性对比
- [转]CSS中设置div最小高度(支持IE6, IE7, IE8, FF等浏览器)
- DIV+CSS 网页兼容性问题(IE6 IE7 IE8 IE9 火狐 chorm)
- IE6、IE7、IE8的CSS、JS兼容
- CSS兼容IE6 IE7 IE8 IE9 Firefox的总结
- IE6、IE7、IE8的CSS、JS兼容
- 固定层不随滚动条滚动而滚动(纯CSS,兼容IE6,IE7,IE8,Firefox,Safari)