浮动层固定兼容IE6 position:fixed的最佳解决方案

第一种:css方法

有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固定层CSS代码。如下:

.bottom{bottom:0;position:fixed;height:23px;line-height:23px;width:100%;z-index:999;
_bottom:auto;_width:100%;_position:absolute;
//_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}
_top:expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight);
/* for IE6 ,执行JS语句,documentElement即html对象,clientHeight即可视窗口高度,
offsetHeight即浮动层高度(包括border边框厚度),scrollTop即滚动条滚动过的页面高度 */

需要注意的是:使用以上代码后在ie中你会发现被固定定位的元素在滚动滚动条的时候会闪动,下面给出解决方法:

//body{background:url(notfound)fixed;}
body{
background-image:url(text.txt); /*for IE6,也可以用一张图片URL,是否存在这文件无所谓*/
background-attachment:fixed;   /* 这句是关键之一:一定要fixed,不能用scroll */

第二种:css方法

body{margin:0;}
#test{display:block; bottom:3px; right:3px; width:130px; position:fixed;}
/* 以下是写给IE6的 */
* html body{height:100%; overflow-y:auto;}
* html #test{position:absolute; right:18px;}
* html{overflow-x:auto; overflow-y:hidden;}

原理:
1、position定位,如果前面的父元素都没有position的话,那么该position定位元素是相对于html的,

注意!是相对html,而不是body

2、html滚动条隐藏,使用body滚动条overflow:auto模拟html滚动条。

3、body层高度100%,当html不设置height高度的时候,除了IE6外,body处的百分比高度是无效的(这算是利用了IE6的bug吧!)

第三种:jQuery方法

var otop = $("#sub_nav").offset().top;
	 $(window).scroll(function(){
		var scroll_top = parseInt($(window).scrollTop());
		if( scroll_top > otop ){
			if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style) {
				$("#sub_nav").css({position:"absolute", top:$(window).scrollTop()+"px"});
			}else{
				$("#sub_nav").css({position:"fixed", top:"0px"});
			}
		}else{
			$("#sub_nav").css({position:"static", top:""});
		}
	 });

上面的sub_nav是一个层,随着页面向下滚动,当此水平条接触浏览器的上边缘时,水平条独立出来,不跟随滚动条滚动了

第四种:插件方法

jquery插件:任意位置浮动固定层(09-11-05更新插件)

演示地址

第五种:js函数

<div class="float" id="float"> 我是个腼腆羞涩的浮动层… </div>

var $smartFloat = function(elements) {
var position = function(element) {
var top = element.getPosition().y, pos = element.getStyle("position");
window.addEvent("scroll", function() {
var scrolls = this.getScroll().y;
if (scrolls > top) {
if (window.XMLHttpRequest) {
element.setStyles({
position: "fixed",
top: 0
});
} else {
element.setStyles({
top: scrolls
});
}
}else {
element.setStyles({
position: "absolute",
top: top
});
}
});
};
if ($type(elements) === "array") {
return elements.each(function(items) {
position(items);
});
} else if ($type(elements) === "element") {
position(elements);
}
};
//绑定
$smartFloat($("float"));
时间: 2024-08-02 05:49:35

浮动层固定兼容IE6 position:fixed的最佳解决方案的相关文章

解决 IE6 position:fixed 固定定位问题

实现<div id="ad"></div>固定在窗口左下角 一般的 position:fixed; 实现方法#ad{ position:fixed; bottom:0; left:0px;} 在 IE6 中实现 position:fixed; 的办法#ad{ position:fixed; bottom:0; left:0px; _position:absolute; _bottom:auto; _top:expression(eval(document.doc

IE6 position:fixed 固定定位问题

IE6下 position:fixed失效,解决方法如下: 除了IE6的浏览器 .top { width: 97px; height: 278px; background: #e0c157; position: fixed; z-index: 30000; /*    控制左右位置*/ right: 50px; margin-bottom: 10px; } 针对IE6使元素固定在浏览器的顶部 .top{  _position:absolute; _bottom:auto; _top:expres

完美解决 IE6 position:fixed 固定定位问题

关于 position:fixed; 属性 生成绝对定位的元素,相对于浏览器窗口进行定位. 元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定. position:fixed; 可以让网页上的某个元素固定在一个绝对的位置,即使拉动滚动条位置也不发生变化.(在 LOO2K 博客右下角的那个置顶的小按钮就是用了这个 CSS 属性实现的) 一般的 position:fixed; 实现方法 以我的博客为例,在右下角<div id="top"

IE6下position:fixed不支持问题及其解决方案

IE6有诸多奇葩,不支持position:fixed就是其中之一.所以在做一些比如固定在顶部或者底部或者固定元素的效果时需要考虑兼容IE6的这个问题.解决方案是用Ie6的hack. *html {/* 只有IE6支持 */ background-image: url(about:blank); background-attachment: fixed; /* 固定背景 */ } #box { /* 非IE6浏览器使用固定元素 */ position: fixed; top: 0; left: 0

完美解决IE6不支持position:fixed的bug【转】

废话不多说,先看一下下面这段代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>IE6 position:fixed bug</title> <style> *{padding:0;margin:0} p{heigh

让页面底部浮动起来固定起来

 1.获取屏幕高度, 2.获取页面高度. 3.判断两者之间的的大小.当页面高度大于屏幕高度时就不添加浮动到底布的样式.否则就添加.不去判断是否滚动屏幕. 获取屏幕高度$(window).height();$(document).height(); 浮动到底布: .footer{ position:fixed; width:100%; display:block; bottom:0; }

jQuery&amp;CSS 顶部和底部固定浮动工具栏 兼容IE6

现在常常能看到一些网站(如:新浪微博和花瓣)导航条或工具栏固定在网页的顶部或其他地方.这样的布局方式,能便于用户点击和“曝光率”,不用每次都要把网页拖动到某个特定位置才能点击或看到. 其实这样的布局方式很早就有,只是没有那么个契机推广开吧.做起来也不复杂,只要设置一个小小的属性“position:fixed”便能完成,最关键的无不呼在于要兼容IE6而已. 首先我们来看HTML代码,是不是超简单?这里提供的只是一个简单的框架,实际应用的时候,只要把想要的元素添加东西就在这区域内加就行. HTML

解决IE6不支持position:fixed固定定位的bug(转载范鸭)

http://blog.funya.in/csscss3/ie6-fixed-bug/ 在IE6中实现 position:fixed; 的办法: 浏览器头部固定 .fixed_top { position:fixed; top:0px; } * html .fixed_top /* IE6 头部固定 */{ position:absolute; bottom:auto; top:expression(eval(document.documentElement.scrollTop)); } 浏览器

IE6浏览器不支持固定定位(position:fixed)解决方案

代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> </head> <body> <div style="width