600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > js 设置html高度自适应 js实现的高度自适应

js 设置html高度自适应 js实现的高度自适应

时间:2022-02-26 01:33:47

相关推荐

js 设置html高度自适应 js实现的高度自适应

自适应高度布局比较常用,大家知道自适应宽度可以很方便的用css布局,但是自适应高度用css虽然能解决,但是比较麻烦,还不如写一段脚本来的的轻松多了。

原理:计算屏幕的高度,减去头部和底部的高度就OK了,

[code=”javascript”]

//计算文档的可见高度

function windowHeight() {

??? var de = document.documentElement;

??? return self.innerHeight||(de && de.clientHeight)||document.body.clientHeight;

}

//window.onresize是窗口改变大小的时候,因为窗口改变大小,文档的可见高度或宽度会变化。

window.οnlοad=window.οnresize=function(){

?var wh=windowHeight();

?document.getElementById(“contentWrap”).style.height = document.getElementById(“sidebar”).style.height = (wh-document.getElementById(“header”).offsetHeight-document.getElementById(“footer”).offsetHeight)+”px”;

}

[/code]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。