文章标签 ‘DIV’
20108 月3

DIV+CSS常用属性定义说明

.toptab {
    min-width:60px;/*最小宽度*/
    _width:60px; /*实现min-width兼容 IE6*/
    overflow: hidden;/*不出现滚动条*/
    float:left;/*左浮动*/
    padding:2px 6px 0 6px;/*定义元素内边框,上右下左*/
    margin:0 10px 0 0;/*定义元素外边框,上右下左*/
    text-align:center;/*文本居中对齐*/
    height:22px;
    line-height:22px; /*两个高度一致,使文本上下垂直居中 */
    font-size: 12px;/*文字大小*/
    background-color:#ffffff;/*元素背景颜色*/
    color: #333333;/*文字颜色*/
    font-weight: normal;
    text-decoration: none;/*不出现下划线*/
}

ps:内联(display:inline;)属于行布局,其特性是在一行里进行布局,不能设置宽高

20108 月1

JS 实现DIV随浏览器窗口大小变化

以下是宽度变化的示例,高度也可参考:

<script type="text/javascript">
    function initWidth() {
        var bodyWidth = document.compatMode == "BackCompat"?document.body.clientWidth:document.documentElement.clientWidth;
        document.getElementById("Content").style.width = bodyWidth - 180;
    }
    initWidth();
    if (document.attachEvent) {
        window.attachEvent('onresize', initWidth);
    } else {
        window.addEventListener('resize', initWidth, false);
    }
</script>
20107 月9

CSS+DIV+UL+LI 实现简单下拉菜单

<!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>wizzer.cn</title>
<style type="text/css">
<!--
* {
margin:0px;
padding:0px;
font-family:"宋体";
font-size:12px;
text-align:center;
}
body ul{
margin:0px auto;
}
#mean{
width:500px;
margin:0px auto;
padding:0px;
}
#mean li{
float:left;
list-style-type:none;
width:80px;
padding:1px;
line-height:25px;
}
.a{
    background-color: #9DDFAA;
    color: #060;
    text-decoration: none;
    border: 1px solid #0C6;
    display: block;

}
.a:hover{
    color: #FFFFFF;
    background-color: #12581A;
    border: 1px solid #6AFF6A;
}
.list{
    width: 80px;

}
.list ul{
margin:0px auto;
padding:0px;
}
.list li{
    float: left;
    height: 18px;
    width: 80px;
}
.b{
    width: 80px;
    background-color: #55BF60;
    line-height:18px;
    display: block;
    text-decoration: none;
    color: #000;
    filter:Alpha(opacity=70);
}
.b:hover{
    background-color: #FFFFFF;
    color: #060;
    text-decoration: none;
    filter:Alpha(opacity=70);
}
.menu2{
    height: 26px;
    width: 80px;
    cursor:hand;
    overflow-y:hidden;
}
#keai{
    width:800px;
    height:50px;
    background-color:#4033D7;
    z-index:-1;
    position: absolute;
    left: 0px;
    top: 35px;
}
-->
</style>
</head>
<body>
<div id="mean">
<ul>
<li onMouseOver="this.className='menu1'"
onMouseOut="this.className='menu2'">
<a href="#">网站首页</a>
<div>
<ul>
<li><a href="#">我的主页</a></li>
<li><a href="#">我的空间</a></li>
<li><a href="#">我的相册</a></li>
<li><a href="#">我的日志</a></li>
</ul>
</div>
</li >
<li onMouseOver="this.className='menu1'"
onMouseOut="this.className='menu2'"><a href="#">新闻公告</a>
<div>
<ul>
<li><a href="#">我的主页</a></li>
<li><a href="#">我的空间</a></li>
<li><a href="#">我的相册</a></li>
<li><a href="#">我的日志</a></li>
</ul>
</div>
</li>
<li onMouseOver="this.className='menu1'"
onMouseOut="this.className='menu2'"><a href="#">产品信息</a>
<div>
<ul>
<li><a href="#">我的主页</a></li>
<li><a href="#">我的空间</a></li>
<li><a href="#">我的相册</a></li>
<li><a href="#">我的日志</a></li>
</ul>
</div>
</li>
<li onMouseOver="this.className='menu1'"
onMouseOut="this.className='menu2'"><a href="#">最新推荐</a>
<div>
<ul>
<li><a href="#">我的主页</a></li>
<li><a href="#">我的空间</a></li>
<li><a href="#">我的相册</a></li>
<li><a href="#">我的日志</a></li>
</ul>
</div>
</li>
<li onMouseOver="this.className='menu1'"
onMouseOut="this.className='menu2'"><a href="#">联系我们</a>
<div>
<ul>
<li><a href="#">我的主页</a></li>
<li><a href="#">我的空间</a></li>
<li><a href="#">我的相册</a></li>
<li><a href="#">我的日志</a></li>
</ul>
</div>
</li>
</ul>
</div>
</body>
</html>
20107 月7

