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

Implementation of importing and exporting docker images

Docker usage of gitlab gitlab docker Startup Comm...

Detailed explanation of pid and socket in MySQL

Table of contents 1. Introduction to pid-file 2.S...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Use of Linux chkconfig command

1. Command Introduction The chkconfig command is ...

A brief discussion on CSS blocking merging and other effects

Non-orthogonal margins When margin is used, it wi...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

react-diagram serialization Json interpretation case analysis

The goal of this document is to explain the Json ...

Basic knowledge: What does http mean before a website address?

What is HTTP? When we want to browse a website, w...

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

The current better way to make select list all options when selected/focused

During development, I encountered such a requireme...

Pure CSS3 to achieve mouse over button animation Part 2

After the previous two chapters, do you have a ne...

MySQL index usage instructions (single-column index and multi-column index)

1. Single column index Choosing which columns to ...

The complete usage of setup, ref, and reactive in Vue3 combination API

1. Getting started with setUp Briefly introduce t...

Mini Program to implement Token generation and verification

Table of contents process Demo Mini Program Backe...