(JavaScript learning record): jQuery size and position operation

catalogue

jQuery size and position operation

jQuery size

jQuery location

offset() sets or gets the element offset

position() gets the element offset

scrollTop()/scrollLeft() sets or gets the header and left side of the rolled element

jQuery size and position operation

jQuery size

  • If the above parameter is empty, the corresponding value is obtained and the returned value is numeric.
  • If the parameter is a number, the corresponding value is modified.
  • It is not necessary to write unit for parameters.

jQuery location

  • There are three main positions: offset(), position(), scrollTop()/scrollLeft()

offset() sets or gets the element offset

  • ① The offset() method sets or returns the offset coordinates of the selected element relative to the document, which has nothing to do with the parent.
  • ② This method has two attributes: left and top.
    • offset().top is used to obtain the distance from the top of the document, offset() Left is used to get the distance to the left of the document.
  • ③ You can set the offset of the element: offset ({top: 10, left: 30});

position() gets the element offset

  • ① The position() method is used to return the offset coordinates of the selected element relative to the parent with positioning. If the parent has no positioning, the document shall prevail.
  • ② This method has two attributes: left and top.
    • position()top is used to obtain the distance from the top of the positioning parent, position() Left is used to get the distance to the left of the positioning parent.
  • ③ This method can only get.
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        $(function() {
            // 1. Get and set the position (offset) offset from the document
            console.log($(".son").offset());
            console.log($(".son").offset().top);
            // $(".son").offset({
            //     top: 200,
            //     left: 200
            // });
           // 2. Obtain the position (offset) from the parent with positioning. If there is no parent with positioning, the document shall prevail
            // This method can only get and cannot set the offset
            console.log($(".son").position());
        })
    </script>
</body>

scrollTop()/scrollLeft() sets or gets the header and left side of the rolled element

  • ① The scrollTop() method sets or returns the header of the selected element to be rolled away.
  • ② If the parameter is not followed, it is obtained. If the parameter is a number without unit, it is used to set the rolled head.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <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">Back to top</div>
    <div class="container">
    </div>
    <script>
        $(function() {
            $(document).scrollTop(100);
            // Rolled head scrollTop() / rolled left scrollLeft()
            // Page scroll event
            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 to top
            $(".back").click(function() {
                // $(document).scrollTop(0);
                $("body, html").stop().animate({
                    scrollTop: 0
                });
                // $(document).stop().animate({
                //     scrollTop: 0
                // });  It can't be a document, but html and body elements for animation
            })
        })
    </script>
</body>

</html>

 

Posted by Denholm on Sat, 14 May 2022 06:50:39 +0300