实现鼠标拖动div交换位置

<html>
<head>
<title>月影</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
        float:left;
        width:20%;
        border:1px solid #FF0000;
        margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
</style>
<script>
var dragobj={}
window.onerror=function(){return false}
function on_ini(){
        String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
        var agent=navigator.userAgent
        window.isOpr=agent.inc("Opera")
        window.isIE=agent.inc("IE")&&!isOpr
        window.isMoz=agent.inc("Mozilla")&&!isOpr&&!isIE
        if(isMoz){
                Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
                Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
        }
        basic_ini()
}
function basic_ini(){
        window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
        window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.onload=function(){
        on_ini()
        var o=document.getElementsByTagName("h1")
        for(var i=0;i<o.length;i++){
                o[i].onmousedown=function(e){
                        if(dragobj.o!=null)
                                return false
                        e=e||event
                        dragobj.o=this.parentNode
                        dragobj.xy=getxy(dragobj.o)
                        dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
                        dragobj.o.style.width=dragobj.xy[2]+"px"
                        dragobj.o.style.height=dragobj.xy[3]+"px"
                        dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
                        dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
                        dragobj.o.style.position="absolute"
                        var om=document.createElement("div")
                        dragobj.otemp=om
                        om.style.width=dragobj.xy[2]+"px"
                        om.style.height=dragobj.xy[3]+"px"
                        dragobj.o.parentNode.insertBefore(om,dragobj.o)
                        return false
                }
        }
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
        if(dragobj.o!=null){
                dragobj.o.style.width="auto"
                dragobj.o.style.height="auto"
                dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
                dragobj.o.style.position=""
                oDel(dragobj.otemp)
                dragobj={}
        }
}
document.onmousemove=function(e){
        e=e||event
        if(dragobj.o!=null){
                dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
                dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
                createtmpl(e)
        }
}
function getxy(e){
        var a=new Array()
        var t=e.offsetTop;
        var l=e.offsetLeft;
        var w=e.offsetWidth;
        var h=e.offsetHeight;
        while(e=e.offsetParent){
                t+=e.offsetTop;
                l+=e.offsetLeft;
        }
        a[0]=t;a[1]=l;a[2]=w;a[3]=h
  return a;
}
function inner(o,e){
        var a=getxy(o)
        if(e.x>a[1]&&e.x<(a[1]+a[2])&&e.y>a[0]&&e.y<(a[0]+a[3])){
                if(e.y<(a[0]+a[3]/2))
                        return 1;
                else
                        return 2;
        }else
                return 0;
}
function createtmpl(e){
        for(var i=0;i<12;i++){
                if($("m"+i)==dragobj.o)
                        continue
                var b=inner($("m"+i),e)
                if(b==0)
                        continue
                dragobj.otemp.style.width=$("m"+i).offsetWidth
                if(b==1){
                        $("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
                }else{
                        if($("m"+i).nextSibling==null){
                                $("m"+i).parentNode.appendChild(dragobj.otemp)
                        }else{
                                $("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
                        }
                }
                return
        }
        for(var j=0;j<3;j++){
                if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
                        continue
                var op=getxy($("dom"+j))
                if(e.x>(op[1]+10)&&e.x<(op[1]+op[2]-10)){
                        $("dom"+j).appendChild(dragobj.otemp)
                        dragobj.otemp.style.width=(op[2]-10)+"px"
                }
        }
}
</script>
</head>
<body>
<div>
        <div id=dom0>
                <div id=m0>
                        <h1>dom0</h1>
                        <div></div>
                </div>
                <div id=m1>
                        <h1>dom1</h1><div></div>
                </div>
                <div id=m2><h1>dom2</h1><div></div></div>
                <div id=m3><h1>dom3</h1><div></div></div>
        </div>
        <div id=dom1>
                <div id=m4><h1>dom4</h1><div></div></div>
                <div id=m5><h1>dom5</h1><div></div></div>
                <div id=m6><h1>dom6</h1><div></div></div>
                <div id=m7><h1>dom7</h1><div></div></div>
        </div>
        <div id=dom2>
                <div id=m8><h1>dom8</h1><div></div></div>
                <div id=m9><h1>dom9</h1><div></div></div>
                <div id=m10><h1>dom10</h1><div></div></div>
                <div id=m11><h1>dom11</h1><div></div></div>
        </div>
</div>
</body>
</html>