首页 > 其他 > 详细

设置div 居中 和得到dom元素高度

时间:2019-07-14 19:43:36      阅读:191      评论:0      收藏:0      [点我收藏+]

javascript中获取dom元素高度和宽度的方法如下:

网页可见区域宽: document.body.clientWidth
网页可见区域高: document.body.clientHeight
网页可见区域宽: document.body.offsetWidth (包括边线的宽)
网页可见区域高: document.body.offsetHeight (包括边线的高)
网页正文全文宽: document.body.scrollWidth
网页正文全文高: document.body.scrollHeight
网页被卷去的高: document.body.scrollTop
网页被卷去的左: document.body.scrollLeft

对应的dom元素的宽高有以下几个常用的:

元素的实际高度:document.getElementById("div").offsetHeight
元素的实际宽度:document.getElementById("div").offsetWidth
元素的实际距离左边界的距离:document.getElementById("div").offsetLeft
元素的实际距离上边界的距离:document.getElementById("div").offsetTop

二.实例

</head>
<body>
<div id="main">
    <div id="error">
        <div>网络连接错误</div>
    </div>
</div>
</body>
<script>
    total = document.documentElement.clientHeight;//得到窗口高度
    document.getElementById("main").style.height=total+"px";//设置main高度
    errorHeight = document.getElementById("error").offsetHeight;/*得到error高度*/
    document.getElementById("error").style.paddingTop=(total-errorHeight)/2+"px"; //设置error 居中
</script>
</html>

技术分享图片

 

设置div 居中 和得到dom元素高度

原文:https://www.cnblogs.com/jiangfeilong/p/11185104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!