Home >Web Front-end >H5 Tutorial >Use html5 css3 to achieve slider switching effect Say goodbye to javascript css_html5 tutorial skills

Use html5 css3 to achieve slider switching effect Say goodbye to javascript css_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:50:221557browse

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