HTML核心代码
<div class="container">
<div class="item active" style="background-image: url(/images/133/sun.jpg);">
<div class="shadow"></div>
<div class="content">
<div class="icon">
<i class="fa fa-sun-o"></i>
</div>
<div class="text">
<div class="tit">这是标题</div>
<div class="sub">这是一段描述</div>
</div>
</div>
</div>
<div class="item" style="background-image: url(/images/133/cloud.jpg);">
<div class="shadow"></div>
<div class="content">
<div class="icon">
<i class="fa fa-cloud"></i>
</div>
<div class="text">
<div class="tit">这是标题</div>
<div class="sub">这是一段描述</div>
</div>
</div>
</div>
<div class="item" style="background-image: url(/images/133/rain.jpg);">
<div class="shadow"></div>
<div class="content">
<div class="icon">
<i class="fa fa-tint"></i>
</div>
<div class="text">
<div class="tit">这是标题</div>
<div class="sub">这是一段描述</div>
</div>
</div>
</div>
<div class="item" style="background-image: url(/images/133/bolt.jpg);">
<div class="shadow"></div>
<div class="content">
<div class="icon">
<i class="fa fa-bolt"></i>
</div>
<div class="text">
<div class="tit">这是标题</div>
<div class="sub">这是一段描述</div>
</div>
</div>
</div>
<div class="item" style="background-image: url(/images/133/snow.jpg);">
<div class="shadow"></div>
<div class="content">
<div class="icon">
<i class="fa fa-snowflake-o"></i>
</div>
<div class="text">
<div class="tit">这是标题</div>
<div class="sub">这是一段描述</div>
</div>
</div>
</div>
</div>
CSS代码
*{
margin: 0;
padding: 0;
}
body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: radial-gradient(circle at top center,#718497,#29323c);
}
.container{
display: flex;
width: 90vw;
max-width: 900px;
height: 400px;
overflow: hidden;
}
.item{
position: relative;
width: 60px;
margin: 10px;
cursor: pointer;
border-radius: 30px;
background-size: cover;
background-position: center;
transition: 0.5s cubic-bezier(0.05,0.61,0.41,0.95);
overflow: hidden;
}
.item .shadow{
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 100px;
transition: 0.5s cubic-bezier(0.05,0.61,0.41,0.95);
}
.item .content{
display: flex;
position: absolute;
left: 10px;
right: 0;
bottom: 10px;
height: 40px;
transition: 0.5s cubic-bezier(0.05,0.61,0.41,0.95);
}
.item .content .icon{
min-width: 40px;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 16px;
}
.item:nth-child(1) .fa{
color: #fc6e51;
}
.item:nth-child(2) .fa{
color: #ffce54;
}
.item:nth-child(3) .fa{
color: #2ecc71;
}
.item:nth-child(4) .fa{
color: #5d9cec;
}
.item:nth-child(5) .fa{
color: #ac92ec;
}
.item .content .text{
display: flex;
flex-direction: column;
justify-content: center;
margin-left: 10px;
color: #fff;
width: 100%;
}
.item .content .text div{
width: calc(100% - 70px);
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
opacity: 0;
transition: opacity 0.5s ease-out;
}
.item .content .text .tit{
font-weight: bold;
font-size: 18px;
}
.item .content .text .sub{
transition-delay: 0.1s;
}
.item.active{
flex: 1;
margin: 0;
border-radius: 40px;
}
.item.active .shadow{
background: linear-gradient(to top,rgba(0,0,0,0.35) 65%,transparent);
}
.item.active .content{
bottom: 20px;
left: 20px;
}
.item.active .content .text div{
opacity: 1;
}
JS代码
// 获取所有.item元素
let items=document.querySelectorAll('.item');
// 设置选中态样式
function setActive(){
// 遍历所有.item元素,移出active样式
items.forEach((item)=>{
item.classList.remove('active');
})
// 为当前选中项添加active样式
this.classList.add('active');
}
// 遍历所有.item元素,分别为其设置点击事件
items.forEach((item)=>{
item.addEventListener('click',setActive);
})