Skip to content

Instantly share code, notes, and snippets.

@tiam-bloom
Last active August 6, 2023 04:23
Show Gist options
  • Save tiam-bloom/764d25cf89f2c98b3b03960b535b6dae to your computer and use it in GitHub Desktop.
Save tiam-bloom/764d25cf89f2c98b3b03960b535b6dae to your computer and use it in GitHub Desktop.
移动端底部内凹导航栏
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="http://at.alicdn.com/t/c/font_4144272_3vtq4renm6e.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #222327;
}
.nav {
width: 400px;
height: 70px;
padding: 0 25px;
border-radius: 10px;
background-color: #fff;
position: relative;
display: flex;
}
.nav > li {
width: 70px;
height: 70px;
z-index: 1;
position: relative;
list-style: none;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.nav > li > i {
display: block;
height: 70px;
line-height: 70px;
font-size: 24px;
text-align: center;
position: relative;
transition: all 0.5s;
}
.nav > li > span {
position: absolute;
font-size: 12px;
font-weight: 900;
letter-spacing: 2px;
transition: all 0.5s;
opacity: 0;
transform: translateY(20px);
}
/* 激活时的样式 */
.nav > li.active > i {
transform: translateY(-35px);
color: #fff;
}
.nav > li.active > span {
opacity: 1;
transform: translateY(10px);
}
/* 遮罩层 */
.indicator {
position: absolute;
top: -50%;
width: 70px;
height: 70px;
background-color: #2196f3;
border-radius: 50%;
transition: all 0.5s;
border: 6px solid #222327;
}
.indicator::after,
.indicator::before {
content: "";
position: absolute;
top: 50%;
width: 20px;
height: 20px;
background-color: #fff;
}
.indicator::after {
right: -22px;
border-top-left-radius: 20px;
box-shadow: -1px -10px 0 0 #222327;
}
.indicator::before {
left: -22px;
border-top-right-radius: 20px;
box-shadow: 1px -10px 0 0 #222327;
}
.nav > li:nth-child(1).active ~ .indicator {
transform: translateX(calc(70px * 0));
}
.nav > li:nth-child(2).active ~ .indicator {
transform: translateX(calc(70px * 1));
}
.nav > li:nth-child(3).active ~ .indicator {
transform: translateX(calc(70px * 2));
}
.nav > li:nth-child(4).active ~ .indicator {
transform: translateX(calc(70px * 3));
}
.nav > li:nth-child(5).active ~ .indicator {
transform: translateX(calc(70px * 4));
}
</style>
</head>
<body>
<ul class="nav">
<li class="active"><i class="iconfont icon-home"></i><span>Home</span></li>
<li><i class="iconfont icon-envelope"></i><span>Envelope</span></li>
<li><i class="iconfont icon-comment"></i><span>Comment</span></li>
<li><i class="iconfont icon-heart"></i><span>Heart</span></li>
<li><i class="iconfont icon-user"></i><span>User</span></li>
<div class="indicator"></div>
</ul>
<script>
const lis = document.querySelectorAll(".nav > li");
lis.forEach(li => {
li.addEventListener("click", function () {
for (const item of lis) {
item.classList.remove("active");
this.classList.add("active");
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment