网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

使用CSS3怎么实现一个切片式图片轮播效果-创新互联

使用CSS3怎么实现一个切片式图片轮播效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联建站是一家专业提供喀什企业网站建设,专注与网站制作、成都网站制作、H5网站设计、小程序制作等业务。10年已为喀什众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

css是什么意思

css是一种用来表现HTML或XML等文件样式的计算机语言,主要是用来设计网页的样式,使网页更加美化。它也是一种定义样式结构如字体、颜色、位置等的语言,并且css样式可以直接存储于HTML网页或者单独的样式单文件中,而样式规则的优先级由css根据这个层次结构决定,从而实现级联效果,发展至今,css不仅能装饰网页,也可以配合各种脚本对于网页进行格式化。

轮播的构成


HTML由三个主要部分组成:单选按钮和标签、包含四个面板的容器及其每张图片的“切片”以及标题。设置为class="cr-bgimg"的容器将为每个图片划分四个面板,其中,在每个面板里通过background-position属性将背景图片定位在合适的位置上。所以,第一个面板将有四个切片,每个切片都有一个图片作为背景,并且位于容器最左边的位置。第二个面板也有四个切片,它位于第一个面板的右侧,用于显示图片的第二个切片部分:

            
    
    
    1
    
    2
    
    3
    
    4
    
                       
            Slice 1 - Image 1             Slice 1 - Image 2             Slice 1 - Image 3             Slice 1 - Image 4         
        
            Slice 2 - Image 1             Slice 2 - Image 2             Slice 2 - Image 3             Slice 2 - Image 4         
        
            Slice 3 - Image 1             Slice 3 - Image 2             Slice 3 - Image 3             Slice 3 - Image 4         
        
            Slice 4 - Image 1             Slice 4 - Image 2             Slice 4 - Image 3             Slice 4 - Image 4         
    
                  

            Serendipity             What you've been dreaming of         

        

            Adventure             Where the fun begins         

        

            Nature             Unforgettable eperiences         

        

            Serenity             When silence touches nature         

    

h4元素中包含两个span元素,一个是主标题,一个是子标题。

CSS

为了代码的简洁,文章的CSS中都省略了浏览器前缀,但是你获取到的代码是包含完整的。

我们的第一个目标是实现点击label元素的时候触发对应的单选按钮,方法很简单,就是将label元素的for属性值与单选按钮的ID值对应起来就可以了。

第二步,我们要将所有的背景图片定位在正确的位置上。第三步,在单击label标签时,显示对应的图片切片和标题。

我们一步一步看,首先设置最外层section元素的样式,给它设置一个淡淡的阴影效果。

.cr-container{
    width: 600px;
    height: 400px;
    position: relative;
    margin: 0 auto;
    border: 20px solid #fff;
    box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}

因为后面我们要使用通用兄弟选择器“~”来选中对应的图片切片和标题,于是将所有的label元素放在代码的最上面。通过设置z-index属性确保label元素显示在图片和文字的上面,并且通过margin-top属性使它们距离整体上边框350px。

.cr-container label{
    font-style: italic;
    width: 150px;
    height: 30px;
    cursor: pointer;
    color: #fff;
    line-height: 32px;
    font-size: 24px;
    float:left;
    position: relative;
    margin-top: 350px;
    z-index: 1000;
}

接着,添加一个小圆圈来美化label元素,我们创建一个伪元素,并将其放在文字的中心位置。

.cr-container label:before{
    content:'';
    width: 34px;
    height: 34px;
    background: rgba(130,195,217,0.9);
    position: absolute;
    left: 50%;
    margin-left: -17px;
    border-radius: 50%;
    box-shadow: 0px 0px 0px 4px rgba(255,255,255,0.3);
    z-index:-1;
}

为了在面板与面板之间创建一个分隔线,我们使用label元素的另一个伪元素,并通过渐变背景将它从图片的上边缘延伸到下边缘。

.cr-container label:after{
    width: 1px;
    height: 400px;
    content: '';
    background: linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
    position: absolute;
    bottom: -20px;
    right: 0px;
}

最后一个面板的右侧不应该有分隔线,所以我们将它的宽度设置为0。

