scroll、client、offset系列的区分

目录

代码部分

滚动条的默认样式以及如何修改样式


代码部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            height: 1200px;
        }
        div {
            position: relative;
            top: 20px;
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 10px solid red;
            padding: 10px;
            overflow: auto;
        }
    </style>
</head>

<body>
    <div>
        我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容  我是内容 我是内容 
    </div>
    <script>
        // // 纵向参数
        // var div = document.querySelector('div');
        // console.log('clientHeight,元素含内边距的高度为' + div.clientHeight);  //220      height+padding
        // console.log('clientTop,元素含内边距的高度为' + div.clientTop);  //10      border-top/bottom
        // console.log('offsetHeight,元素含外边距的高度为' + div.offsetHeight);  //240      height+padding+margin
        // console.log('offsetTop,元素距离页面边缘的距离为' + div.offsetTop);        //0        top
        // console.log('scrollHeight,元素内容的高度为' + div.scrollHeight);  //249      hiddenContent
        // console.log('scrollTop,元素内容被卷起的高度为' + div.scrollTop);        //0        top

        // // scroll滚动事件当我们滚动条发生变化会触发的事件
        // window.addEventListener('scroll', function() {
        //     console.log('页面被卷起的高度'+parseInt(document.documentElement.scrollTop));
        // })
        // div.addEventListener('scroll', function() {
        //     console.log('div被卷起的高度'+parseInt(div.scrollTop) );
        // })

        // 横向参数
        var div = document.querySelector('div');
        console.log('clientWidth,元素内边距不含滚动条17的宽度为' + div.clientWidth);  //203      width+padding-滚动条宽度17
        console.log('clientLeft,左边框宽度宽度为' + div.clientLeft);  //10      border-left/right
        console.log('offsetWidth,元素含外边距的宽度为' + div.offsetWidth);  //240      width+padding+margin
        console.log('offsetLeft,元素距离页面边缘的距离为' + div.offsetLeft);        //8        left
        console.log('scrollWidth,元素内容的宽度为' + div.scrollWidth);  //203      hiddenContent
        console.log('scrollLeft,元素内容被卷起的宽度为' + div.scrollLeft);        //0        left

        window.addEventListener('scroll', function() {
            console.log('页面被卷起的宽度'+parseInt(document.documentElement.scrollLeft));
        })
        div.addEventListener('scroll', function() {
            console.log('div被卷起的宽度'+parseInt(div.scrollLeft) );
        })
    </script>
</body>
</html>

请复制以上代码到编辑器里,自行修改css属性体会这三个系列的区别

滚动条的默认样式以及如何修改样式

滚动条的位置包括在padding里,且宽17px,这是默认样式,也可以修改

下方给出修改滚动条样式方法的链接,侵删

CSS 设置滚动条样式 ::-webkit-scrollbar - 槑孒 - 博客园

浏览器滚动条样式设置 - 菜鸟成长日记lx - 博客园

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

John_Crown

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值