【jQuery】常用API——尺寸、位置操作

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

一、jQuery 尺寸

以上参数为空则是获取相应值返回的是数字型。

如果参数为数字则是修改相应值。

参数可以不必写单位。

    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            padding: 10px;
            border: 15px solid red;
            margin: 20px;
        }
    </style>
    <script src="../jquery.min.js"></script>
</head>

<body>
    <div></div>
    <script>
        $(function () {
            // 1. width() / height() 获取元素 width和height大小 
            console.log($("div").width());
            // $("div").width(300); //修改

            // 2. innerWidth() / innerHeight()  获取设置元素 width和height + padding 大小 
            console.log($("div").innerWidth());

            // 3. outerWidth()  / outerHeight()  获取设置元素 width和height + padding + border 大小 
            console.log($("div").outerWidth());

            // 4. outerWidth(true) / outerHeight(true) 获取设置 width和height + padding + border + margin
            console.log($("div").outerWidth(true));
        })
    </script>
</body>

二、jQuery 位置

位置主要有三个offset()、position()、scrollTop() / scrollLeft()

1. offset() 设置或获取元素偏移

① offset() 方法设置或返回被选元素相对于文档的偏移坐标跟父级没有关系。

② 该方法有2个属性 left、top 。offset().top 用于获取距离文档顶部的距离offset().left 用于获取距离文档左侧的距离。

③ 可以设置元素的偏移offset({ top: 10, left: 30 });

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .father {
            width: 400px;
            height: 400px;
            background-color: pink;
            margin: 100px;
            overflow: hidden;
            position: relative;
        }

        .son {
            width: 150px;
            height: 150px;
            background-color: purple;
            position: absolute;
            left: 10px;
            top: 10px;
        }
    </style>
    <script src="../jquery.min.js"></script>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function () {
            // 1. 获取距离文档的位置偏移 offset
            console.log($(".son").offset());
            console.log($(".son").offset().top);

            // 修改
            // $(".son").offset({
            //     top: 200,
            //     left: 200
            // });
        })
    </script>
</body>

2. position() 获取元素偏移

① position() 方法用于返回被选元素相对于带有定位的父级偏移坐标如果父级都没有定位则以文档为准。

② 该方法有2个属性 left、top。position().top 用于获取距离定位父级顶部的距离position().left 用于获取距离定位父级左侧的距离。

③ 该方法只能获取

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .father {
            width: 400px;
            height: 400px;
            background-color: pink;
            margin: 100px;
            overflow: hidden;
            position: relative;
        }

        .son {
            width: 150px;
            height: 150px;
            background-color: purple;
            position: absolute;
            left: 10px;
            top: 10px;
        }
    </style>
    <script src="../jquery.min.js"></script>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function () {
            // 2. 获取距离带有定位父级位置偏移 position   如果没有带有定位的父级则以文档为准
            // 这个方法只能获取不能设置偏移
            console.log($(".son").position());
        })
    </script>
</body>

3. scrollTop()/scrollLeft() 设置或获取元素被卷去的头部和左侧

不跟参数是获取参数为不带单位的数字则是设置被卷去的头部。

    <style>
        body {
            height: 2000px;
        }

        .back {
            position: fixed;
            width: 50px;
            height: 50px;
            background-color: pink;
            right: 30px;
            bottom: 100px;
            display: none;
        }

        .container {
            width: 900px;
            height: 500px;
            background-color: skyblue;
            margin: 400px auto;
        }
    </style>
    <script src="../jquery.min.js"></script>
</head>

<body>
    <div class="back">返回顶部</div>
    <div class="container">
    </div>
    <script>
        $(function () {
            // 刷新页面就会让文档页面直接跳到100
            // $(document).scrollTop(100);
            
            // 被卷去的头部 scrollTop()  / 被卷去的左侧 scrollLeft()
            var boxTop = $(".container").offset().top;
            // 页面滚动事件
            $(window).scroll(function () {
                // console.log(11);
                console.log($(document).scrollTop());
                if ($(document).scrollTop() >= boxTop) {
                    $(".back").fadeIn();
                } else {
                    $(".back").fadeOut();
                }
            });
            // 返回顶部
            $(".back").click(function () {
                // $(document).scrollTop(0);
                $("body, html").stop().animate({
                    scrollTop: 0
                });
                // $(document).stop().animate({
                //     scrollTop: 0
                // }); 不能是文档而是 html和body元素做动画
            })
        })
    </script>
</body>
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6