1. Element offset seriesOffset is translated as offset. We use the offset series of related attributes to dynamically get the position (offset), size, etc. of the element.
Common attributes of offset are: For example: given a child box and a parent box, and giving them a certain size, let's see how these properties are obtained: <style> *{ margin: 0px; padding: 0px; } .father{ position: relative; margin-left: 50px; margin-top: 10px; width: 200px; height: 200px; background-color: brown; } .son{ width: 100px; height: 100px; background-color: cornflowerblue; } </style> </head> <body> <div class="father"> <div class="son"></div> </div> <script> var father = document.querySelector('.father'); var son = document.querySelector('.son') console.log('father.offsetLeft',father.offsetLeft); console.log('father.offsetTop',father.offsetTop); console.log('son.offsetWidth',son.offsetWidth); console.log('son.offsetHeight',son.offsetHeight); </script> </body> The print result is: We know that offset can help us get the size and parent element of an element, but the style attribute can also get related attributes, so what is the difference between them?
2. Element Visible Area Client SeriesClient is translated as client. We use the client series of related attributes to obtain relevant information about the element's visible area. The border size, element size, etc. of the element can be obtained dynamically through the related properties of the client series. For example, given a box with a border, return these properties and see the result. <style> .box{ width: 200px; height: 200px; background-color: darkorchid; border: 20px solid #ccc; } </style> </head> <body> <div class="box"></div> </body> <script> var box = document.querySelector('.box') console.log('box.clientWidth:'+box.clientWidth); console.log('box.clientWidth:'+box.clientWidth); console.log('box.clientWidth:'+box.clientWidth); console.log('box.clientTop:'+box.clientTop); </script> The result is: It can be found that the box size obtained by the client series does not include the box border. 3. Element scrolling seriesScroll means scrolling. We can use the related properties of the scroll series to dynamically get the size of the element, scroll distance, etc. For example, let’s print the scroll series properties of the box in the above example to see the result. <style> .box{ width: 200px; height: 200px; background-color: darkorchid; border: 20px solid #ccc; } </style> </head> <body> <div class="box"></div> </body> <script> var box = document.querySelector('.box') console.log('box.scrollWidth:'+box.scrollWidth); console.log('box.scrollHeight:'+box.scrollHeight); console.log('box.scrollLeft:'+box.scrollLeft); console.log('box.scrollTop:'+box.scrollTop); </script> The result is: We find that the height and width of the box we get are our given values, which are the same as the values obtained by the client series, so what is the difference between them? Now we add content to the box that exceeds the height of the box: <div class="box"> Wang Huan is studying front-end<br><br> Wang Huan is studying front-end<br><br> Wang Huan is studying front-end<br><br> Wang Huan is studying front-end<br><br> Wang Huan is studying front-end<br><br> Wang Huan is studying front-end<br><br> Wang Huan is learning front-end The print result is: It can be found that the height of the printed box has become larger. In fact, this value refers to the actual height of the box after adding the text. You will also find that the values of box.scrollLeft and box.scrollTop printed twice are both 0. What does this mean? Now we use overflow:auto to display the content outside the box as a scroll bar, and then add a scroll event to it, as shown below: var box = document.querySelector('.box') box.addEventListener('scroll',function(){ console.log('box.scrollTop:'+box.scrollTop); }) The effect is; It is found that the value obtained changes with the scrolling. In fact, box.scrollTop returns the upper distance that is scrolled away, as shown in the following figure: The above is the details of how to implement three common web effects (offset, client, scroll series) with JavaScript. For more information about JavaScript web effects, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: The hottest trends in web design UI in 2013 The most popular UI designs
>>: How to make the height of child div fill the remaining space of parent container in CSS
view What is a view? What is the role of a view? ...
In Linux, when a file is created, the owner of th...
There are many loopholes in the simple seamless s...
In an article a long time ago, I talked about the...
Find the problem Recently, I found a problem at w...
Does color influence website visitors? A few year...
border-radius: CSS3 rounded corners Syntax: borde...
Install Docker Update the yum package to the late...
Explain the whole process of CLion from scratch. ...
The blogger said : I have been writing a series o...
In actual projects, the database needs to be back...
Problem/failure/scenario/requirement The hard dis...
When you use HTML div blocks and the middle of th...
Edit docker-compose.yml and add the following con...
Customize a demo command The syntax of Vue custom...