当先锋百科网

首页 1 2 3 4 5 6 7

程序员用代码交流:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>css3 tab切换</title>
    <!--角标-->
    <link rel="shortcut icon" href="https://www.kaisir.cn/icon/favicon.ico" target="_blank" rel="external nofollow" >
    <style>
        /* 根据target的特性锚链接到对应的div,再根据z-index的属性,改变div的层级关系,从而实现tab的切换效果!*/
        .tabmenu {
            position: absolute;
            top: 100%;
            margin: 0;
        }

        .tabmenu li {
            display: inline-block;
        }

        .tabmenu li a {
            display: block;
            padding: 5px 10px;
            margin: 0 10px 0 0;
            border: 1px solid #91a7b4;
            border-radius: 0 0 5px 5px;
            background: #e3f1f8;
            color: #333;
            text-decoration: none;
        }

        .tablist {
            position: relative;
            margin: 50px auto;
            min-height: 200px;
        }

        .tab_content {
            position: absolute;
            width: 600px;
            height: 170px;
            padding: 15px;
            border: 1px solid #91a7b4;
            border-radius: 3px;
            box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
            font-size: 1.2em;
            line-height: 1.5em;
            color: #666;
            background: #fff;
        }
        /*使用css3(:target属性实现),z-index控制元素层级*/
        #tab1:target, #tab2:target, #tab3:target {
            z-index: 1;
        }
    </style>
</head>
<body>
<div class="tablist">
    <ul class="tabmenu">
        <li><a href="#tab1" target="_blank" rel="external nofollow" >tab1</a></li>
        <li><a href="#tab2" target="_blank" rel="external nofollow" >tab2</a></li>
        <li><a href="#tab3" target="_blank" rel="external nofollow" >tab3</a></li>
    </ul>
    <div id="tab1" class="tab_content">tab1内容</div>
    <div id="tab2" class="tab_content">tab2内容</div>
    <div id="tab3" class="tab_content">tab3内容</div>
</div>

</body>
</html>

 

之前用js控制元素的显隐来实现tab切换效果,现在用css3来实现,省去了很多代码,化繁为简!