css背景视差
In this tutorial, we are going to create a slideshow with a parallax effect with the help of some CSS3 properties. We’ll use radio buttons and sibling combinators for controlling which slide is shown. There will be two backgrounds and the idea is to change the background positions and the position of the slider with transitions in order to create a slight parallax effect.
在本教程中,我们将借助一些CSS3属性来创建具有视差效果的幻灯片。 我们将使用单选按钮和同级组合器来控制显示哪个幻灯片。 将有两个背景,其想法是更改背景位置和带有过渡的滑块位置,以产生轻微的视差效果。
The graphics used in the demo are by: 5Milli (Global Vector Map) and by WeGraphics (Free Vector Infographic Kit).
该演示中使用的图形是: 5Milli (全球矢量地图)和WeGraphics (免费矢量信息图表工具包)。
标记 (The Markup)
We will “connect” the input elements to the division with the class sp-content by using the general sibling combinator. For that we will leave the inputs on the same level like the sp-content div. When we click on an input we will change the background color and background position of it (grid pattern) and also the background-position of the sp-parallax-bg div (the world map) with transitions. The respective slide will be shown by moving the sp-slider ul to the right position. The markup looks as follows:
我们将使用通用的同级组合器将输入元素“连接”到类sp-content的除法上。 为此,我们将输入保持与sp-content div相同的级别。 单击输入时,我们将更改其背景颜色和背景位置(网格图案),以及带有过渡的sp-parallax-bg div(世界地图)的背景位置。 通过将sp滑块ul移到正确位置将显示相应的幻灯片。 标记如下所示:
<div class="sp-slideshow">
<input id="button-1" type="radio" name="radio-set" class="sp-selector-1" checked="checked" />
<label for="button-1" class="button-label-1"></label>
<input id="button-2" type="radio" name="radio-set" class="sp-selector-2" />
<label for="button-2" class="button-label-2"></label>
<input id="button-3" type="radio" name="radio-set" class="sp-selector-3" />
<label for="button-3" class="button-label-3"></label>
<input id="button-4" type="radio" name="radio-set" class="sp-selector-4" />
<label for="button-4" class="button-label-4"></label>
<input id="button-5" type="radio" name="radio-set" class="sp-selector-5" />
<label for="button-5" class="button-label-5"></label>
<label for="button-1" class="sp-arrow sp-a1"></label>
<label for="button-2" class="sp-arrow sp-a2"></label>
<label for="button-3" class="sp-arrow sp-a3"></label>
<label for="button-4" class="sp-arrow sp-a4"></label>
<label for="button-5" class="sp-arrow sp-a5"></label>
<div class="sp-content">
<div class="sp-parallax-bg"></div>
<ul class="sp-slider clearfix">
<li><img src="images/image1.png" alt="image01" /></li>
<li><img src="images/image2.png" alt="image02" /></li>
<li><img src="images/image3.png" alt="image03" /></li>
<li><img src="images/image4.png" alt="image04" /></li>
<li><img src="images/image5.png" alt="image05" /></li>
</ul>
</div><!-- sp-content -->
</div><!-- sp-slideshow -->
The list elements are the wrappers for each slide and although we are using simply images here, you can use any kind of content.
列表元素是每张幻灯片的包装,尽管我们在这里仅使用图像,但是您可以使用任何类型的内容。
CSS (The CSS)
We’ll set the width of the main container to 80% and set the width of the divisions with class sp-content and class sp-parallax-bg to 100%. The sp-content div will have a background color and a background image (grid) that we will move whenever we slide the slider ul. The sp-parallax-bg div will have a map as background image and we will also move the background position.
我们将主容器的宽度设置为80%,并将sp-content类和sp-parallax-bg类的分隔宽度设置为100%。 sp-content div将具有背景色和背景图像(网格),每当滑动滑块ul时我们将移动它。 sp-parallax-bg div将有一个地图作为背景图像,我们还将移动背景位置。
.sp-slideshow {
position: relative;
margin: 10px auto;
width: 80%;
max-width: 1000px;
min-width: 260px;
height: 460px;
border: 10px solid #fff;
border: 10px solid rgba(255,255,255,0.9);
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.sp-content {
background: #7d7f72 url(../images/grid.png) repeat scroll 0 0;
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.sp-parallax-bg {
background: url(../images/map.png) repeat-x scroll 0 0;
background-size: cover;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
The styles of the inputs and the labels:
输入和标签的样式:
.sp-slideshow input {
position: absolute;
bottom: 15px;
left: 50%;
width: 9px;
height: 9px;
z-index: 1001;
cursor: pointer;
opacity: 0;
}
.sp-slideshow input + label {
position: absolute;
bottom: 15px;
left: 50%;
width: 6px;
height: 6px;
display: block;
z-index: 1000;
border: 3px solid #fff;
border: 3px solid rgba(255,255,255,0.9);
border-radius: 50%;
transition: background-color linear 0.1s;
}
.sp-slideshow input:checked + label {
background-color: #fff;
background-color: rgba(255,255,255,0.9);
}
.sp-selector-1, .button-label-1 {
margin-left: -36px;
}
.sp-selector-2, .button-label-2 {
margin-left: -18px;
}
.sp-selector-4, .button-label-4 {
margin-left: 18px;
}
.sp-selector-5, .button-label-5 {
margin-left: 36px;
}
We’ve set the opacity of the inputs to 0 so that they are not visible. The labels are under the radio button and we will make it look like a little circle. All the inputs and labels will be positioned absolutely and we will place them next to each other by applying a left margin.
我们将输入的不透明度设置为0,以使它们不可见。 标签位于单选按钮下方,我们将使其看起来像一个小圆圈。 所有输入和标签都将绝对放置,并且我们将通过应用左侧边距将它们彼此相邻放置。
Next, we will style the arrows which are simply labels with the respective for attribute. Note, that clicking on a label to active an associated input might not work in mobile browsers. But anyway, you can navigate using the dots since we are actually clicking on the inputs.
接下来,我们将为箭头样式化,这些箭头只是带有各自for属性的标签。 请注意,在移动浏览器中,单击标签以激活关联的输入可能不起作用。 但是无论如何,由于我们实际上是在单击输入,因此您可以使用点进行导航。
The arrow labels have the following style:
箭头标签具有以下样式:
.sp-arrow {
position: absolute;
top: 50%;
width: 28px;
height: 38px;
margin-top: -19px;
display: none;
opacity: 0.8;
cursor: pointer;
z-index: 1000;
background: transparent url(../images/arrows.png) no-repeat;
transition: opacity linear 0.3s;
}
.sp-arrow:hover{
opacity: 1;
}
.sp-arrow:active{
margin-top: -18px;
}
Now, let’s define when each arrow is shown. On the first slide we, for example, don’t want to show the left arrow. And on the last slide we don’t want to show the right arrow:
现在,让我们定义何时显示每个箭头。 例如,在第一张幻灯片上,我们不想显示向左箭头。 在最后一张幻灯片上,我们不想显示向右箭头:
.sp-selector-1:checked ~ .sp-arrow.sp-a2,
.sp-selector-2:checked ~ .sp-arrow.sp-a3,
.sp-selector-3:checked ~ .sp-arrow.sp-a4,
.sp-selector-4:checked ~ .sp-arrow.sp-a5 {
right: 15px;
display: block;
background-position: top right;
}
.sp-selector-2:checked ~ .sp-arrow.sp-a1,
.sp-selector-3:checked ~ .sp-arrow.sp-a2,
.sp-selector-4:checked ~ .sp-arrow.sp-a3,
.sp-selector-5:checked ~ .sp-arrow.sp-a4 {
left: 15px;
display: block;
background-position: top left;
}
When an input is selected, the sp-content div will have a transition for the background-position and the background-color. The second transition is going to take a bit longer:
选择输入后, sp-content div将具有background-position和background-color的过渡。 第二个过渡将花费更长的时间:
.sp-slideshow input:checked ~ .sp-content {
transition: background-position linear 0.6s, background-color linear 0.8s;
}
The div with the world map (sp-parallax-bg) will also have a transition for the background-position:
具有世界地图(sp-parallax-bg)的div也将具有背景位置的过渡:
.sp-slideshow input:checked ~ .sp-content .sp-parallax-bg {
transition: background-position linear 0.7s;
}
In this way we can add a background parallax effect.
这样,我们可以添加背景视差效果。
Let’s define the changes to color and background-position for the sp-content div:
让我们为sp-content div定义颜色和背景位置的更改:
input.sp-selector-1:checked ~ .sp-content {
background-position: 0 0;
background-color: #727b7f;
}
input.sp-selector-2:checked ~ .sp-content {
background-position: -100px 0;
background-color: #7f7276;
}
input.sp-selector-3:checked ~ .sp-content {
background-position: -200px 0;
background-color: #737f72;
}
input.sp-selector-4:checked ~ .sp-content {
background-position: -300px 0;
background-color: #79727f;
}
input.sp-selector-5:checked ~ .sp-content {
background-position: -400px 0;
background-color: #7d7f72;
}
… and the sp-parallax-bg div:
…和sp-parallax-bg div:
input.sp-selector-1:checked ~ .sp-content .sp-parallax-bg {
background-position: 0 0;
}
input.sp-selector-2:checked ~ .sp-content .sp-parallax-bg {
background-position: -200px 0;
}
input.sp-selector-3:checked ~ .sp-content .sp-parallax-bg {
background-position: -400px 0;
}
input.sp-selector-4:checked ~ .sp-content .sp-parallax-bg {
background-position: -600px 0;
}
input.sp-selector-5:checked ~ .sp-content .sp-parallax-bg {
background-position: -800px 0;
}
The unordered list with the class sp-slider will have a width of 500%. This is because we have 5 slides. It will have a transition for the left value, that we will change depening on the input that is checked:
sp-slider类的无序列表的宽度为500%。 这是因为我们有5张幻灯片。 左值将有一个过渡,我们将更改所检查输入的分界:
.sp-slider {
position: relative;
left: 0;
width: 500%;
height: 100%;
list-style: none;
margin: 0;
padding: 0;
transition: left ease-in 0.8s;
}
Each list element is a slide and it will also have a transition for the opacity. We will give both, the slide and the image inside the box-sizing property of “border-box”. This will allow us to set a padding but also use 100% values for heights and widths and not worry about any overflow:
每个列表元素都是一张幻灯片,并且还将具有不透明度的过渡。 我们将在“边框”的box-sizing属性中同时提供幻灯片和图像。 这将允许我们设置填充,但也可以使用100%的高度和宽度值,而不必担心任何溢出:
.sp-slider > li {
color: #fff;
width: 20%;
box-sizing: border-box;
height: 100%;
padding: 0 60px;
float: left;
text-align: center;
opacity: 0.4;
transition: opacity ease-in 0.4s 0.8s;
}
.sp-slider > li img{
box-sizing: border-box;
display: block;
margin: 0 auto;
padding: 40px 0 50px 0;
max-height: 100%;
max-width: 100%;
}
Now we need to set the correct left values for each selected slide:
现在我们需要为每个选定的幻灯片设置正确的左值:
input.sp-selector-1:checked ~ .sp-content .sp-slider {
left: 0;
}
input.sp-selector-2:checked ~ .sp-content .sp-slider {
left: -100%;
}
input.sp-selector-3:checked ~ .sp-content .sp-slider {
left: -200%;
}
input.sp-selector-4:checked ~ .sp-content .sp-slider {
left: -300%;
}
input.sp-selector-5:checked ~ .sp-content .sp-slider {
left: -400%;
}
Each current slide will then get opacity 1:
每个当前幻灯片将变得不透明1:
input.sp-selector-1:checked ~ .sp-content .sp-slider > li:first-child,
input.sp-selector-2:checked ~ .sp-content .sp-slider > li:nth-child(2),
input.sp-selector-3:checked ~ .sp-content .sp-slider > li:nth-child(3),
input.sp-selector-4:checked ~ .sp-content .sp-slider > li:nth-child(4),
input.sp-selector-5:checked ~ .sp-content .sp-slider > li:nth-child(5){
opacity: 1;
}
That’s all, hope you like it!
仅此而已,希望您喜欢!
翻译自: https://tympanus.net/codrops/2012/04/30/fluid-css3-slideshow-with-parallax-effect/
css背景视差
1万+




被折叠的 条评论
为什么被折叠?



