美文网首页
纯css3:radio+label实现轮播图

纯css3:radio+label实现轮播图

作者: 岳晓亮 | 来源:发表于2018-08-15 01:13 被阅读0次
纯css3:radio+label实现轮播图

预览地址:纯css3:radio+label实现轮播图

轮播图这东西,我相信只要是做前端的,肯定都做过,不过大部分应该都是用js来实现的,其实css3也是可以实现轮播图的,而且也可以加一些动画之类的,下边就分享下思路。声明,这里没有贬低js的意思,相反,js能做的,css基本做不到,css能做的,js基本能做到,这里纯粹就是为了分享css的一些运用。

css里有个可以绑定到<input>上的标签<label>,它的功能就是当你点击所绑定的这个<label>的时候,就可以选中相应的<input>,既然有了这个功能,那就有了控制轮播图的方法了。

HTML代码

<div id="wrap">
    <!-- 用来控制对应的轮播块 -->
    <input type="radio" name="banner" id="banner_bt1" checked />
    <input type="radio" name="banner" id="banner_bt2" />
    <input type="radio" name="banner" id="banner_bt3" />
    <ul class="banner">
        <li>
            <h2>这是第一个标题</h2>
            <p>这是第一段话...</p>
        </li>
        <li>
            <h2>这是第二个标题</h2>
            <p>这是第二段话...</p>
        </li>
        <li>
            <h2>这是第三个标题</h2>
            <p>这是第三段话...</p>
        </li>
    </ul>
    <!-- 左右切换按钮 -->
    <div class="bn_arrows">
        <label for="banner_bt1"></label>
        <label for="banner_bt2"></label>
        <label for="banner_bt3"></label>
    </div>
    <!-- 索引按钮 -->
    <div class="bn_index">
        <label for="banner_bt1"></label>
        <label for="banner_bt2"></label>
        <label for="banner_bt3"></label>
    </div>
</div>

CSS代码

* {
    padding: 0;
    margin: 0;
}
ul{
    list-style: none;
}
input{
    display: none;
}
#wrap{
    width: 1200px;
    height: 375px;
    margin: 100px auto;
    background: red;
    position: relative;
    overflow: hidden;
}
.banner{
    height: 100%;
    position: relative;
    top: 0;
    left: 0;
    white-space: nowrap;
    font-size: 0;
    transform: translateX(-100%);
    transition: transform .3s;
}
.banner li{
    display: inline-block;
    width: 1200px;
    height: 100%;
    font-size: 20px;
    color: #fff;
    position: relative;
}
.banner li:nth-of-type(1){
    background-color: #3c3c3c
}
.banner li:nth-of-type(2){
    background-color: green
}
.banner li:nth-of-type(3){
    background-color: blue
}
.bn_arrows label{
    display: inline-block;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-radius: 50%;
    background: url(img/prev.png) no-repeat center rgba(255,255,255,.1);
    background-size:auto 50%;
    color: #fff;
    transition: background-color .2s;
    font-size: 16px;
    position: absolute;
    top: 50%;
    margin-top: -25px;
    cursor: pointer;
    display: none;
}
.bn_arrows label:hover{
    background-color: rgba(255,255,255,.5);
}

