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

Nginx dynamic and static separation implementation case code analysis

Separation of static and dynamic Dynamic requests...

Introduction and use of Javascript generator

What is a generator? A generator is some code tha...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

Vue implements student management function

This article example shares the specific code of ...

js to upload pictures to the server

This article example shares the specific code of ...

A brief discussion on the VUE uni-app life cycle

Table of contents 1. Application Lifecycle 2. Pag...

About MariaDB database in Linux

Table of contents About MariaDB database in Linux...

How to understand Vue front-end and back-end data interaction and display

Table of contents 1. Technical Overview 2. Techni...

Eight examples of how Vue implements component communication

Table of contents 1. Props parent component ---&g...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...

MYSQL custom function to determine whether it is a positive integer example code

You can write a function: Mainly use regular expr...

Web Design Experience

<br />The author used to be a novice in web ...

Correct use of MySQL partition tables

Overview of MySQL Partitioned Tables We often enc...

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...