美文网首页
JS第七天-01

JS第七天-01

作者: knot98 | 来源:发表于2018-10-19 21:43 被阅读0次

JS盒模型

1、width | height

  • parseInt(getComputedStyle(ele, null).getPropertyValue('width'))
  • parseInt(getComputedStyle(ele, null).getPropertyValue('height'))

2、padding + width | padding + height

  • clientWidth
  • clientHeight

3、border + padding + width | border + padding + height

  • offsetWidth
  • offsetHeight

4、结合绝对定位,距离最近定位父级的Top | Left

  • offsetTop
  • offsetLeft

代码示例:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>JS盒模型</title>

    <style type="text/css">
        .sup {
            width: 200px;
            height: 200px;
            padding: 30px;
            border: 5px solid black;
            background-color: orange;
            margin: 20px;
            position: relative;
        }

        .sub {
            width: 100px;
            height: 100px;
            padding: 20px;
            border: 5px solid black;
            background-color: red;
            position: absolute;
            top: 0;
            left: 20px;
        }
    </style>
</head>
<body>
    <div class="sup">
        <div class="sub"></div>
    </div>
</body>
<script type="text/javascript">
    var sup = document.querySelector('.sup');
    // 盒子content的width
    var width = parseInt(getComputedStyle(sup, null).width);
    console.log(width);

    // 盒子padding+width => 子级的可活动范围
    var p_width = sup.clientWidth;
    console.log(p_width);

    // 盒子border+padding+width => 可视区域
    var b_p_width = sup.offsetWidth;
    console.log(b_p_width);

    // 盒子距离定位父级的top,left
    var sup_top = sup.offsetTop;
    var sup_left = sup.offsetLeft;
    console.log(sup_top);
    console.log(sup_left);


    var sub = document.querySelector('.sub');
    // 父级定位(relative)后,子级活动区域为父级的client(padding+width)区域
    var sub_top = sub.offsetTop;
    var sub_left = sub.offsetLeft;
    console.log(sub_top);
    console.log(sub_left);

</script>
</html>

相关文章

  • JS第七天-01

    JS盒模型 1、width | height parseInt(getComputedStyle(ele, nul...

  • JavaScript30 Day 7

    这是我在github 上发现的一个原生js挑战项目,由于是js小白,希望通过这次的项目加深对js的理解 第七天跟第...

  • DOM(一)

    第七天 03-对象模型-第01天{DOM、常用属性} 第七天DOM相关概念什么是DOMDOM内容概念DOM操作获取...

  • JS学习——第七天

    2018年2月28日,第七天课程。 课程主题:bitwise operator,JS dates。 今天早上一直在...

  • 轮播图

    轮播图01 html css js

  • 2020-03-16

    JavaScript 初识 《① JS 速览——进入 JS 的世界》[编号:js_01] 《② 运算符、运算符优先...

  • Scp-JS-01-J  网抑云患者

    项目编号:Scp-JS-01-J项目等级:Euclid 特殊收容措施:Scp-JS-01-J常以虚拟化存在于「」「...

  • promise使用

    title: promisedate: 2019-01-30 17:49:36tags: js js 实现异步操作...

  • 07.创建自定义模块并调用

    一般一个js文件就是一个模块 创建模块并导出并将文件命名为module01.js 在其他程序node01.js中调...

  • JavaScript作用域闭包分析总结

    原文链接 http://blog.poetries.top/2017/01/10/js-scope 一、JS解析顺...

网友评论

      本文标题:JS第七天-01

      本文链接:https://www.haomeiwen.com/subject/qinuzftx.html