详细了解 clientWidth、clientHeight、clientLeft、clientTop 以及几个常用的场景

详细了解 clientWidth、clientHeight、clientLeft、clientTop 以及几个常用的场景clientLeft、clientHeight、clientWidth、clientHeightclientWidth、clientHeight元素内部宽度和高度,clientLeft、clientTop获取元素内边距边框到边框的距离.大概如下图所示:clientWidth属性表示元素的内部宽度,以像素计。该属性包括内边距,但不包括垂直滚动条(如果有)、边框和外边距。如上图所示,计算方式为,分为如下两种:存在垂直滚动条 contentwidth+paddin…

大家好,又见面了,我是你们的朋友全栈君。

clientLeft、clientHeight、clientWidth、clientHeight

clientWidthclientHeight 元素内部宽度和高度, clientLeftclientTop 获取元素内边距边框到边框的距离.

大概如下图所示:

图片描述

clientWidth

属性表示元素的内部宽度,以像素计。该属性包括内边距,但不包括垂直滚动条(如果有)、边框和外边距。

如上图所示, 计算方式为, 分为如下两种:

  • 存在垂直滚动条

    content width + padding – scollbarWidth

  • 不存在滚动条

    content width + padding

clientHeight

属性表示元素内部的高度(单位像素),包含内边距,但不包括水平滚动条、边框和外边距。

如上图所示, 计算方式为如下两种:

  • 存在水平滚动条

    content height + padding – scollbarWidth

  • 不存在滚动条

    content height + padding

clientLeft

表示一个元素的左边框的宽度.

计算方式为如下两种情况:

  • 如果文字方向从右往左(默认从左往右,通过设置 direction: rtl;)进行排列,且存在垂直滚动条的情况下

    border width + scollbar width

  • 默认情况下

    border width

注意:

如果当前元素是行内元素(inline)时, clientLeft将返回 0;

clientTop

表示一个元素的上边框的宽度.

把基本的情况介绍完了看看具体实例,代码如下:


<style>
    .box {
        width: 200px;
        height: 200px;
        overflow: hiddle;
        margin: 10px;
        padding: 10px;
        border: 5px solid black;
        background-color: #ccc;
        direction: rtl;
    }
</style>
<div class="box" id="box">
</div>

<script>
    var ele = document.querySelector("#box");
    var clientWidth = ele.clientWidth;
    var clientHeight = ele.clientHeight;
    var clientLeft = ele.clientLeft;
    var clientTop = ele.clientTop;

    console.log(clientWidth, clientHeight, clientLeft, clientTop);
    // 220 , 220, 5, 5
</script>

这是不存在水平和垂直,以及文字按照默认情况下排列.

下面我将对上面实例进行如下修改:

  • 给元素 .box 添加 overflow:scroll 让它显示滚动条, 再来看看每个值:

    <style>
        .box {
            ...
            overflow: scroll;
        }
    </style>

   <script>
        console.log(clientWidth, clientHeight, clientLeft, clientTop);
        // 203, 203, 5, 5
   </script>

如下图所示:

图片描述

从输出答应结果来看, clientWidth 、clientHeight 变小了,也就是说不包含其滚动条.

  • 改变容器文字输出方向,看看 clientLeft 值会不会像之前说的会加上滚动条的width ?

    <style>
        .box {
            ...
            overflow: scroll;
            direction: rtl;
        }
    </style>

   <script>
        console.log(clientWidth, clientHeight, clientLeft, clientTop);
        // 203, 203, 22, 5
   </script>

如下图所示:

图片描述

从打印结果来看, 改变文字方向(rtl)并且存在处置滚动条情况下:clientLeft = scrollbarWidth + borderLeftWidth

使用场景

计算滚动条宽度

默认情况下(没有滚动条情况下) 
clientWidth = content width + paddingLeftWidth + paddingRightWidth;
对上面示例来说 clientWidth = 200 + 10 + 10;

