Home >Web Front-end >HTML Tutorial >HTML5 and CSS3 realize the switching effect of smart animation
This article will share with you a demo based on HTML5 CSS3 to achieve smart animated TAB switching effect. It is very good and has reference value. Friends who need it can refer to it.
The designer gave a rendering of tab switching. . Although it is a small function, front-end engineers still have many details to pay attention to when implementing it. I wrote a demo for your reference.
The final effect is as follows:
In order for the gif animation to show details, I extended the animation time to 3 seconds
Implementation ideas
Interval vertical lines, because they are not vertical, so borders cannot be used. I'm going to use pseudo-elements.
There are only 3 vertical bars, but there are 4 li s. This is simple and can be selected using the :not(:first-child) selector.
The switching background color changes, because I want to have an effect from small to large, so I can’t directly use the background color to achieve it. I am also going to use pseudo elements to achieve it.
If the size of the pseudo element is used to control it, the calculation will be more complicated, so I want to use box-shadow shadow to achieve it.
Okay, that’s it. Let’s start writing the code, as follows:
HTML code
<p class="m"> <ul class="tab"> <li><a href="">导航1</a></li> <li><a href="">导航2</a></li> <li><a href="">导航3</a></li> <li><a href="">导航4</a></li> </ul> </p>
The above code structure has been written before. I think it is OK, so I won’t make any adjustments. There is no cumbersome code.
CSS code
.m { margin: 100px; } .tab { width: 400px; margin: 0 auto; border: 1px solid #ddd; height: 40px; text-align: center; line-height: 40px; background: #fff; border-radius: 10px; overflow: hidden; } .tab li { float: left; width: 100px; position: relative; overflow: hidden; } .tab li:before, .tab li:after, .tab li a { -webkit-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; } .tab li:before, .tab li:after { content: ""; display: block; } .tab li:not(:first-child):after { background: #ddd; height: 20px; width: 1px; left: 0; top: 10px; position: absolute; } .tab li a { display: block; position: relative; z-index: 2; color: #000; font-size: 14px; } .tab li:before { width: 0; height: 0; top: 50%; left: 50%; z-index: 1; position: absolute; } .tab li:hover a { color: #fff; } .tab li:hover:before { box-shadow: 0 0 0 100px #36bc99; } .tab li:hover + li:after, .tab li:hover:after { height: 0; top: 20px; }
Code analysis:
Animation implementation It's very simple, just use the transition attribute.
To control your own pseudo-elements and the pseudo-elements of the next sibling element, just use the selector.
Other codes are relatively clear and simple, you can analyze them yourself.
It is very simple to achieve this effect. The focus is on daily accumulation and flexible matching of various parameters. Thinking of the implementation method and finally writing the code is very fast. And there is no high point of knowledge in it.
The reason why CSS is difficult is not that you don’t know how to do it, but that you don’t know how to match it.
Actually, only 99% of the design effect is restored. One of the two lines is inside the background and the other is outside the background. What should I do if I want to put the two dividing lines inside the background? What about implementation? You can think about it.
Let’s use scss. The css above is compiled. In fact, it is very convenient and fast to implement it with scss, and the code is more readable.
The demonstration is as follows:
.m { margin: 100px; } .tab { width: 400px;margin: 0 auto;border: 1px solid $cdd;height: 40px;text-align: center;line-height: 40px; background: $cff;border-radius: 10px;overflow: hidden; li { float: left;width: 100px;position: relative;overflow: hidden; &:before,&:after,a {@include dz();} &:before,&:after { content: "";display: block; } &:not(:first-child) { &:after { background: $cdd;height: 20px;width: 1px;left: 0;top: 10px;position: absolute; } } a { display: block;position: relative;z-index: 2;color: $c00;font-size: 14px; } &:before { width: 0;height: 0;top: 50%;left: 50%;z-index: 1;position: absolute; } &:hover { a {color: $cff;} &:before { box-shadow: 0 0 0 100px $cyan; } & + li:after,&:after { height: 0;top: 20px; } } } }
Of course, I used color variables and mixin code in this code. You cannot use it directly.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use CSS3 linear gradient linear-gradient to create a border
How to hide the scroll bar in CSS
The above is the detailed content of HTML5 and CSS3 realize the switching effect of smart animation. For more information, please follow other related articles on the PHP Chinese website!