jQuery尺寸,位置操作

jQuery尺寸

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

代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            padding: 10px;
            border: 15px solid red;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div></div>
    <script src="../js/jQuery.js"></script>
    <script>
        $(function() {
            //width() / height() 获取/设置元素width和height大小
            console.log($('div').width);
            // $('div').width(300);

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

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

            //outerWidth() / outerhHeight() 获取/设置元素width和height + padding + border + margin大小
            console.log($('div').outerWidth(true));
        })
    </script>
</body>
</html>

 

jQuery位置

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

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

  1. offset()方法设置或返回被选元素相对于文档(document)的偏移坐标,跟父级没有关系
  2. 该方法有2个属性:left,top;offset().top用于获取距离文档顶部的距离,offset().left用于获取距离文档左侧的距离
  3. 可以设置元素的偏移:offset({top: 10, left: 30});

position()获取元素偏移

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

代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <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>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script src="../js/jQuery.js"></script>
    <script>
        $(function() {
            //获取/设置制距离文档的位置(偏移) offset
            console.log($('.son').offset());
            console.log($('.son').offset().top);//获取top属性
            // $('.son').offset({
            //     top: 200,
            //     left: 200
            // });

            //获取距离带有定位父级位置(偏移) position 如果没有带有定位父级, 则以文档为准
            console.log($('.son').position());
            $('.son').position({//这个方法只能获取不能设置
                top: 200,
                left: 200
            });
        })
    </script>
</body>
</html>

 

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

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

代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            margin: 500px auto;
            width: 1000px;
            height: 500px;
            background-color: pink;
        }
        .back {
            position: fixed;
            background-color: aqua;
            text-align: center;
            display: none;
            width: 45px;
            height: 45px;
            bottom: 50px;
            right: 30px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="back">返回顶部</div>
    <div class="container"></div>
    <script src="../js/jQuery.js"></script>
    <script>
        $(function() {
            $(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);
                //只有元素才能动画 不能使用document
                $('body, html').stop().animate({
                    scrollTop: 0
                })
            })
        })
    </script>
</body>
</html>

当中返回顶部的animate()只有元素才能使用动画,document不能用做动画

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值