纯css3:radio+label实现轮播图

纯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)节点选择器控制与之相对应的轮播、按钮,整个例子也没什么难度,也就不废话了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容

  • 1.背景介绍 轮播图,是由网页banner进化而来,通常放在屏幕最显眼的位置,以大图显示。随着互联网的发展...
    xiaoyudesu阅读 3,321评论 0 16
  • 1.背景介绍 轮播图,是由网页banner进化而来,通常放在屏幕最显眼的位置,以大图显示。随着互联网的发展,网页中...
    xiaoyudesu阅读 1,915评论 0 8
  • 首先先看demo吧,点击查看demo 一、随便说几句 css3动画效果的强大不言而喻,自它出现一直热度不减,它与j...
    忽如寄阅读 51,280评论 17 55
  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 1,610评论 0 7
  • 今天看完了《世界上的另一个我·欧洲季》的第三集!纪录片导演杨帆骑着摩托车在世界上寻找与他同年月日出生(1990年1...
    wblearn阅读 493评论 2 15