jQuery常用API(六)

本文详细介绍了如何使用jQuery进行元素的尺寸(width(), height(), innerWidth(), outerWidth()等)测量和设置,以及position()、offset()和scrollTop()/scrollLeft()方法来处理元素位置。实例演示了如返回顶部按钮和品优购电梯导航的实现。
摘要由CSDN通过智能技术生成

目录

7. jQuery尺寸、位置操作

7.1 jQuery尺寸

7.2 jQuery位置

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

 2. position()获取元素偏移

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

 案例:带有动画的返回顶部

案例:品优购电梯导航


 

7. jQuery尺寸、位置操作

7.1 jQuery尺寸

语法用法
width() / height()取得匹配元素宽度和高度值 只算width / height
innerWidth() / innerHeight()取得匹配元素宽度和高度值,包括padding
outerWidth() / outerHeight()取得匹配元素宽度和高度值 包含padding、border
outerWidth(true) / outerHeight(true)

取得匹配元素宽度和高度值,包含padding、border、margin

  • 以上参数为空,则是获取相应值,返回的是数字型
  • 如果参数为数字,则是修改相应值
  • 参数可以不必写单位
<style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            padding: 10px;
            border: 15px solid red;
            margin: 20px;
        }
    </style>
    <script src="jquery.min.js"></script>

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

 

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

<body>
    <div></div>
    <script>
        $(function () {
            // 1. width() / height() 获取设置元素 width和height大小 
            console.log($("div").width());
        
            // 2. innerWidth() / innerHeight()  获取设置元素 width和height + padding 大小 
            console.log($("div").innerWidth());
        })
    </script>
</body>

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

<body>
    <div></div>
    <script>
        $(function () {
            // 1. width() / height() 获取设置元素 width和height大小 
            console.log($("div").width());
        
            // 2. innerWidth() / innerHeight()  获取设置元素 width和height + padding 大小 
            console.log($("div").innerWidth());

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

           

        })
    </script>
</body>

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

<body>
    <div></div>
    <script>
        $(function () {
            // 1. width() / height() 获取设置元素 width和height大小 
            console.log($("div").width());
        
            // 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>

                                                

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

<body>
    <div></div>
    <script>
        $(function () {
            $("div").outerWidth(100);
        })
    </script>
</body>

 使用$("div").outWidth(100),将border+padding+width的值改为了100px,但是border、padding值固定,一共50px,所以width值变为了50px

7.2 jQuery位置

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

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

(1)offset()方法这是或返回被选元素相对于文档的偏移坐标,跟父级没有关系,得到的是一个对象

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

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

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

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

        .son {
            width: 150px;
            height: 150px;
            background-color: purple;

        }
    </style>
    <script src="jquery.min.js"></script>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function () {
            // 1. 获取设置距离文档的位置(偏移) offset
            console.log($(".son").offset());
            console.log($(".son").offset().left);
        })
    </script>
</body>

 

 如果设置了定位,还修改了子盒子的偏移量,会发现子盒子相对于父盒子的定位对子盒子的偏移量没有影响:

 <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: 20px;

        }
    </style>
    <script src="jquery.min.js"></script>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function () {
            // 1. 获取设置距离文档的位置(偏移) offset
             $(".son").offset({
                left: 200,
                top: 200
            });
            console.log($(".son").offset());
        })
    </script>
</body>

 

 2. position()获取元素偏移

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

(2)这个方法只能获取不能设置

 <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: 20px;

        }
    </style>
    <script src="jquery.min.js"></script>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function () {
             // 2. 获取距离带有定位父级位置(偏移) position   如果没有带有定位的父级,则以文档为准
            console.log($(".son").position());
        })
    </script>
</body>

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

 

(1)scrollTop()方法设置或返回被选元素被卷去的头部

(2)scrollTop(值)设置被选元素被卷去的头部

<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>
<body>
    <div class="back">返回顶部</div>
    <div class="container">
    </div>
    <script>
        $(function () {
            $(document).scrollTop(200); //使页面一开始就位于距离文档200px的位置
            // 页面滚动事件
            $(window).scroll(function () {
                if ($(document).scrollTop() >= $(".container").offset().top) {
                    $(".back").fadeIn();
                } else {
                    $(".back").fadeOut();
                }
            });
        })
    </script>
</body>

 

 案例:带有动画的返回顶部

 注意:

只能给元素做动画 ,不能给文档document做动画,所以给body和html添加动画效果

 <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>

<body>
    <div class="back">返回顶部</div>
    <div class="container">
    </div>
    <script>
        $(function () {
            $(document).scrollTop(200); //使页面一开始就位于距离文档200px的位置
            // 页面滚动事件
            $(window).scroll(function () {
                if ($(document).scrollTop() >= $(".container").offset().top) {
                    $(".back").fadeIn();
                } else {
                    $(".back").fadeOut();
                }
            });
            // 返回顶部
            $(".back").click(function () {
                $("body,html").stop().animate({  //stop()动画停止排队                    scrollTop: 0
                });
            });
        })
    </script>
</body>

案例:品优购电梯导航

--------------------------------------------------显示隐藏电梯导航-------------------------------------------------------

 步骤:

  1. 给窗口绑定滚动事件
  2. 如果文档滚动的距离大于等于今日推荐模块距离文档顶部的距离,就显示导航模块
  3. 如果小于,就隐藏导航模块

$(function () {
    $(window).scroll(function () {
        if ($(document).scrollTop() >= $(".recommend").offset().top) {
            $(".fixedtool").fadeIn();
        } else {
            $(".fixedtool").fadeOut();
        }
    });
})

----------------------------------------------------点击滚动到目标位置--------------------------------------------------

步骤:

  1. 把电梯导航中的所有盒子获取过来,绑定点击事件
  2. 每点击电梯导航中的一个小盒子的时候,就获取到它的index
  3. 通过index找到内容区域中的对应元素,获取到它距离文档顶部的距离
  4. 给页面元素(body,html)设置动画效果,使页面滚动的距离作为步骤3得到的距离 

 注意:

1.电梯导航和内容区域是一一对应的,点击电梯导航对应的盒子,返回点击的盒子对应的index,然后根据index去选择内容区域中的第几个盒子,页面要滚动去的距离就是被选中的内容区域的盒子距离文档顶部的距离(offset().top)

 // 2.点击电梯导航页面可以滚动到相应内容区域
    $(".fixedtool li").click(function () {
        // console.log($(this).index());
        // 页面要滚动到的距离
        var des = $(".floor .w").eq($(this).index()).offset().top;
        $("body,html").stop().animate({
            scrollTop: des
        });
    });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值