[Web] CSS的固定位置

博客首页 » Web CSS的固定位置

发布于 19 Sep 2014 07:26
标签 blog
最近有一个需要在窗口上固定位置的需求,是用CSS position:fixed实现的,虽然JS也可以实现,但不如CSS稳定。

参考的文章在这里
http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html

css固定元素位置(fixed)

我们经常碰到的需求是希望页面某个元素固定在浏览器的一个位置,无论如何滚动滚动条,位置不变,就例如经常看到的弹出广告。方法一般是使用js控制,或者使用css。这里我写的是css的控制方法。

在IE7以上版本及firefox、opera、safari里,都支持css属性"position:fixed",它的作用就是将元素相对于窗口固定位置。代码如下

#ads{
    position:fixed;
    right:0;
    bottom:0;
    border:1px solid red;
    width:300px;
    height:250px;
}

我们定义一个#ads的id样式,并给他设了高度宽度,通过position:fixed以及right、bottom将元素定位在窗口右下角。

但是在IE6下,并不支持position:fixed属性,这个时候我们需要对IE6进行hack处理。解决的方案是使用postion:absolute属性,它的作用大家都很熟悉,相对于父元素进行绝对定位,然后我们可以通过expression来改变#ads的top值。

PS expression的定义:IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javas cript表达式关联起来,这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段Javas cript表达式,CSS属性的值等于Javas cript表达式计算的结果。 在表达式中可以直接引用元素自身的属性和方法,也可以使用其他浏览器对象。这个表达式就好像是在这个元素的一个成员函数中一样。

所以我们可以通过在css里计算javascript值来改变top值,代码如下:

#ads{
    _position:absolute;
    _top:expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight);
}

似乎一切都完美了,但是我们在IE6下运行的时候会发现,随着滚动条的移动,我们的这个#ads朋友他会抖动。解决方法也很简单,只要在body里加一点点的css,如下:

body{
    background-image:url(about:blank); /* for IE6 */ 
    background-attachment:fixed; /*必须*/
}

PS:原本使用的是"url(text.txt)",但是txt这个是不存在的,http请求报404错误,导致影响加载速度,参考了网上的一些写法,使用about:blank可以达到相同目的。

原理据说是ie6不支持fixed 而其样式背景却支持fixed,通过背景来此消彼长消除抖动,望大牛指教。

完整的代码:

body{
    background-image:url(about:blank); /* for IE6 */ 
    background-attachment:fixed; /*必须*/
}
#ads{
    width:300px;
    height:250px;
    position:fixed;
    right:0;
    bottom:0;
    _position:absolute;
    _top:expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight);
    border:1px solid red;
}

不过CSS expression 是一个令人发疯的功能,甚至在鼠标移动的时候都要重新计算,从而轻易超过一万次,所以尽量避免这类功能的使用。

英文参考:
http://www.css-101.org/fixed-positioning/05.php

How to mimic position:fixed in Internet Explorer 6:
Via a CSS expression

/*
* there is no need to use a real image here
* anything will do :)
*/

* html {
background: url(css-101);
}

#footer {
position: fixed;
bottom: 0;
_position: absolute;
_top: expression(eval(document.compatMode && document.compatMode=='CSS1Compat')
? documentElement.scrollTop +(documentElement.clientHeight-this.clientHeight)
: document.body.scrollTop +(document.body.clientHeight-this.clientHeight));
}
Be aware that CSS expressions are evil.

Via "markup"
CSS:

html,
body {
padding:0;
margin:0;
_padding:0 1px;
_height:100%;
_overflow:hidden;
}
#scrollable {
_height:100%;
_width:100%;
_overflow:auto;
}
#fixed {
right:25px;
position:fixed;
_position:absolute;
}
Markup:

<div id="fixed">This box is fixed.</div>
<div id="scrollable">
<div id="doc">This box will scroll.</div>
</div>
Be aware that this approach may lower the semantic of your document.


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Web CSS的固定位置

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License