1 element offset series1.1 Offset OverviewOffset meaning: Offset means offset. Using the related properties of offset, you can dynamically obtain the position, size, etc. of the element.
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
2 Elements of the visual area client seriesClient 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.
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 SeriesMeaning 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.
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:
|
<<: Examples of some usage tips for META tags in HTML
>>: MySQL master-slave synchronization principle and application
I solved a problem tonight that has been botherin...
This article shares the specific code for canvas ...
Table of contents Preface condition Install Docke...
Share a real-time clock effect implemented with n...
Today is 618, and all major shopping malls are ho...
Introduction to structure and performance HTML st...
Basic Environment Pagoda installation service [Py...
Table of contents Component recursive call Using ...
Table of contents Principle Source code analysis ...
Table of contents javascript tamper-proof object ...
In the process of making web pages, we often use f...
The mysql explain command is used to show how MyS...
Question How to access the local database in Dock...
1. Basic Use It can be instantiated through the M...
Recently, when I was learning how to use webpack,...