动态调整div的高度

时间:2011-12-06 17:53:38

标签: javascript css xhtml

嗨我正在寻找这个:Dynamically adjust an iframe's height但是为了把它做到div,我到目前为止有以下内容:

#NewsBlock {
height: 222px;
overflow-x: auto;
padding: 5px;
}



<div class="boxFive">
<h3 class="tab">Latest News</h3>
<script type="text/javascript">
function resizeIframe() {
    var height = document.documentElement.clientHeight;
height -= document.getElementById('NewsBlock').offsetTop;

height -= 20; /* whatever you set your body bottom margin/padding to be */
document.getElementById('NewsBlock').style.height = height +"px";

};
document.getElementById('NewsBlock').onload = resizeIframe;
window.onresize = resizeIframe;
</script>
                    <div id="NewsBlock">
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>

                    </div>
                </div>

但它似乎没有用,我错过了什么?

3 个答案:

答案 0 :(得分:0)

尝试:

function resizeIframe() {
  var height = 0;
  var body = window.document.body;
  if (window.innerHeight) {
      height = window.innerHeight;
  } else if (body.parentElement.clientHeight) {
      height = body.parentElement.clientHeight;
  } else if (body && body.clientHeight) {
      height = body.clientHeight;
  }
  document.getElementById('NewsBlock').style.height = ((height - document.getElementById('NewsBlock').offsetTop) + "px");
}

答案 1 :(得分:0)

将你的脚本放在NewsBlock div之后

答案 2 :(得分:0)

看起来你的Javascript工作得很好。但是,您可能没有看到结果,只是因为包含div #NewsBlock没有剪切其内容。尝试将您的CSS设置为:

#NewsBlock {
   height: 222px;
   overflow-x: auto;
   overflow-y: hidden;
   padding: 5px;
}

这对我有用,请在此处查看:http://jsfiddle.net/zKnnQ/4/

请注意,如果打开webkit控制台,则可以看到容器div的高度发生变化。