有滚动条情况下:
clientWidth = (content width + paddingLeftWidth + paddingRightWidth) - scrollbarWidth

可以推断出滚动条计算方式:
scrollbarWidth = (content width + paddingLeftWidth + paddingRightWidth) - clientWidth;

clientWidth 已知, 从上面公式来看只要知道内容区域大小和左右padding值即可


    var ele = document.querySelector("#box");
    var computedStyle = window.getComputedStyle(ele);
    var offsetWidth = ele.offsetWidth; // content widht + padding width + border width (包含滚动条)
    var ceil = function(v){
        return Math.ceil(parseFloat(v))
    }
    var maxClientWidth = offsetWidth
        - ceil(computedStyle.borderLeftWidth)
        - ceil(computedStyle.borderRightWidth);  // ClientWidth 最大值(理想情况下, 也就是不存在滚动条)

    var scrollbarWidth = maxClientWidth          // 滚动条大小
        - ceil(ele.clientWidth);

    console.log(scrollbarWidth); // 17

这样就计算出滚动条的 width, 高度同理可得.

上面因为使用 getComputedStyle 只能兼容到 IE9+、Chrome、Firefox、Safari 等 , 需要兼容IE8以及以下版本,可以作下兼容处理:


    // 这里如果IE8以下则需要改为 getElementById
    var selector = function (selectorName) {
        if (!selectorName && !(typeof selectorName === "string")) { return {} };
        var prefix = /^#/;
        if (document.querySelector) {
            selectorName = prefix.test(selectorName) ? selectorName.substr(1) : selectorName;
            return document.querySelector(selectorName);
        } else if(document.getElementById) {
            selectorName = prefix.test(selectorName) ? selectorName : "#" + selectorName;
            return document.getElementById(selectorName);
        }
    }

    var selector = selector("box");  
    var computedStyle = window.getComputedStyle === undefined ? ele.currentStyle : window.getComputedStyle(ele);
    var offsetWidth = ele.offsetWidth; // content widht + padding width + border width (包含滚动条)
    var ceil = function (v) {
        return Math.ceil(parseFloat(v))
    }
    var maxClientWidth = offsetWidth
        - ceil(computedStyle.borderLeftWidth)
        - ceil(computedStyle.borderRightWidth);  // ClientWidth 最大值(理想情况下, 也就是不存在滚动条)

    var scrollbarWidth = maxClientWidth          // 滚动条大小
        - ceil(ele.clientWidth);

    console.log(scrollbarWidth); // 17

元素内部实际可用区域

可用区域其实就是内容可以排放的空间, 其可见区域宽度其实就是 clientWidth - paddingLeftWidth - paddingRightWidth 的值. 

比求滚动条宽度相对简单点, 只需要知道左右内边距的大小即可.


     var selector = function (selectorName) {
        if (!selectorName && !(typeof selectorName === "string")) { return {} };
        var prefix = /^#/;
        if (document.querySelector) {
            selectorName = prefix.test(selectorName) ? selectorName.substr(1) : selectorName;
            return document.querySelector(selectorName);
        } else if(document.getElementById) {
            selectorName = prefix.test(selectorName) ? selectorName : "#" + selectorName;
            return document.getElementById(selectorName);
        }
    }

    var selector = selector("box");  
    var computedStyle = window.getComputedStyle === undefined ? ele.currentStyle : window.getComputedStyle(ele);
    var ceil = function (v) {
        return Math.ceil(parseFloat(v))
    }
    console.log(ele.clientWidth - ceil(computedStyle.paddingLeft) - ceil(computedStyle.paddingRight));

