DOM-BOM-EVENT(5)

5.宽、高、位置相关

5.1.clientX/clientY

clientX和clientY表示鼠标在浏览器可视区的坐标位置

<script>
    document.onclick = function(ev){
        var ev = ev || event
        alert("clientX:"+ev.clientX+", clientY:"+ev.clientY)
    }
</script>

5.2.pageX/pageY

pageX和pageY表示鼠标在网页文档中的坐标位置,这里需要注意:网页的宽高是可以大于浏览器可视区的

<!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>
        #box{
            width: 300px;
            height: 2000px;
            background-color: #009f95;
        }
    </style>
</head>
<body>
    <div id="box"></div>

    <script>
        document.onclick = function(ev){
            var ev = ev || event
            alert("clientX:"+ev.pageX+", clientY:"+ev.pageY)
        }
    </script>
</body>
</html>

5.3.offsetLeft/offsetTop/offsetWidth/offsetHeight/offsetParent

offsetLeft和offsetTop是相对于定位父级的坐标位置

offsetWidth和offsetHeight是元素实体所占的总宽高,例如:总宽度 = 内容 + padding + border

offsetParent表示定位父级

<!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>
        #box{
            width: 400px;
            height: 400px;
            background-color: #009f95;
            position: absolute;
            left: 200px;
            top:100px;
        }
        #box-inner{
            width: 100px;
            height: 100px;
            background-color: orangered;
            position: absolute;
            left: 50px;
            top: 50px;
            padding: 50px;
        }
    </style>
</head>
<body style="position:relative">
    <div id="box">
        <div id="box-inner"></div>
    </div>
    <script>
        var oBoxIner = document.getElementById("box-inner")
        var oBox = document.getElementById("box")
        document.onclick = function(ev){
            var ev = ev || event
            // 获取left 和 top偏移量
            alert("offsetLeft:"+oBoxIner.offsetLeft+", offsetTop:"+oBoxIner.offsetTop)
            // 获取当前元素的宽高, width = 内容宽高+padding+border
            alert("offsetWidth:"+oBoxIner.offsetWidth+", offsetHeight:"+oBoxIner.offsetHeight)
            // 获取元素的最近的定位父级
            console.log("offsetParent:"+oBoxIner.offsetParent)
        }
    </script>
</body>
</html>

5.4.scrollLeft/scrollTop/scrollWidth/scrollHeight

scrollLeft 和 scrollTop 表示元素滚出去的距离

scrollWidth和scrollHeight 对象的实际内容的宽高,会随对象中内容超过可视区后而变大

<!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>
        #box{
            width: 300px;
            height: 200px;
            border: 5px solid black; 
            margin: 100px auto;
            overflow: auto;
            /* box-sizing: border-box; */
        }
        #box-inner{
            width: 450px;
            margin: 100px auto;
            height: 300px;
            border: 1px solid red; 
            background-color: #009f95
        }
    </style>
</head>
<body>
    <div id="box">
        <div id="box-inner"></div>
    </div>
    <script>
        var oBox = document.getElementById("box")
        var oBoxInner = document.getElementById("box-inner")

        oBox.onclick = function(){
            // 顶部滚动距离
            alert("scrollTop:"+this.scrollTop+"scrollLeft:"+this.scrollLeft)

            alert("scrollWidth:"+this.scrollWidth+",scrollHeight:"+this.scrollHeight)
        }
    </script>
</body>
</html>

5.5.clientWidth/clientHeight

<!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>
        #box{
            width: 400px;
            height: 400px;
            background-color: #009f95;
            position: absolute;
            left: 200px;
            top:100px;
        }
        #box-inner{
            width: 100px;
            height: 100px;
            background-color: orangered;
            position: absolute;
            left: 50px;
            top: 50px;
            padding: 50px;
            border: 20px solid black;
        }
    </style>
</head>
<body style="position:relative">
    <div id="box">
        <div id="box-inner"></div>
    </div>
    <script>
        var oBoxIner = document.getElementById("box-inner")
        var oBox = document.getElementById("box")
        document.onclick = function(ev){
           var ev = ev || event
           //元素可视区宽高, 内容+padding 不包括边框
           alert("clientWidth:"+oBoxIner.clientWidth+", clientHeight:"+oBoxIner.clientHeight)      
        }
    </script>
</body>
</html>

5.6.总结

offsetWidth/offsetHeight clientWidth/clientHeight scrollWidth/scrollHeight

<!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>
        #box{
            width: 300px;
            height: 200px;
            border: 5px solid black; 
            margin: 100px auto;
            overflow: auto;
            padding: 20px;
          
        }
        #box-inner{
            width: 450px;
            margin: 100px 300px;
            height: 300px;
            border: 1px solid red; 
            background-color: #009f95
        }
    </style>
</head>
<body>
    <div id="box">
        <div id="box-inner"></div>
    </div>
    <script>
        var oBox = document.getElementById("box")
        var oBoxInner = document.getElementById("box-inner")

        oBox.onclick = function(){
            // 顶部滚动距离
            console.log("有滚动条的时候-----》")
            console.log("scrollWidth:"+this.scrollWidth)

            //offsetWidth
            console.log("offsetWidth-----》")
            console.log("offsetWith:"+this.offsetWidth)

            // clientWidth
            console.log("clientWidth-----》")
            console.log("clientWidth:"+this.clientWidth)
        }
    </script>
</body>
</html>

总结: 1. clientWidth和offsetWidth返回的值和内部元素无关,它们两个的区别是,前者返回的值是:width+padding ,后者返回的值是:width+padding+border 2. scrollWidth返回的值和内部的元素有关,它返回的是内部元素实际的宽带,包括margin、padding、border等 等,会随着内部元素所占宽度的增加而增加

5.7.如何获取文档的宽/高?

document.documentElement.scrollWidth/scrollHeight

5.8.如何获取可视区宽/高?

docuement.documentElement.clientWidth document.documentElement.clientHeight

5.9.如何获取页面的滚动距离?

document.documentElement.scrollTop/scrollLeft

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大地飞鸿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值