.cr-container label.cr-label-img-4:after{
    width: 0px;
}

现在,label标签的样式就完成了,我们可以将所有的单选按钮隐藏掉。

.cr-container input{
    display: none;
}

当我们点击一个label元素时,它对应的单选按钮就是被选中。反过来,就可以使用通用兄弟选择器来选择选中按钮对应的label元素,并改变它的文字颜色。

.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1,
.cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2,
.cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3,
.cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4{
    color: #68abc2;
}

我们还可以改变小圆圈的颜色并添加一个阴影效果。

.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1:before,
.cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2:before,
.cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3:before,
.cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4:before{
    background: #fff;
    box-shadow: 0px 0px 0px 4px rgba(104,171,194,0.6);
}

图片的容器将占据所有的宽度,并且绝对定位。稍后使用这个容器将背景图片设置为当前选定的图片。我们还需要一张默认可见的图片,因此,再添加一些背景属性:

.cr-bgimg{
    width: 600px;
    height: 400px;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: 0 0;
}

因为我们有四个面板,每个面板的宽度为150像素(600除以4)。面板设置为左浮动后会并排显示,同时设置它们溢出隐藏,因为我们不希望在滑动时看到切片出来:

.cr-bgimg div{
    width: 150px;
    height: 100%;
    position: relative;
    float: left;
    overflow: hidden;
    background-repeat: no-repeat;
}

每个切片也被设置为绝对定位,并且通过left:-150px将它们显示在面板的外面并隐藏起来:

.cr-bgimg div span{
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: -150px;
    z-index: 2;
    text-indent: -9000px;
}

再来,让我们来处理容器和每个切片的背景图片:

.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(1){
    background-image: url(../images/1.jpg);
}
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(2){
    background-image: url(../images/2.jpg);
}
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(3){
    background-image: url(../images/3.jpg);
}
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(4){
    background-image: url(../images/4.jpg);
}

我们还需要根据不同的面板为切片的背景图片提供不同的定位:

.cr-bgimg div:nth-child(1) span{
    background-position: 0px 0px;
}
.cr-bgimg div:nth-child(2) span{
    background-position: -150px 0px;
}
.cr-bgimg div:nth-child(3) span{
    background-position: -300px 0px;
}
.cr-bgimg div:nth-child(4) span{
    background-position: -450px 0px;
}

当我们单击一个label标签时,我们将所有的切片滑到右边:

.cr-container input:checked ~ .cr-bgimg div span{
    animation: slideOut 0.6s ease-in-out;
}
@keyframes slideOut{
    0%{
        left: 0px;
    }
    100%{
        left: 150px;
    }
}

但是除了我们选中的这个label标签,它对应的图片切片是从-150px滑到0px:

.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg div span:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg div span:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg div span:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg div span:nth-child(4)
{
    transition: left 0.5s ease-in-out;
    animation: none;
    left: 0px;
    z-index: 10;
}

最后,设置h4标签中主副标题的样式,当我们点击了某个label标签后,它对应的标题的透明度会从0过渡到1:

.cr-titles h4{
    position: absolute;
    width: 100%;
    text-align: center;
    top: 50%;
    z-index: 10000;
    opacity: 0;
    color: #fff;
    text-shadow: 1px 1px 1px rgba(0,0,0,0.1);
    transition: opacity 0.8s ease-in-out;
}
.cr-titles h4 span:nth-child(1){
    font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
    font-size: 70px;
    display: block;
    letter-spacing: 7px;
}
.cr-titles h4 span:nth-child(2){
    letter-spacing: 0px;
    display: block;
    background: rgba(104,171,194,0.9);
    font-size: 14px;
    padding: 10px;
    font-style: italic;
    font-family: Cambria, Palatino, "Palatino Linotype", "Palatino LT STD", Georgia, serif;
}
.cr-container input.cr-selector-img-1:checked ~ .cr-titles h4:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-titles h4:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-titles h4:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-titles h4:nth-child(4){
    opacity: 1;
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


网站题目:使用CSS3怎么实现一个切片式图片轮播效果-创新互联
链接地址:http://bjjierui.cn/article/ccghog.html

其他资讯