`
java-mans
  • 浏览: 11380520 次
文章分类
社区版块
存档分类
最新评论

DIV绝对定位 position、z-index、top、right、bottom和left属性

 
阅读更多

转载自:

http://wujt.iteye.com/blog/1181558

一、Position
1、语法:position:static/ absolute / fixed / relative
2、参数:
(1)static:默认值,无特殊定位,对象遵循HTML定位规则。
(2)absolute:将对象从文档流中拖出,使用left,right,top,bottom等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据body对象。而其层叠通过z-index属性定义。
(3)relative:对象不可层叠,但将依据left,right,top,bottom等属性在正常文档流中偏移位置。
(4)fixed:对象定位遵从绝对(absolute)方式。
3、说明:
对象的定位方式。
设置此属性值为absolute会将对象拖离出正常的文档流绝对定位而不考虑它周围内容的布局。假如其他具有不同z-index属性的对象已经占据了给定的位置,他们之间不会相互影响,而会在同一位置层叠。
此时对象不具有外边距margin,但仍有内边距padding和边框border。
对应的脚本特性为:position。
4、示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function selA(id) {
switch(id) {
case "1":
document.getElementById("subobj").style.position = "static";
break;
case "2":
document.getElementById("subobj").style.position = "absolute";
break;
case "3":
document.getElementById("subobj").style.position = "relative";
break;
case "4":
document.getElementById("subobj").style.position = "fixed";
break;
}
}
</script>
<style type="text/css">
#all {
width:190px;
height:95px;
padding:10px 0 0 10px;
border:1px #000 solid;
position:relative;
}
#subobj {
width:100px;
height:50px;
border:1px #000 solid;
bottom:9px;
position:static;
}
#sel {
margin-top:5px;
}
select {
width:200px;
}
</style>
</head>
<body>
<div id="all">
<div id="subobj">子对象1</div>
</div>
<div id="sel"><select onchange="selA(this.value)"><option value="1">static</option><option value="2">absolute</option><option value="3">relative</option><option value="4">fixed</option></select></div>
</body>
</html>


5、提示:
(1)由上面的例子可以看出,只有position属性值为absolute或relative时top、right、bottom、left才有效。
(2)目前还不支持position:fixed的属性值。
二、z-index
1、语法:z-index:auto/ number 2、参数:
(1)auto:默认值,遵从其父对象的定位。
(2)number:无单位的整数值,可为负数。
3、说明:
设置对象的层叠顺序。
较大number值的对象会覆盖在较小number值的对象之上。如两个绝对定位对象的此属性具有同样的number值,那么将依据它们在HTML文档中声明的顺序层叠对于未指定此属性的绝对定位对象,此属性的number值为正数的对象会在其之上,而number值为负数的对象在其之下。设置参数为null可以移除此属性。 此属性仅仅作用于position属性值为relative或absolute的对象上。
对应的脚本特性为:zIndex。
4、示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function selA(id,v) {
if (v) obj = "subobj"; else obj = "subobj2";
switch(id) {
case "0":
document.getElementById(obj).style.zInex = 0;
break;
case "1":
document.getElementById(obj).style.zIndex = 10;
break;
case "2":
document.getElementById(obj).style.zIndex = -10;
break;
}
}
</script>
<style type="text/css">
#all {
width:190px;
height:95px;
padding:10px 0 0 10px;
border:1px #000 solid;
position:relative;
}
#subobj {
width:100px;
height:50px;
border:1px #000 solid;
top:10px;
position:absolute;
background-color:#09C;
}
#subobj2 {
width:100px;
height:50px;
border:1px #000 solid;
top:10px;
position:absolute;
background-color:#CCC;
}
#sel {
margin-top:5px;
}
select {
width:95px;
}
</style>
</head>
<body>
<div id="all">
<div id="subobj">子对象1</div>
<div id="subobj2">子对象2</div>
</div>
<div id="sel">
<select onchange="selA(this.value,1)"><option value="0">对象1</option><option value="1">10</option><option value="2">-10</option></select>
<select onchange="selA(this.value,0)"><option value="0">对象2</option><option value="1">10</option><option value="2">-10</option></select>
</div>
</body>
</html>

5、提示:
(1)z-index只有position属性的值为relative或absolute时才有效。
三、Top、Right、Bottom、Left 四个属性的设置都是相同的,下面以Top为例。
1、语法:top:auto/ length
2、参数:
(1)auto:默认值,无特殊定位,根据HTML定位规则在文档流中分配。
(2)length:由浮点数字和单位标识符组成的长度值或者百分数。必须定义position属性值为absolute或者relative此取值方可生效。请参阅长度单位
3、说明:
设置对象与其最近一个定位的父对象顶部相关的位置。
对应的脚本特性为:top。其值为一字符串,所以不可用于脚本中的计算。请使用style对象的posTop,pixelTop,以及对象的offsetTop等特性。


///////////////////////
div总是被select遮挡的解决方法
//////////////////////////
html>
<head>

<meta http-equiv="Content-Type" c>

<title>div被select遮挡的解决方法——脚本之家</title>
</head>
<body>

<iframe style="position:absolute;z-index:9;width:expression
(this.nextSibling.offsetWidth);height:expression
(this.nextSibling.offsetHeight);top:expression(this.nextSibling.offsetTop);left:expression
(this.nextSibling.offsetLeft);" frameborder="0" ></iframe>

<form id="Form1" method="post">

<div style="z-index:10;position:absolute;background-
color:blue;width:100;height:18;overflow:hidden;" >aaaaaaa<br/>bbbbbbb<br/>ccccccc</div>

<select style="width:200" ><option>test0<option>test1<option>test2<option>test3</select>

<br/>

<pre>

Div被Select挡住,是一个比较常见的问题。

有的朋友通过把div的内容放入iframe或object里来解决。

可惜这样会破坏页面的结构,互动性不大好。
这里采用的方法是:
虽说div直接盖不住select

但是div可以盖iframe,而iframe可以盖select,

所以,把一个iframe来当作div的底,

这个div就可以盖住select了.
</pre>

</form>

</body>
</html>

function install(){
var propertiesCount = chenhao.count();

//var object = document.getElementById('test');
var object = document.body;


createElement(object,chenhao.name,1);
createElement(object,chenhao.email,2);
createElement(object,chenhao.website,3);

createElement(object,chenhao['name'],4);
createElement(object,chenhao['email'],5);
createElement(object,chenhao['website'],6);


//document.("test").innerHTML = chenhao.name;
//document.getElementById("test").innerHTML = chenhao.email;
//document.getElementById("test").innerHTML = chenhao.website;
}

function createElement(object,content,index){
var div = document.createElement("div");
div.setAttribute('style','position:absolute;top:'+parseInt(index*20)+'px;left:100px');
div.innerHTML=content+'';
object.appendChild(div);

}

分享到:
评论

相关推荐

    div的position属性

    如果想为元素设置层模型中的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流中拖出来,然后使用left、right、top、bottom属性相对于其最接近的一个具有定位属性的父包含块进行...

    深入理解css中position属性及z-index属性(推荐)

    最后将会介绍和position属性密切相关的z-index属性。 第一部分:position: static static定位是HTML元素的默认值,即没有定位,元素出现在正常的流中,因此,这种定位就不会收到top,bottom,left,right的影响。 如...

    div-css-漂亮的导航条

    background:url("images/tableft1.gif") no-repeat left top; margin:0; padding:0 0 0 4px; text-decoration:none; } #tabs1 a span { float:left; display:block; background:url("images/tabright1.gif...

    三星9305收索

    z-index:100}#form .bdsug{top:35px}#head .c-icon-bear-round{display:none}.bdsug .bdsug-direct{width:auto;padding:0;border-bottom:1px solid #f1f1f1}.bdsug .bdsug-direct p{color:#00c;font-weight:bold;...

    DIV重叠 CSS让DIV层叠 两个DIV或多个DIV顺序重叠加

    1、z-index 重叠顺序属性 2、position:relative和position:absolute设置对象属性为可定位(可重叠) 3、left right top bottom绝对定位具体位置设置 配合的样式 1、css width 2、css height 3、background 为了...

    CSS 嵌套DIV布局(position属性)

    absolute:absolute绝对定位,直接指定top、left、right、bottom。有意思的是绝对定位也是“相对”的。它的坐标是相对其容器来说的。容器又是什么呢,容器就是离元素最近的一个定位好的“祖先”,定位好的意思就是其...

    CSS教程:position 绝对定位的问题

    到网上查了下,发现了这么一句话:absolute 脱离文档流,通过 top,bottom,left,right 定位,选取其最近的父级定位元素,当父级 position 为 static 时,absolute元素将以body坐标原点进行定位,可以通过z-index进行...

    css中定位中的absolute和relative是什么意思

    absolute是指绝对定位,即将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位,而其层叠通过z-index属性定义。此时对象不具有边距,但仍有补白和边框。 ralative是指相对定位,就是依据left,...

    老生常谈position定位——让人又爱又恨的属性

    让人无奈的时候,就是我们一旦滥用了position这个定位属性,就会让自己的布局飞的满天是,又因为z-index没有设定好,基本上,整体的布局就会让你手足无措,找原因的话,又非常麻烦,最后,恐怕只能推倒重做了。...

    CSS中的position:relative;的作用示例介绍

    static : 无特殊定位,对象遵循HTML定位规则 absolute : 将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位。而其层叠通过z-index属性定义。此时对象不具有边距,但仍有补白和边框 relative : ...

    DIV滑动门代码

    background-position:left; background-repeat:repeat-y; margin-bottom:2px; } .nTab .TabTitle{ clear: both; height: 26px; overflow: hidden; } .nTab .TabTitle ul{ margin:0; padding:0; } .nTab .TabTitle li...

    零基础学HTML CSS源代码

    div.css 名为div的样式文件。 style.css 名为style的样式文件。 第15章(源代码\第15章) 示例描述:本章演示CSS基础部分知识。 CSS基础.html 演示CSS基础链接知识用法。 style1.css ...

    通过绝对定位实现div重叠实例代码

    复制代码代码如下: &lt... .div-relative {position: absolute;color: #000;border: 1px solid #000;width: 500px;height: 400px;right: 0;bottom: 0;} .div-a {position: absolute;left: 30px;top: 30px;background: #

    HTML5&CSS3网页制作:设置多重背景图像.pptx

    在CSS3中,通过background-im、age等属性提供多个属性值可以实现多重背景图像效果,各属性值之间用逗号隔开。 background: background-color background-image background-position background-repeat background-...

    静态网业模板1

    z-index:200; } .special_icon{ position:absolute; top:0px; _top:6px; right:2px; z-index:250; } img.thumb{ padding:10px 0 0 0; } .new_products{ clear:both; padding:0px; } ul.list{ clear:both; padding:...

    移动div层.txt

    z-index:1; OVERFLOW: none; HEIGHT: 119px;width:228px; left:284px; top:200px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px" &gt;&lt;/div&gt; &lt;br /&gt; &lt;b&gt;Drag and drop the image...

    HTML 网页设计

    z-index: 9999; } div.bl-panel-items &gt; div { background:rgba(252, 252, 252, 0.760784); } /**** About ***/ .bl-content h2 { font-size:1.2em; color:#FFF; padding: 5px 0; margin-top:20px; text-...

    media-queries

    z-index:inherit;} .h-aslsc .inner-box{ width:96%;} .h-aslsc-l{ width:100%; padding:0;} .h-aboutus{ float:left; width:48%; margin-top:10px;} .h-news{ float:right; width:48%;} .h-news-b li{ width:auto;...

    雅虎TAB效果代码 Javascript实现

    z-index:500;} #tabs ul {padding:0; margin:0; width:400px; list-style:none; position:relative;} #tabs ul li {float:left; display:inline; width:125px; height:53px; margin:0 4px;} #tabs ul li a.outer {...

Global site tag (gtag.js) - Google Analytics