查看: 6641|回复: 0
打印 上一主题 下一主题

用JAVASCRIPT实现DIV自动栏高

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-18 16:24:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
在最近的一个项目中遇到一个问题,需要在如下图中的B栏中使用背景,但A和C的高度都是不固定的。标准的CSS+DIV是不支持这种自动高度,想了很多办法,最后找出一个方案,借用JAVASCRIPT来实现!

JAVASCRIPT代码如下:

function init() {
if (document.getElementById) {
var h = document.getElementById("right").scrollHeight-document.getElementById("left_top").scrollHeight;
document.getElementById("left_bottom").style.height = h+"px";
}
}

其中right就是C栏,left_top就是A栏,left_bottom是B栏。基本的原理就是用C栏的scrollHeight减去A栏的scrollHeight,从而得到C栏的高度
将init加到body的onload中就可以了。

演示代码:
<!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=utf-8" />
<title>tzlink.com</title>
<style type="text/css">
<!--
body{
        margin:0px;
}
#container {
        width:700px;
}
#left{
        float:left;
        width:150px;
}
#left_top{
        clear:both;
        width:150px;
        background-color:#FF0000;
        overflow:hidden;
}
#left_bottom{
        width:150px;
        clear:both;
        background-color:#00ff00;
        overflow:hidden;
}
#right{
        width:550px;
        background-color:#0000FF;
        float:left;
}
.clear{
        width:1px;
        height:1px;
        font-size:1px;
        clear:both;
}
p{
        margin:0px;
        padding:5px;
}
-->
</style>
<script type="text/javascript">
<!--
function init() {
        if (document.getElementById) {
                var h = document.getElementById("right").scrollHeight-document.getElementById("left_top").scrollHeight;
                document.getElementById("left_bottom").style.height = h+"px";
        }
}
//-->
</script>
</head>
<body onload="init()">
<div id="container">
        <div id="left">
                <div id="left_top">
                        <p style="font-size:120px">A</p>
                </div>
                <div id="left_bottom">
                        <p style="font-size:120px">B</p>
                </div>
                <div class="clear"></div>
        </div>
        <div id="right">
                        <p style="font-size:500px">C</p>
        </div>
</div>
</body>
</html>

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表