offset().left的获取的长度到底在那的实验

对于offset().left,top其实在网上很多说的都是迷迷糊糊的搞得人晕头转向的

所以我自己写了段代码证明一下到底是咋回事!

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        *{
            padding: 0;
            margin: 0;
            border:0;
        }
        .a{
            width: 400px;
            height: 400px;
            background-color: #222222;
            position: relative;
            left:100px;

        }
        .b{
            width: 200px;
            height: 200px;
            background-color: blue;
            position: absolute;
            top:100px;
            left:50px;
        }
        .cp{
            width: 50px;
            height: 20px;
        }
    </style>
</head>

<body>
    <div class="a">
        <div class="b">
            <div class="cp">hello</div>
        </div>
    </div>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.0.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(".cp").html("left"+$(".cp").offset().left+"top:"+$(".cp").offset().top);
        });
    </script>
</body>
</html>

 

 

结果如下图:

 

然后把CSS修改为:

<style type="text/css">
.a{
width: 2400px;
height: 4000px;
background-color: #222222;
position: relative;
left:100px;

}
.b{
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
top:1000px;
left:50px;
}

即对以下作出修改:
.a{
width: 2400px;
height: 4000px;
}

.b{
top:1000px;
}

结果如下:

 

说明是根据DOC文件来的,于当前页无关或者当前窗口无关,如果还没明白可以复杂代码操作下!

转载于:https://www.cnblogs.com/zhujifa/p/10766061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值