zoukankan      html  css  js  c++  java
  • js中 clientWidth offsetWidth scrollWidth等区别

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            body,html{
                margin:0px;
                padding:0px;
            }
            #div{
                height: 300px;width:200px;background:indianred;
                padding: 20px;
                border:20px solid #777 ;
                margin: 100px;
            }
        </style>
    </head>
    <body style="">
        <div style="height: 40px;background: antiquewhite"></div>
        <div id="div2" onscroll="myFunction()" style=" 300px;overflow: scroll;position: absolute;height: 200px;background: rebeccapurple">
            <div id="div"></div>
        </div>
    
    
    </body>
    </html>
        <script src="./event.js"></script>
    <script>
            var dom = document.getElementById('div');
            var dom2 = document.getElementById('div2');
            console.log(window.getComputedStyle(dom).width) //200px; content
            console.log(dom.offsetWidth); //280 content + padding + bordder
            console.log(dom.clientWidth) ;//240  content + padding
            console.log(dom.scrollWidth) ;//240 content + padding +(溢出尺寸)
            //当前元素 上边框 外边缘 到 最近的已定位父级(offsetParent) 上边框 内边缘的 距离。如果父
            //级都没有定位,则分别是到body 顶部 和左边的距离
            console.log(dom.offsetTop); //100 content + padding + bordder
            console.log(dom.clientTop) ;//20  border-top
            function myFunction(){
                //元素滚动条内的顶部隐藏部分的高度
                console.log(dom2.scrollTop); //0
    
            }
    
    </script>
  • 相关阅读:
    Problem 3
    Problem 2
    Problem 1
    Python基础 装饰器
    算法——狄克斯特拉算法
    A Knight's Journey POJ 2488
    多校10 1007 CRB and Queries
    多校9 1007 Travelling Salesman Problem
    多校8 1008 Clock
    多校7 1005 The shortest problem
  • 原文地址:https://www.cnblogs.com/mengdiezhuangzhou/p/9761140.html
Copyright © 2011-2022 走看看