jQuery之位置坐标图形相关方法(offset\offset\position\scrollTop\scrollLeft\width\height\innerWidth\outerWidth)

offset取值、offset设值

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温故而知"心"</title>
    <style>
        .wrapper {
            position: absolute;
            width: 200px;
            height: 200px;
            background-color: #ff0000;
            left: 70px;
            top: 70px;
        }

        .wrapper .demo {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: #0000ff;
            left: 20px;
            top: 20px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="demo"></div>
    </div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //offset取值
    //得到top和left距离文档的距离
    console.log($('.demo').offset())

    //offset设值
    $('.demo').offset({ left: 200, top: 200 })
    console.log($('.demo').offset())
</script>

</html>

position

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温故而知"心"</title>
    <style>
        .wrapper {
            position: absolute;
            width: 200px;
            height: 200px;
            background-color: #ff0000;
            left: 70px;
            top: 70px;
        }

        .wrapper .demo {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: #0000ff;
            left: 20px;
            top: 20px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="demo"></div>
    </div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //position 和 css 定位规则一样
    console.log($('.demo').position())
</script>

</html>

scrollTop、scrollLeft

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温故而知"心"</title>
    <style>
        .wrapper {
            position: absolute;
            width: 200px;
            height: 200px;
            background-color: #ff0000;
            left: 70px;
            top: 70px;
        }

        .wrapper .demo {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: #0000ff;
            left: 20px;
            top: 20px;
        }

        body {
            width: 3000px;
            height: 3000px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="demo"></div>
    </div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
//    页面滚动了多少的距离(获取值)
//    $(window).scrollTop()
//    $(window).scrollLeft()

//    赋值
//    $(window).scrollTop(100)

</script>
</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温故而知"心"</title>
    <style>
        .test {
            width: 200px;
            height: 200px;
            overflow: auto;
            border: 7px solid #333333;
        }

        .test p {
            width: 3000px;
            height: 3000px;
            background-color: #ff0000;
        }
    </style>
</head>

<body>
    <div class="test">
        <p></p>
    </div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //检查div也是可以的
    // $('.test').scrollTop()
    //赋值也是可以的
     // $('.test').scrollTop(0)
</script>

</html>

width、height、innerWidth、innerHeight、outerWidth、outerHeight

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温故而知"心"</title>
    <style>
        .demo {
            width: 200px;
            height: 200px;
            background-color: #ff0000;
            border: 20px solid #333333;
            padding: 15px;
            margin: 10px;
        }
    </style>
</head>

<body>
    <div class="demo"></div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    // width height
    // content
    console.log($('.demo').width())
    console.log($('.demo').height())

    // innerWidth innerHeight
    // content + padding
    console.log($('.demo').innerWidth())
    console.log($('.demo').innerHeight())

    // outerWidth outerHeight
    // content + padding + border
    console.log($('.demo').outerWidth())
    console.log($('.demo').outerHeight())

    // outerWidth outerHeight
    // content + padding + border + margin
    console.log($('.demo').outerWidth(true))
    console.log($('.demo').outerHeight(true))
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值