纯CSS实现JS效果研究

利用CSS3
:checked
选择器和
~
配合实现tab切换

效果:
857662-20160923153717043-2078381410.png

代码:

<style>
    body,div,input,label{
        margin:0;
        padding:0;
    }
    #tab>input{
        opacity:0;
        position:absolute;
        left:0;
        top:0;
    }
    #tab .label{
        overflow:hidden;
    }
    #tab .label>label{
        float:left;
        width:100px;
        height:30px;
        line-height:30px;
        border:1px solid #dedede;
        text-align:center;
        margin-left:-1px;
    }
    .content li{
        display:none;
    }
    
    input:nth-of-type(1):checked~.label>label:nth-of-type(1){
        background-color:red;
        color:#fff;
    }
    input:nth-of-type(2):checked~.label>label:nth-of-type(2){
        background-color:red;
        color:#fff;
    }
    input:nth-of-type(3):checked~.label>label:nth-of-type(3){
        background-color:red;
        color:#fff;
    }
    input:nth-of-type(1):checked~.content li:nth-of-type(1){
        display:block;
    }
    input:nth-of-type(2):checked~.content li:nth-of-type(2){
        display:block;
    }
    input:nth-of-type(3):checked~.content li:nth-of-type(3){
        display:block;
    }
</style>
<div id="tab">
    <input checked type="radio" name="name" id="name1">
    <input type="radio" name="name" id="name2">
    <input type="radio" name="name" id="name3">
    <div class="label">
        <label for="name1">选择1</label>
        <label for="name2">选择2</label>
        <label for="name3">选择3</label>
    </div>
    <div class="content">
        <ul>
            <li>内容1内容内容1内容1</li>
            <li>内容2内容内容2内容2</li>
            <li>内容3内容内容3内容3</li>
        </ul>
    </div>
</div>

原理就是点击label的时候就会选中input标签,然后通过CSS让当前选中的那个元素的对应内容显示就好了。

利用:target实现遮罩层效果

单击按钮的时候会弹出遮罩层,如果再点遮罩层的话就会把遮罩层给隐藏。

857662-20160924104214059-1449903032.gif
代码:

<style>
   #show{
      position:fixed;
      left:0;
      top:0;
      width:100%;
      height:100%;
      background-color:rgba(0,0,0,0.5);
      display:none;
   }
   a[href="#hide"]{
      position:absolute;
      left:0;
      top:0;
      width:100%;
      height:100%;
   }
   #hide:target{
    display:none;
   }
   #show:target{
    display:block;
   }
</style>

<a href="#show">显示</a>
<div id="hide">
  <div id="show">
    <a href="#hide"></a>
  </div>
</div>

长期更新...

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值