CSS3实现的动态内容标签页切换效果

内容标签页在网站或者web开发中经常使用到,它对于帮助我们美化页面非常的实用。这个教程中我们将使用radio button和:checked伪类和sibling组合来实现一个CSS3内容标签页。

 

注意目前并不是所有的浏览器都支持CSS3。


HTML标签 

这里我们使用input元素来生成内容切换操作元素,并且使用label元素来生成标签页内容:

 

<section class="tabs">
    <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
    <label for="tab-1" class="tab-label-1">About us</label>

    <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
    <label for="tab-2" class="tab-label-2">How we work</label>

    <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
    <label for="tab-3" class="tab-label-3">References</label>

    <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
    <label for="tab-4" class="tab-label-4">Contact us</label>

    <div class="clear-shadow"></div>

    <div class="content">
        <div class="content-1">
            <p>Some content</p>
        </div>
        <div class="content-2">
            <p>Some content</p>
        </div>
        <div class="content-3">
            <p>Some content</p>
        </div>
        <div class="content-4">
            <p>Some content</p>
        </div>
    </div>
</section> 

 

每一个input元素都包含一个数值,我们可以通过checked属性添加缺省的值。


CSS样式 

首先我们需要定义尺寸并且通过设置opacity:0来隐藏input

 

tabs {
    position: relative;
    margin: 40px auto;
    width: 750px;
}

.tabs input {
    position: absolute;
    z-index: 1000;
    width: 120px;
    height: 40px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
}
.tabs input#tab-2{
    left: 120px;
}
.tabs input#tab-3{
    left: 240px;
}
.tabs input#tab-4{
    left: 360px;
} 

  

input元素将会覆盖label。这样当我们点击label的时候,其实是点击input。这个小技巧同样可以使用在mobile浏览器中(有些浏览器,点击label将不会对input产生效果)。

 

接下来,我们将通过定义一些样式使label看起来像标签页。注意每一个label都有一个不同的z-index。一个box-shadow将会添加深度和真实感到标签页。

 

.tabs label {
    background: linear-gradient(top, #5ba4a4 0%,#4e8c8a 100%);
    font-size: 15px;
    line-height: 40px;
    height: 40px;
    position: relative;
    padding: 0 20px;
    float: left;
    display: block;
    width: 80px;
    color: #385c5b;
    letter-spacing: 1px;
    text-transform: uppercase;
    font-weight: bold;
    text-align: center;
    text-shadow: 1px 1px 1px rgba(255,255,255,0.3);
    border-radius: 3px 3px 0 0;
    box-shadow: 2px 0 2px rgba(0,0,0,0.1), -2px 0 2px rgba(0,0,0,0.1);
}

.tabs input:hover + label {
    background: #5ba4a4;
}

.tabs label:first-of-type {
    z-index: 4;
    box-shadow: 2px 0 2px rgba(0,0,0,0.1);
}

.tab-label-2 {
    z-index: 3;
}

.tab-label-3 {
    z-index: 2;
}

.tab-label-4 {
    z-index: 1;
}

 

因为我们不想显示box-shadow的底部,这里我们将使用一个:after伪类来覆盖:
.tabs label:after {
    content: '';
    background: #fff;
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 2px;
    display: block;
} 

  

当我们点击一个标签页时,将会有不同的样式和颜色。最重要的一点在于确认"checked" label将会被置于所有标签页的顶层。因此,这里我们修改z-index:

 

.tabs input:checked + label {
    background: #fff;
    z-index: 6;
} 

 

上面我们提到过,内容部分将包含了所有的标签页面,我们将设置z-index到5,正好处于选择的label的下方。使用这种方式,box-shadow内容将会覆盖其它的label。

 

在内容区域,这里有四个部分,每一个都有自己的内容。缺省,我们隐藏他们。因此,我们设置opacity为0并且z-index为1。我们不能使用display:none,因为这个属性不支持过渡效果。
.content {
    background: #fff;
    position: relative;
    width: 100%;
    height: 370px;
    z-index: 5;
    box-shadow: 0 -2px 3px -2px rgba(0,0,0,0.2), 0 2px 2px rgba(0,0,0,0.1);
    border-radius: 0 3px 3px 3px;
}

.content div {
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px 40px;
    z-index: 1;
    opacity: 0;
    transition: all linear 0.1s;
}

.content div h2,
.content div h3{
    color: #398080;
}
.content div p {
    font-size: 14px;
    line-height: 22px;
    font-style: italic;
    text-align: left;
    margin: 0;
    color: #777;
    padding-left: 15px;
    font-family: Cambria, Georgia, serif;
    border-left: 8px solid rgba(63,148,148, 0.1);
}
 
当我们想要一个内容出现,我们设置opacity为1,并且将z-index值变大。

 

.tabs input.tab-selector-1:checked ~ .content .content-1,
.tabs input.tab-selector-2:checked ~ .content .content-2,
.tabs input.tab-selector-3:checked ~ .content .content-3,
.tabs input.tab-selector-4:checked ~ .content .content-4 {
    z-index: 100;
    opacity: 1;
    transition: all ease-out 0.2s 0.1s;
} 

 

 

在这个教程中,我们介绍了基本的淡入/淡出的例子

转载于:https://www.cnblogs.com/zdp007/p/6391109.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值