Detailed explanation of three commonly used web effects in JavaScript

Detailed explanation of three commonly used web effects in JavaScript

1 element offset series

1.1 Offset Overview

Offset meaning: Offset means offset. Using the related properties of offset, you can dynamically obtain the position, size, etc. of the element.

property illustrate
offsetLeft Returns the offset of an element relative to the left border of its positioned parent element.
offsetTop Returns the offset of an element above its positioned element.
offsetWidth Returns the width of the element (including padding, borders, and the width of the content area). Note that the returned value has no unit
offsetHeight Returns the height of the element (including padding, borders, and content area). Note that the returned value has no unit
offsetParent Returns the element that is the parent of this element with a positioned element (if none of the parents are positioned, returns body)

Get the mouse position: Schematic analysis of the coordinate position of the mouse pointer in the box.

Example: Write a box, output the width and height of the box in the terminal, and obtain and output the coordinates of the mouse pointer in the box

    <div id="box"></div>
    <script>
        var box = document.querySelector('#box');
        //1. Output the width and height of the box console.log("width:", box.offsetWidth);
        console.log("Height:", box.offsetHeight);
        //2. Bind the mouse movement event to boxbox.addEventListener('mousemove', function (e) {
            //2.1 Get the offset of box var left = box.offsetLeft;
            var top = box.offsetTop;
            console.log("Offset: (" + left + "," + top + ")");
            //2.2 Calculate the coordinates of the mouse pointer in the box var x = e.pageX - left;
            var y = e.pageY - top;
            console.log("x-axis coordinate: " + x + ", y-axis coordinate: " + y);
        })
    </script>

Every time the mouse moves a little in the box, the terminal will output the corresponding coordinates.

1.2 The difference between offset and style

offset style
offset can get the style value in any style sheet style can only get the style value in the inline style sheet
The values ​​obtained by the offset series are unitless. style.width gets a string with units
offsetWidth contains the values ​​of padding, border, and width The value obtained by style.width does not include padding and border
Attributes such as offsetWidth are read-only attributes and can only be obtained but not assigned. style.width is a read-write property that can be obtained or assigned

2 Elements of the visual area client series

Client series: client means client in Chinese. By using the related attributes of the client series, you can get the relevant information of the element's visual area.

property illustrate
clientLeft Returns the size of the element's left border
clientTop Returns the size of the element's top border
clientWidth Returns the width of the element itself (including padding) and the width of the content area (excluding borders). Note that the returned value has no unit
clientHeight Returns the height of the element itself (including padding) and the height of the content area (excluding borders). Note that the returned value has no unit

Example: Output the size of the top and left borders of an element, as well as its own width and height

    <div></div>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: aqua;
            border: 3px solid yellow;
        }
    </style>
    <script>
        //Get the div element let div = document.querySelector("div");
        //Output the size of the left and top borders of the element console.log("Size of the left border:", div.clientLeft);
        console.log("Top border size:", div.clientTop);
        // Output the element's own width and height (excluding borders)
        console.log("Width:", div.clientWidth);
        console.log("Height:", div.clientHeight);
    </script>

3 Elements Scroll Series

Meaning of scroll: scroll means scrolling. Using the related properties of the scroll series, you can dynamically obtain the scroll distance, size, etc. of the element.

property illustrate
scrollLeft Returns the left distance that was rolled away. The returned value has no unit.
scrollTop Returns the distance above the scrolled area. The returned value does not include units.
scrollWidth Returns the width of the element, excluding borders. Note that the returned value has no unit
scrollHeight Returns the height of the element, excluding borders. Note that the returned value has no unit

Example: Get the height and width of the div, and get the height of the rolled div

    <div>
        I am the content ...</div><br>
    <button>Click to get the rolled height and width</button>
    <style>
        div {
            width: 200px;
            height: 100px;
            background-color: pink;
            /* Scroll the elements that cannot be placed to display them*/
            overflow: scroll;
        }
    </style>
    <script>
        //1. Get the div and button elements in the page
        let div = document.querySelector("div");
        let button = document.querySelector("button");
        //2. Output its own height and width console.log("Height:", div.scrollHeight);
        console.log("Width:", div.scrollWidth);
        //Register a click event for the button, and output the height and width of the scroll after clicking button.addEventListener("click", function () {
            console.log("Scroll height:", div.scrollTop);
            console.log("Scroll width:", div.scrollLeft);
        })
    </script>

This concludes this article on the detailed explanation of three commonly used JavaScript web effects. For more relevant JavaScript web effects content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS realizes special effects of web page navigation bar
  • JS realizes web clock special effects
  • Using JS to create web page special effects_large picture carousel (example explanation)
  • Realizing web page positioning and navigation effects based on JQuery and native JavaScript
  • JavaScript special text input box web page special effects
  • Web clock special effects made with pure js code [with examples]
  • JavaScript uses output statements to implement web page special effects code

<<:  Examples of some usage tips for META tags in HTML

>>:  MySQL master-slave synchronization principle and application

Recommend

Hide div in HTML Hide table TABLE or DIV content css style

I solved a problem tonight that has been botherin...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

How to configure redis sentinel mode in Docker (on multiple servers)

Table of contents Preface condition Install Docke...

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

Write a shopping mall card coupon using CSS in three steps

Today is 618, and all major shopping malls are ho...

Sharing the structure and expression principles of simple web page layout

Introduction to structure and performance HTML st...

Detailed tutorial on deploying Django project under CentOS

Basic Environment Pagoda installation service [Py...

Implementation of VUE infinite level tree data structure display

Table of contents Component recursive call Using ...

React event mechanism source code analysis

Table of contents Principle Source code analysis ...

Example usage of JavaScript tamper-proof object

Table of contents javascript tamper-proof object ...

Two methods to disable form controls in HTML: readonly and disabled

In the process of making web pages, we often use f...

Use and analysis of Mysql Explain command

The mysql explain command is used to show how MyS...

How to access the local machine (host machine) in Docker

Question How to access the local database in Dock...

Details of MutationObServer monitoring DOM elements in JavaScript

1. Basic Use It can be instantiated through the M...

How to debug loader plugin in webpack project

Recently, when I was learning how to use webpack,...