js 底部导航栏切换 (炫酷效果)
直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
:root{
--color:#222327;
}
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: var(--color);
}
.container{
width: 440px;
height: 70px;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
border-radius: 10px;
position: relative;
}
.item{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 100px;
}
img{
width: 30px;
height: 30px;
position: absolute;
font-size: 1.7em;
transition: .5s;
}
.text{
transform: translateY(20px);
font-size: 0.75em;
font-weight: bold;
transition: .5s;
opacity: 0;
}
.item.active img{
transform: translateY(-32px);
z-index: 1;
}
.item.active .text{
opacity: 1;
transform: translateY(10px);
}
.indicator{
position: absolute;
width: 70px;
height: 70px;
background: rgb(18, 212, 50);
left:35px;
top:-50%;
border-radius: 50%;
border:6px solid var(--color);
transition: .5s;
}
.indicator::before{
content: '';
position: absolute;
top: 50%;
left: -22px;
width: 20px;
height: 20px;
background: transparent;
border-top-right-radius: 20px;
box-shadow: 0 -10px 0 0 var(--color);
}
.indicator::after{
content: '';
position: absolute;
top: 50%;
right: -22px;
width: 20px;
height: 20px;
background: transparent;
border-top-left-radius: 20px;
box-shadow: 0 -10px 0 0 var(--color);
}
.item:nth-child(1).active ~ .indicator {
transform:translateX(calc(100px * 0));
}
.item:nth-child(2).active ~ .indicator {
transform:translateX(calc(100px * 1));
}
.item:nth-child(3).active ~ .indicator {
transform:translateX(calc(100px * 2));
}
.item:nth-child(4).active ~ .indicator {
transform:translateX(calc(100px * 3));
}
</style>
<body>
<div class="container">
<div class="item active">
<img src="https://s1.ax1x.com/2022/04/23/LffnC8.png" alt="">
<div class="text">首页</div>
</div>
<div class="item">
<img src="https://s1.ax1x.com/2022/04/23/Lffu8S.png" alt="">
<div class="text">分类</div>
</div>
<div class="item">
<img src="https://s1.ax1x.com/2022/04/23/LffKgg.png" alt="">
<div class="text">更多</div>
</div>
<div class="item">
<img src="https://s1.ax1x.com/2022/04/23/LffMvQ.png" alt="">
<div class="text">我的</div>
</div>
<div class="indicator"></div>
</div>
</body>
<script>
const list = document.querySelectorAll('.item')
console.log(list);
function activeLike(){
list.forEach((item) =>
item.classList.remove('active'))
this.classList.add('active')
}
list.forEach((item) => item.addEventListener('click',activeLike))
</script>
</html>