jQuery偏移量offset

jQuery偏移量offset

jquery的参考文档地址:http://jquery.cuishifeng.cn/

获取匹配元素在当前视口的相对偏移。参照物是可视窗口。

返回的对象包含两个整型属性:top 和 left,以像素计。此方法只对可见元素有效。

position和offset相似,position的参照物是父亲窗口,必须是已经定位的父亲窗口。

初始:

点击按钮之后:

点击按钮设置偏移量,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .c1{
            width: 200px;
            height: 200px;
            background-color: #2459a2;
        }
    </style>
</head>
<body>



<div class="c1"></div>
<button>change</button>
<script src="jquery-3.1.1.js"></script>

<script>

    // offset方法的参照物是可视窗口
    console.log($(".c1").offset());         // 偏移量对象
    console.log($(".c1").offset().top);     // 偏移量对象
    console.log($(".c1").offset().left);     // 偏移量对象
    
//    var top1=100
    $("button").click(function () {
        $(".c1").offset({"top":100,left:200})
    })
</script>
</body>
</html>

 

 

转载于:https://www.cnblogs.com/litzhiai/p/8540490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值