Xiaobai is not very familiar with js, so I would like to ask, if I use js to click a button, and then p moves to a position of 500px on the right after 5 seconds, what is the idea of making this effect?
Should I write a class first to achieve all these effects, and then use js to switch the className?
But in the click event, write all the css effects? QUQ
I’ll give you a simple demo and you’ll know it. This is just one of the methods, there are many more!
Is this what you want? See the code below. p moves to 500px in 5s. You can set the transition effect for p first and add a class that moves to the right. What js mainly does is to add a class to the element
ps: It’s just a demo code, not considering compatibility, etc...
css3
You can use css or write directly in js. For css, use transition to write it. For Baidu specifically, when a click event occurs, add a class to the element and change the style. For js, use settimeout, take the left margin you mentioned as an example, change the left margin bit by bit, and stop when it reaches the specified value, which is cleartimeout
As mentioned above, if it is ie9 or below, you need to use a timer
Usually css3’s
transition
is used to achieve the transition effect. If you want animation, useanimation
. Using js is more performance-intensive. Here are some reference articlescss3d properties