嗨,我正在找这个: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>
但它似乎没有用,我是不是漏掉了什么?
最佳答案
尝试:
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"); }
关于javascript - 动态调整div的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404430/