做者:battleKing
倉庫:Github、CodePen
博客:CSDN、掘金
反饋郵箱:myh19970701@foxmail.com
特別聲明:原創不易,未經受權不得轉載或抄襲,如需轉載可聯繫筆者受權css
手風琴式摺疊卡片展現效果,通常用於 展現圖片、照片、文字
等等。其 主要特色
是:當咱們點擊任意一張照片時,那張照片像手風琴同樣緩緩展開,其餘照片像手風琴同樣緩緩摺疊起來,後續咱們還能夠向其添加不少各類各樣豐富的效果,好比自動輪播、增長3D立體感 ......html
div
命名類名爲 box
box
裏面添加五個類名爲 panel
的 div
panel
的 div
中添加 active
類名panel
的 div
中添加一個 <h3> </h3>
<div class="box">
<div class="panel active"">
<h3>Explore The World</h3>
</div>
<div class=" panel">
<h3>Wild Forest</h3>
</div>
<div class="panel">
<h3>Sunny Beach</h3>
</div>
<div class="panel">
<h3>City on Winter</h3>
</div>
<div class="panel">
<h3>Mountains - Clouds</h3>
</div>
</div>
複製代碼
先初始化頁面git
*
爲 box-sizing: border-box
body
來使整個項目居中body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
複製代碼
介紹一個隨機圖庫github
做用:生產隨機假圖,用於測試web
Lorem Picsum 圖庫官網 、 所有圖片IDmarkdown
https://picsum.photos/200/300
https://picsum.photos/1350/900?random=1
https://picsum.photos/id/237/200/300
主要樣式的代碼dom
.box {
display: flex;
width: 90vw;
}
.panel {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: #fff;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
-webkit-transition: all 700ms ease-in;
transition: all 700ms ease-in;
}
.panel:nth-child(1){
background-image: url("https://picsum.photos/1350/900?random=1");
}
.panel:nth-child(2){
background-image: url("https://picsum.photos/1350/900?random=2");
}
.panel:nth-child(3){
background-image: url("https://picsum.photos/1350/900?random=3");
}
.panel:nth-child(4){
background-image: url("https://picsum.photos/1350/900?random=4");
}
.panel:nth-child(5){
background-image: url("https://picsum.photos/1350/900?random=5");
}
.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}
.panel.active {
flex: 5;
}
.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.4s;
}
複製代碼
主要邏輯oop
document.querySelectorAll('.panel')
forEach
進行遍歷,爲每個類名爲 pancel
的元素都添加上一個 click事件
forEach
進行遍歷,移除所有 pancel
的元素的 active
類名,而後再爲 被點擊的那個 pancel
元素添上一個 active
類名。const panels = document.querySelectorAll('.panel')
panels.forEach(panel => {
panel.addEventListener('click', () => {
removeActiveClasses()
panel.classList.add('active')
})
})
function removeActiveClasses() {
panels.forEach(panel => {
panel.classList.remove('active')
})
}
複製代碼
若是本文對你有幫助,就點個贊支持下吧,你的「贊」是我創做的動力。測試
若是你喜歡這篇文章的話,能夠「點贊」 + 「收藏」 + 「轉發」 給更多朋友。flex