上面计算方式没有什么固定,仅供参考. 更好计算方式也可以推荐下. ~ 比如通过 getBoundingClientRect()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163068.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 深度学习#1.有监督学习和无监督学习

    深度学习#1.有监督学习和无监督学习机器学习#1.有监督学习和无监督学习人工智能与机器学习与深度学习机器学习有监督学习无监督学习人工智能与机器学习与深度学习什么是人工智能?人类用各种方式让机器拥有与人类相仿的“智能”,就是人工智能。什么是机器学习?机器学习就是实现人工智能的一种方式。什么是深度学习?深度学习就是实现机器学习的一种技术。机器学习为什么要机器学习呢?你想啊,人类这么聪明是为什么,还不是因为在整个人类历史…

    2022年5月27日
    45
  • 服务器中了挖矿病毒怎么办

    服务器中了挖矿病毒怎么办前言服务器好端端的竟然中了挖矿病毒!!!可怜我那1核2G的服务器,又弱又小,却还免除不了被拉去当矿工的命运,实在是惨啊惨。事情原来是这样的。。。就在今天下午,我准备登陆自己的远程服务器搞点东西的时候,突然发现ssh登陆不上了。如上,提示被拒绝。这个问题很明显就是服务器没有我的公钥,或者不识别我的公钥,然后拒绝登录。这就很难办了,我确定我的公钥是一直没有变动过的,不应该会出现这种情况啊。还有让我头疼的是,我当初为了安全起见,设置过此台服务器只能通过ssh的方式

    2022年6月3日
    102
  • SpringBoot面试总结「建议收藏」

    SpringBoot面试总结「建议收藏」SpringBoot面试总结一.SpringBoot是什么?Springboot是一个脚手架(而非框架),构建于Spring框架(Framework)基础之上,基于快速构建理念,提供了自动配置功能,可实现其开箱即用特性(创建完一个基本的项目以后,可零配置或者少量配置即可运行我们的项目)。2.SpringBoot的核心特性是什么?起步依赖自动配置健康检查3.SpringBoot项目启动原理?SpringBoot项目在启动时,首先基于启动入口类上的注解描述,进行自动配置并扫描启动类所在

    2022年6月7日
    30
  • 最新PHP 面试、笔试题汇总(code happy)[通俗易懂]

    最新PHP 面试、笔试题汇总(code happy)[通俗易懂]一、秒杀(商品超卖,高并发,同一用户多次抢购) 后端:redis+队列 redis队列实现,三个队列(库存队列,排队队列,抢购结果队列) 用户先进入排队队列,先进先出,判断是否已经在抢购结果队列,如果在,则直接下一个,如果不在,将用户信息加入抢购结果队列,库存-1,等待数据库空闲时,将抢购结果写入数据库 前端: 面对高并发的抢购活动,前端常用的三板斧是【扩容】【静态化】【限流】 扩容:加机器,这是最简单的方法,通过增加前端池的整体承载量来抗峰值。 静态化:将活动页面上的所有可以静态的

    2022年6月1日
    34
  • vs中文乱码怎么解决_visual studio code中文乱码

    vs中文乱码怎么解决_visual studio code中文乱码在Windows下使用VSCode编译运行,都出现中文乱码的问题,今天我就遇见了这种情况,上网搜了半天也没有找到正确的解决方法,现将我把我的方法晒一下.中文的windows下的cmd默认使用GBK的编码,每次需要的时候只要在VSCode终端输入命令:chcp65001,切换代码页到使用UTF-8就可以解决中文代码的问题,只不过这种方法太麻烦了,每次进入终端都要输入命令,那有没有永久性解决…

    2022年8月30日
    1
  • 电商网站商品价格获取方法_电商网站

    电商网站商品价格获取方法_电商网站电商网站商品价格获取本文以苏宁易购,京东,两个电商网站,模仿说明电商网站商品价格的两种获取方法。json形式存放,京东商品的价格以json形式存放,以以下页面为例https://item.jd.com/100000287133.html明显价格数据并非放在前端页面里,搜索找到以下数据查看当前的url在浏览器中请求https://p.3.cn/prices/mgets?cal…

    2022年10月1日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号