前端基础学习

target伪类实现标签切换不起作用的问题

2020-02-28  本文已影响0人  小雪洁

目的是希望实现点击标签切换相应的页,原始代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>移动端视图切换</title>
    </head>
    <style>
        *{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        body{
            width: 100vw;
            height: 100vh;
            display: flex;
            flex-direction: column;
        }
        main{
            flex:1; /* flex:1应用在弹性元素上,main会自动撑开剩余空间 相当于flex-grow:1;flex-shrink:1;flex-basis:0; */
            position: relative;
        }
        nav{
            height: 10vh;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            background-color: steelblue;
        }
        nav a{
            font-size: 3em;
            text-transform: uppercase;
            color: white;
            opacity: .8;
            flex:1;
            text-decoration:none;
            text-align: center;
        }
        nav a:nth-child(2){
            border-left: solid #FFEFD5 3px;
            border-right: solid #FFEFD5 3px;
        }
        main>div{
            position: absolute;
            top:0;
            bottom: 0;
            width: 100%;
            height: 100%;
            background: #FFFFFF;
            font-size: 3em;
        }
        /* 问题在这里 */
        main>div:nth-child(1):target{
            background: #009f40;
        }
        main>div:nth-child(2):target{
            background: #DCAE3F;
        }
        main>div:nth-child(3):target{
            background: #93D1FF;
        }
    </style>
    <body>
        <main>
            <div id="home">home</div>
            <div id="video">video</div>
            <div id="live">live</div>
        </main>
        <nav>
            <a href="#home">Home</a>
            <a href="#video">Video</a>
            <a href="#live">Live</a>
        </nav>
    </body>
</html>
main>div:nth-child(1):target{
    background: #009f40;
    z-index: 1;
}
main>div:nth-child(2):target{
    background: #DCAE3F;
    z-index: 1;
}
main>div:nth-child(3):target{
    background: #93D1FF;
    z-index: 1;
}
上一篇下一篇

猜你喜欢

热点阅读