iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >使用CSS3怎么实现一个切片式图片轮播效果
  • 854
分享到

使用CSS3怎么实现一个切片式图片轮播效果

2023-06-08 06:06:10 854人浏览 八月长安
摘要

使用css3怎么实现一个切片式图片轮播效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。CSS是什么意思css是一种用来表现html或XML等文件样式的计算机语

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

CSS是什么意思

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

轮播的构成

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">        <h4>            <span>Serendipity</span>            <span>What you've been dreaming of</span>        </h4>        <h4>            <span>Adventure</span>            <span>Where the fun begins</span>        </h4>        <h4>            <span>Nature</span>            <span>Unforgettable eperiences</span>        </h4>        <h4>            <span>Serenity</span>            <span>When silence touches nature</span>        </h4>    </div></section>

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;}


 

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网精选频道,感谢您对编程网的支持。

--结束END--

本文标题: 使用CSS3怎么实现一个切片式图片轮播效果

本文链接: https://www.lsjlt.com/news/252270.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c++中int和double有什么区别
    int 和 double 是 c++ 的数据类型,用于表示整数和浮点数。它们的关键区别在于:1. 范围:int 为整数,double 为浮点数且范围更大;2. 存储大小:int 占 4 ...
    99+
    2024-05-14
    c++ 隐式转换
  • C++ 多线程程序测试的挑战和策略
    多线程程序测试面临不可重复性、并发错误、死锁和缺乏可视性等挑战。策略包括:单元测试:针对每个线程编写单元测试,验证线程行为。多线程模拟:使用模拟框架在控制线程调度的情况下测试程序。数据竞...
    99+
    2024-05-14
    c++ 多线程
  • c++中深拷贝和浅拷贝的应用时间
    浅拷贝复制对象指针或引用,仅适用于不含动态分配内存或简单数据结构的对象;深拷贝复制实际数据,包括动态分配内存,适用于包含动态分配内存或复杂数据结构的对象。 浅拷贝和深拷贝的应用时间 在...
    99+
    2024-05-14
    c++
  • 探索用于 C++ 服务器架构的高级数据结构
    在 c++++ 服务器架构中,选择适当的高级数据结构至关重要。哈希表用于快速数据查找,树用于表示数据层次结构,图用于表示对象之间的关系。这些数据结构在实践中有着广泛的应用,例如缓存系统、...
    99+
    2024-05-14
    c++ 数据结构 社交网络 键值对
  • fixed在c++中的作用
    fixed 关键字在 c++ 中用于将浮点数存储为固定小数,提供更高精度,尤其适用于需要高精度的金融计算。fixed 将浮点数表示为具有固定小数位数的小数,默认情况下使用十进制表示法,小...
    99+
    2024-05-14
    c++
  • insert在c++中怎么用
    insert() 函数在 c++ 中用于在容器(如 vector、set)中插入元素,提供了一种动态调整容器大小并添加新元素的方法。它需要两个参数:要插入元素的位置 (pos) 和要插入...
    99+
    2024-05-14
    c++ 标准库
  • 如何使用 Golang 构建 RESTful API 并处理 JSON 响应?
    如何使用 golang 构建和处理 json 响应的 restful api步骤:创建 golang 项目并安装 gorilla mux。定义路由并处理 http 请求。安装 json ...
    99+
    2024-05-14
    golang git
  • c++中int和long的区别
    int 和 long 都是 c++ 中的整型类型,主要区别在于范围和存储空间:范围:int 为 32 位整数,范围为 [-2^31, 2^31-1];long 为 64 位整数,范围为 ...
    99+
    2024-05-14
    c++ 数据丢失
  • c++中int a(n)和int a[n]的区别
    int a(n)声明一个不可变的整型变量,而int a[n]声明一个可修改元素的整型数组,用于存储和处理数据序列或集合。 int a(n) 和 int a[n] 在 C++ 中的区别 ...
    99+
    2024-05-14
    c++
  • C++ 多线程编程中调试和故障排除的技术
    c++++ 多线程编程的调试技巧包括:使用数据竞争分析器检测读写冲突,并使用同步机制(如互斥锁)解决。使用线程调试工具检测死锁,并通过避免嵌套锁和使用死锁检测机制来解决。使用数据竞争分析...
    99+
    2024-05-14
    c++ 多线程 故障排除 同步机制
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作