.bn_index{
    width: 100%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    position: absolute;
    left: 0;
    bottom: 10px;
    z-index: 99;
}
.bn_index label{
    display: inline-block;
    width: 36px;
    height: 6px;
    background: rgba(255,255,255,.7);
    margin: 0 10px;
    cursor: pointer;
}
/* 绑定事件 */
/* 控制轮播块 */
input:nth-of-type(1):checked ~ .banner{
    transform: translateX(0);
}
input:nth-of-type(2):checked ~ .banner{
    transform: translateX(-100%);
}
input:nth-of-type(3):checked ~ .banner{
    transform: translateX(-200%);
}
/* 控制索引按钮 */
input:nth-of-type(1):checked ~ .bn_index label:nth-of-type(1),
input:nth-of-type(2):checked ~ .bn_index label:nth-of-type(2),
input:nth-of-type(3):checked ~ .bn_index label:nth-of-type(3){
    background: #fff;
}
/* 向右切换 */
input:nth-of-type(1):checked ~ .bn_arrows label:nth-of-type(2),
input:nth-of-type(2):checked ~ .bn_arrows label:nth-of-type(3){
    display: block;
    right: 10px;
    background-image: url(img/next.png);
}
/* 向左切换 */
input:nth-of-type(2):checked ~ .bn_arrows label:nth-of-type(1),
input:nth-of-type(3):checked ~ .bn_arrows label:nth-of-type(2){
    display: block;
    left: 10px;
}
/* 绑定事件结束 */
/* 添加轮播块内容动画 */
.banner li h2,.banner li p{
    position: absolute;
    left: 100px;
    opacity: 0;
}
.banner li h2{
    top: 80px;
    font-size: 20px;
    transform: translateY(-30px);
    transition: all .4s .3s;
}
.banner li p{
    top: 130px;
    font-size: 16px;
    transform: translateX(-30px);
    transition: all .4s .5s;
    color: #ccc;
}
input:nth-of-type(1):checked ~ .banner li:nth-of-type(1) h2,
input:nth-of-type(1):checked ~ .banner li:nth-of-type(1) p,
input:nth-of-type(2):checked ~ .banner li:nth-of-type(2) h2,
input:nth-of-type(2):checked ~ .banner li:nth-of-type(2) p,
input:nth-of-type(3):checked ~ .banner li:nth-of-type(3) h2,
input:nth-of-type(3):checked ~ .banner li:nth-of-type(3) p{
    opacity: 1;
}
input:nth-of-type(1):checked ~ .banner li:nth-of-type(1) h2,
input:nth-of-type(2):checked ~ .banner li:nth-of-type(2) h2,
input:nth-of-type(3):checked ~ .banner li:nth-of-type(3) h2{
    transform: translateY(0);
}
input:nth-of-type(1):checked ~ .banner li:nth-of-type(1) p,
input:nth-of-type(2):checked ~ .banner li:nth-of-type(2) p,
input:nth-of-type(3):checked ~ .banner li:nth-of-type(3) p{
    transform: translateX(0);
}

通过css可以看到,其实这个例子主要用到的就是<input type="radio">:checked伪类选择器然后通错css3:nth-of-type(n)节点选择器控制与之相对应的轮播、按钮,整个例子也没什么难度,也就不废话了。

相关文章

  • 纯css3:radio+label实现轮播图

    预览地址:纯css3:radio+label实现轮播图 轮播图这东西,我相信只要是做前端的,肯定都做过,不过大部分...

  • 2018-07-30

    Jquery和纯JS实现轮播图(一)--左右切换式 一、页面结构 对于左右切换式的轮播图的结构主要分为以下几个部分...

  • 用四种方法实现轮播图

    1、css3动画实现的轮播图 实现原理如下呀: 1、设置大的div a)设置绝对定位,定位位置,b)设置图片展示...

  • 纯css实现轮播图

    主体思想 准备相同大小的多个图片 将要展示图片横排放在一个图片容器里面 在图片容器外再加一个展示容器,展示容器大小...

  • 纯CSS实现轮播图

    天天写前端的你,会自己写一个轮播图吗,而且不能用js哦,知道轮播图的原理吗? 今天我们要讲的是如何只用css实现轮...

  • 纯CSS3 网站轮播图

    今天抽空做了一个css3的轮播图,都是用的最简单的动画,这次练习的关键在于 所有的动画时间都是一样的,然后更具动画...

  • 轮播图心得

    轮播图 写轮播图之前我们要认识到几个问题:一、什么是轮播图?二、怎么实现轮播效果?三、轮播图还有什么小功能可以实现...

  • iOS轮播图封装 ECAutoScrollBanner

    ECAutoScrollBanner 轮播图封装。可以实现自动定时翻页、手动翻页;垂直和水平滚动等。支持纯文本、本...

  • 轮播图—纯js(javascript)实现:

    纯js实现: 要求:算了,还是先看图吧 实现效果:图片自动轮播,鼠标移入停止,移出继续轮播点击左右按钮实现切换点击...

  • 纯CSS实现轮播图效果

    今天是大年三十,猴年最后一天。为了完成自己在年初定的每周一篇前端周记的目标,又因为之后就要开启疯狂百年模式,所以决...

网友评论

      本文标题:纯css3:radio+label实现轮播图

      本文链接:https://www.haomeiwen.com/subject/asxibftx.html