侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

CSS3实现炫酷的切片式图片轮播效果

前端  /  管理员 发布于 7年前   190

今天我们学习如何使用CSS创建一个炫酷的图片轮播组件。它的原理简单的说就是通过单击标签元素(label)来切换背景图像和动画效果。核心是使用与标签关联的单选按钮和使用通用兄弟选择器来定位每张图片。

我们要实现的最终效果是这样的:

轮播的构成

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

 

<section class="cr-container">    <!-- 单选按钮和label标签 -->    <input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1" checked/>    <label for="select-img-1" class="cr-label-img-1">1</label>    <input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2" />    <label for="select-img-2" class="cr-label-img-2">2</label>    <input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3" />    <label for="select-img-3" class="cr-label-img-3">3</label>    <input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4" />    <label for="select-img-4" class="cr-label-img-4">4</label>    <div class="clr"></div>        <!-- 面板 -->    <div class="cr-bgimg">        <div><span>Slice 1 - Image 1</span><span>Slice 1 - Image 2</span><span>Slice 1 - Image 3</span><span>Slice 1 - Image 4</span>        </div>        <div><span>Slice 2 - Image 1</span><span>Slice 2 - Image 2</span><span>Slice 2 - Image 3</span><span>Slice 2 - Image 4</span>        </div>        <div><span>Slice 3 - Image 1</span><span>Slice 3 - Image 2</span><span>Slice 3 - Image 3</span><span>Slice 3 - Image 4</span>        </div>        <div><span>Slice 4 - Image 1</span><span>Slice 4 - Image 2</span><span>Slice 4 - Image 3</span><span>Slice 4 - Image 4</span>        </div>    </div>    <!-- 标题 -->    <div class="cr-titles">        <h3><span>Serendipity</span><span>What you've been dreaming of</span>        </h3>        <h3><span>Adventure</span><span>Where the fun begins</span>        </h3>        <h3><span>Nature</span><span>Unforgettable eperiences</span>        </h3>        <h3><span>Serenity</span><span>When silence touches nature</span>        </h3>    </div></section>

h3元素中包含两个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;}

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

 

.cr-titles h3{    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 h3 span:nth-child(1){    font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;    font-size: 70px;    display: block;    letter-spacing: 7px;}.cr-titles h3 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 h3:nth-child(1),.cr-container input.cr-selector-img-2:checked ~ .cr-titles h3:nth-child(2),.cr-container input.cr-selector-img-3:checked ~ .cr-titles h3:nth-child(3),.cr-container input.cr-selector-img-4:checked ~ .cr-titles h3:nth-child(4){    opacity: 1;}

 

就是这样!切片式的轮播效果就这样实现了。当然通过这个效果我们还可以延伸出更多的切片效果,例如:

效果二


 

效果三

效果四

总结

以上所述是小编给大家介绍的CSS3实现炫酷的切片式图片轮播效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


  • 上一条:
    CSS 响应式布局系统的实例代码
    下一条:
    CSS scroll-snap滚动事件停止及元素位置检测实现
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客