HTML5+CSS3小实例:富有弹性的导航栏标签

525 阅读1分钟

HTML5+CSS3实现富有弹性的导航栏标签,切换如此Q弹的标签栏,你确定不手敲一遍?没有用到JavaScript哦!

效果:

源码:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <title>弹性标签动画</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/6.css">
</head>

<body>
    <div class="wrapper">
        <nav>
            <input type="radio" name="tab" id="home" checked>
            <input type="radio" name="tab" id="comment">
            <input type="radio" name="tab" id="envelope">
            <input type="radio" name="tab" id="heart">
            <input type="radio" name="tab" id="user">
            <label for="home" class="home" onclick="location.href='#';"><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Home</a></label>
            <label for="comment" class="comment" onclick="location.href='#';"><a href="#"><i class="fa fa-comment" aria-hidden="true"></i>Comment</a></label>
            <label for="envelope" class="envelope" onclick="location.href='#';"><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i>Envelope</a></label>
            <label for="heart" class="heart" onclick="location.href='#';"><a href="#"><i class="fa fa-heart" aria-hidden="true"></i>Heart</a></label>
            <label for="user" class="user" onclick="location.href='#';"><a href="#"><i class="fa fa-user" aria-hidden="true"></i>User</a></label>
            <div class="tab"></div>
        </nav>
    </div>
</body>

</html>
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body{
    /* 100%窗口高度 */
    height: 100vh;
    /* 弹性布局 水平垂直居中 文字居中 */
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
}
.wrapper{
    /* 60%窗口宽度 */
    width: 60vw;
    height: 60px;
    line-height: 60px;
    background-color: #fff;
    /* 盒子阴影 */
    box-shadow: 0px 5px 15px rgba(0,0,0,0.25);
    border-radius: 50px;
}
.wrapper nav{
    display: flex;
    position: relative;
}
.wrapper nav label{
    flex:1;
    width: 100%;
    position: relative;
    z-index: 1;
    cursor: pointer;
}
.wrapper nav label a{
    position: relative;
    z-index: -1;
    color: #1d1f20;
    font-size: 20px;
    font-weight: 500;
    text-decoration: none;
    pointer-events: none;
}
.wrapper nav label a i{
    font-size: 25px;
    margin: 0px 7px;
}
.wrapper nav .tab{
    position: absolute;
    height: 100%;
    width: 20%;
    left: 0px;
    bottom: 0px;
    /* 渐变背景 自左向右 */
    background: linear-gradient(to right, #f09819, #ff5858);
    border-radius: 50px;
    /* 动画过渡 贝塞尔曲线 */
    transition: 0.6s cubic-bezier(0.68,-0.55,0.265,1.55);
}
.wrapper nav input{
    display: none;
}
.wrapper nav #home:checked ~ label.home a,
.wrapper nav #comment:checked ~ label.comment a,
.wrapper nav #envelope:checked ~ label.envelope a,
.wrapper nav #heart:checked ~ label.heart a,
.wrapper nav #user:checked ~ label.user a{
    color: #fff;
    transition: 0.6s;
}
.wrapper nav #comment:checked ~ .tab{
    left: 20%;
}
.wrapper nav #envelope:checked ~ .tab{
    left: 40%;
}
.wrapper nav #heart:checked ~ .tab{
    left: 60%;
}
.wrapper nav #user:checked ~ .tab{
    left: 80%;
}