The reason for writing this article
- In the previous article, I mentioned how to animate display transitions, but I didn’t go into detail about how this works.
- In order to better let people want to learn in-depth understanding so overtime wrote this “short”, I think later or short, otherwise we look too tired
The official start of the
- Initialization interface
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, Word-wrap: break-word! Important; "> <title>Document</title> <style> #app {width: 200px; height: 200px; background-color: red; display: none; transition: all 1s; } < / style > < / head > < body > < div id = "app" > < / div > < button id = "test" > test < / button > < / body > < / HTML >Copy the code
- At this point I initialize the app’s display to None and write to the script file
<style> #app { width: 200px; height: 200px; background-color: red; display: none; } </style>... <script> test.onclick = function () { const app = document.querySelector('#app') console.log(app, 'app') app.style.transform = "translateX(200px)" app.style.display = "block" } </script>Copy the code
- The initialization screen looks like this:
- At this point, I click the test button
- There is no animation, it is very stiff, there are some scenes THAT I need performance, such as initialization without rendering, but when it is animated, it is possible to use this line of code
test.onclick = function () {
const app = document.querySelector('#app')
console.log(app, 'app')
app.style.display = "block"
const height = app.offsetHeight
app.style.transform = "translateX(200px)"
}
Copy the code
- When I add const height = app.offsetheight and click on the test button, the display toggle comes up with “animation” and it’s overdone
- Why is there animation? Because when I read these special properties of the DOM, the browser forces me to empty the render queue once, so I can get the latest values. So when I read it, it’s already a block display, so. We have a transition animation
The effect is as follows:
appear"Transition Animation"
What’s going on?
- Actually,
display
You can’t animate it, so the title+ the quotes
- How can there be a transition?
- There are numerical variations, such as transparency, from
0-1
. - Initialization has a render display
- in
transition
Properties contained in it - . I’ll let you add to that
- There are numerical variations, such as transparency, from
Why is it that when you add a line of code, you get an animation?
- One of the most common problems people encounter when writing modern front-end frameworks is the uncertainty of rendering time.
- For example,
vue
The inside of thenextTick
Implementation, there’s a gracefully degraded implementation. It’s inmounted
To get it from the life cycle functiondom
Nodes are often unavailable or not fully rendereddom
Node. I haven’t used it for a long timevue
Why? - As with today’s data-driven frameworks, whenever the data changes, the corresponding logic binds the data
dom
Nodes should be updated, but the timing of the update is uncertain, because there are intermediate layers, such as existencediff
There may be queues, because when you change data frequently, the framework itself has to be optimized to merge data updates over a period of time and then actually update itdom
, and so on these things are done, to updatedom
Node, and then we can see the node corresponding to the latest data - When we do go to update
dom
Node, there is also a queue. This is theThe browser's render queue
- For example,
- If you can’t understand what I said above, you can read my handwriting earlier
React
In this series of articlessetState
Asynchronous queue implementation
The browser’s render queue
- When is the queue most useful?
- Frequent direct operation
dom
When, for examplefor
Frequent operations inside the loopdom
At this point, the browser will optimize our operations and merge some operations into one - Render queue heel
display
The associated
- Frequent direct operation
<script>
test.onclick = function () {
const app = document.querySelector('#app')
console.log(app, 'app')
app.style.display = "block"
const height = app.offsetHeight
app.style.transform = "translateX(200px)"
}
</script>
Copy the code
- When we execute
app.style.display = "block"
And when I do that,dom
The node is not updated at this point,js
The parsing engine is smart enough to see that you have code to change right after youdom
Nodes are first queued for centralized one-time operations - When we execute
app.offsetHeight
This line of code, we find that we need to readdom
The browser is afraid that operations that are not currently performed in the queue will cause you to read an incorrect valueBUG
The render queue will then be emptied and executed, allowing you to get the maximumAccurate/new
value
- Ask the browser for something when you request it
style
When the message is sent to the browserflush
Queues, such as:offsetTop, offsetLeft, offsetWidth, offsetHeight
scrollTop/Left/Width/Height
clientTop/Left/Width/Height
width,height
- When you request some of the above attributes, the browser needs to give you the most accurate values
flush
The queue, - Because there may be operations in the queue that affect these values. Even if you get information about the layout and style of an element regardless of the most recent or changed layout,
- The browser forces the render queue to refresh.
After clearing the render queue
- When reading
offsetHeight
Property after we empty the render queue, then at this timedom
After re-rendering is complete at this pointdisplay
Is alreadyblock
. And it’s displayed on the interface, so let’s do it againdom
The transition animation will appear on the property.
The last
- Paper come zhongjue shallow, more practice, more thinking is the only way to a higher level, want to see my previous handwritten source article, my gitHub source address is:
https://github.com/JinJieTan/Peter-
Remember,Star
oh
- Welcome to follow my public account:
[Front-end peak]
If you think it’ll help, you can order itpraise
/Looking at the
Let more people see my article