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!
HTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AMThe roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.
HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AMHTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.
HTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AMHTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.
From Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AMHTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.
Understanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AMWebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
The Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AMThe role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.
HTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AMHTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract
HTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AMHTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools






