search
HomeWeb Front-endH5 TutorialUse html5 css3 to achieve slider switching effect Say goodbye to javascript css_html5 tutorial skills

Well, last time I said I would make up for a few articles as soon as possible, but I calmly missed the appointment. It’s been almost a month since I posted an article, which is really depressing. I find that I can't spare any time recently. I am basically arranged one project after another. I either can't find a suitable topic when I'm free, or I can't spare the time when I have a tangled topic. So I decided to summarize the knowledge points on the problems I have struggled with for a period of time, study them one by one in depth when I have free time, and then organize them into articles and share them.

Let’s get to the point. When it comes to slider, in the past, CSS and JS have been used to achieve related switching effects. I have heard that everyone has been discussing the implementation of using html5 css3, but I have never implemented it myself. Ok, this time I have time to play with css3. In fact, I was also attracted by a message on Weibo. I saw the amazing results achieved by others, and then I had the urge to do it myself.
1. Rendering

It doesn’t look much different from the effect achieved with js in the past, but the overall feeling is very elegant. Well, the power of CSS3 is that I can achieve relatively complex effects by writing very little code. However, this example is also not perfect. When switching between two pictures, if there is a picture in the middle, it will still be seen during the execution of the CSS3 animation, which is relatively ineffective. But think about it, this is an effect achieved by pure css3. The complex html structure changes implemented with js cannot be seen here, so the above effect is difficult to achieve simply with css3.
2. HTML structure >





















The above code is the main html structure, which contains an input radio group. You can see it as a hub here. In this example, it plays a key role (this is why I don’t want to Hide it, the real hero should not be the one behind the scenes).
The sliders below contain the images that need to be displayed. It seems to be a sliding door effect. Different images can be displayed by controlling the margin-left of the inner.

Controls are the switching arrows on the left and right sides of the picture. Don’t worry about why we need to design 5 of them. It seems that only two are enough. As a reminder, we will never use js to implement switching in this example.
The last active is the small click button under the picture. You can directly select the picture you want to browse by clicking it. You can also enrich the structure inside to design a thumbnail effect.
3. css style sheet

Copy code
The code is as follows:

@charset utf-8;
/* common */
body{background: #ddd;overflow-x: hidden;}
#bd{width: 960px;margin: 100px auto ;max-width: 960px;}
/* module: sliders */
#sliders{
border-radius: 5px;
box-shadow: 1px 1px 4px #666;
padding : 1%;
background: #fff;
}
#overflow{
width: 100%;
overflow: hidden;
}
#sliders .inner{
width: 500%;
transiton: all 1s linear;
-webkit-transition: all 1s linear;
}
#sliders article{
float: left;
width : 20%;
}
#sliders article .info{
position: absolute;
opacity: 0;
padding: 30px;
color: #666;
font -family: Arial;
transition: opacity 0.1s ease-out;
-webkit-transform: translateZ(0);
-webkit-transition: opacity 0.1s ease-out;
}
#sliders article .info h1{
font-size: 22px;
font-weight: bold;
margin: 0 0 5px;
}
#sliders article .info a{
color: #666;
text-decoration: none;
}
/* module: controls */
#controls{
height: 50px;
width: 100 %;
margin-top: -25%;
}
#controls label{
display: none;
width: 50px;
height: 50px;
opacity: 0.3;
cursor: pointer;
}
#controls label:hover{
opacity: 1;
}
/* module: active */
#active{
width: 100%;
margin-top: 23%;
text-align: center;
}
#active label{
display: inline-block;
width : 10px;
height: 10px;
border-radius: 5px;
background: #bbb;
border-color: #777;
}
#active label:hover{
background: #ccc;
}
/* input checked change style */
#slider1:checked ~ #active label:nth-child(1),
#slider2:checked ~ #active label:nth-child(2),
#slider3:checked ~ #active label:nth-child(3),
#slider4:checked ~ #active label:nth-child(4),
#slider5:checked ~ #active label:nth-child(5){
background: #333;
}
#slider1:checked ~ #controls label:nth-child(5),
#slider2:checked ~ #controls label:nth-child(1),
#slider3:checked ~ #controls label:nth-child(2),
#slider4:checked ~ #controls label:nth -child(3),
#slider5:checked ~ #controls label:nth-child(4){
display: block;
float: left;
background: url(../img /prev.png) no-repeat;
margin-left: -70px;
}
#slider1:checked ~ #controls label:nth-child(2),
#slider2:checked ~ #controls label:nth-child(3),
#slider3:checked ~ #controls label:nth-child(4),
#slider4:checked ~ #controls label:nth-child(5),
#slider5:checked ~ #controls label:nth-child(1){
display: block;
float: right;
background: url(../img/next.png) no- repeat;
margin-right: -70px;
}
#slider1:checked ~ #sliders article:nth-child(1) .info,
#slider2:checked ~ #sliders article:nth -child(2) .info,
#slider3:checked ~ #sliders article:nth-child(3) .info,
#slider4:checked ~ #sliders article:nth-child(4) .info,
#slider5:checked ~ #sliders article:nth-child(5) .info{
opacity: 1;
transition: all 0.6s ease-out 1s;
-webkit-transition: all 0.6s ease-out 1s;
}
#slider1:checked ~ #sliders .inner{
margin-left: 0;
}
#slider2:checked ~ #sliders .inner{
margin-left: -100%;
}
#slider3:checked ~ #sliders .inner{
margin-left: -200%;
}
#slider4:checked ~ #sliders .inner{
margin-left: -300%;
}
#slider5:checked ~ #sliders .inner{
margin-left: -400%;
}

Okay, I admit that the above CSS code is really a lot and complicated, but it really achieves a very cool effect, and when I finished writing it, I was impressed by the huge magic of CSS3. . .
The first half of the code here is mainly used to design the structure of the slider, including some beautifying designs in terms of rounded corners and shadows. The second half is mainly some animation effects to achieve some dynamic effects when switching pictures or switching control buttons. However, the most important thing is the use of the css3 selector at the bottom, through which the image switching function is truly realized. I really think the selector plays a very, very important role in the example, because this is something I ignored when learning CSS3 in the past. I have always felt that the power of CSS3 is rounded corners, shadows, deformation, and animation, but this code really tells us how important selectors are in CSS3. In some complex logic, using these CSS3 selectors can achieve unimaginable effects.
4. Principles of slider implementation
When you read the above code for the first time, you must be like me at first, not believing that such code can achieve the effect of slider.
Okay, let me analyze the implementation principle.
I said above that the top radio group is very important and is the hub of slider implementation. Yes, it really is.
To implement a slider, it is necessary to realize two kinds of switching, that is, when the control button is clicked, the picture switches; at the same time, when the picture switches, all the control buttons must be displayed correctly.
In this example we use label as the control button, article contains the image, and inner serves as the container for the image.
To put it simply, there is no way to establish a connection between label and article, and it is difficult for label status information to be reflected in the selection of article. Unless there is something that can record the switching status of the label, and then use some means to select pictures in the corresponding order to display.
Okay, now, you understand why the radio group is the key to implementing the slider. Yes, it appears to record the click status of the label.
We use the for attribute of the label to associate it with the corresponding radio. When the label is clicked, the corresponding radio becomes checked. Then move the inner to the left through the powerful CSS3 selector to display the corresponding image. Of course, the corresponding left and right selection buttons are also displayed through the selector. In the same way, when the left and right buttons are clicked, the status of the five selection buttons below is also implemented in this way.
The above implementation principle is relatively simple. In fact, as long as the click status of the control button can be recorded, the slider effect can be achieved through the selector.
Not only the radio group can be used, a:hover can also follow this idea to achieve picture switching when a is hovering. Of course, there are many other ways to implement it, as long as you understand the principle of implementation.
5. Summary
Actually, CSS3 is really fun, and there are many effects that can’t be imagined in CSS3. Sometimes I really find that writing CSS3 requires a little cleverness, and sometimes some exquisite implementation methods are really amazing.
Well, as a small practice, this example has gained me a lot, especially the powerful selector, which makes me ashamed. I ignored it too much in the past. . .
I still need to consider the issue of discontinuous image switching. It seems that I need to use some js to assist.
Okay, I will share the results when I have them.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML5: The Building Blocks of the Modern Web (H5)HTML5: The Building Blocks of the Modern Web (H5)Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

H5 Code: Writing Clean and Efficient HTML5H5 Code: Writing Clean and Efficient HTML5Apr 20, 2025 am 12:06 AM

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment