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
Docker usage of gitlab gitlab docker Startup Comm...
Table of contents 1. Introduction to pid-file 2.S...
Disable swap If the server is running a database ...
1. Command Introduction The chkconfig command is ...
Non-orthogonal margins When margin is used, it wi...
background Recently, a leader wanted us to build ...
The goal of this document is to explain the Json ...
What is HTTP? When we want to browse a website, w...
Table of contents Congruent and Incongruent congr...
During development, I encountered such a requireme...
Test environment: C:\>systeminfo | findstr /c:...
After the previous two chapters, do you have a ne...
1. Single column index Choosing which columns to ...
1. Getting started with setUp Briefly introduce t...
Table of contents process Demo Mini Program Backe...