手風琴式摺疊卡片展現效果|8月更文挑戰

做者:battleKing
倉庫:GithubCodePen
博客:CSDN掘金
反饋郵箱:myh19970701@foxmail.com
特別聲明:原創不易,未經受權不得轉載或抄襲,如需轉載可聯繫筆者受權css

背景

手風琴式摺疊卡片展現效果,通常用於 展現圖片、照片、文字 等等。其 主要特色 是:當咱們點擊任意一張照片時,那張照片像手風琴同樣緩緩展開,其餘照片像手風琴同樣緩緩摺疊起來,後續咱們還能夠向其添加不少各類各樣豐富的效果,好比自動輪播、增長3D立體感 ......html

最終效果

摺疊卡片.gif

1、添加 HTML 文件

  1. 添加一層最外層的 div 命名類名爲 box
  2. box 裏面添加五個類名爲 paneldiv
  3. 在第一個類名爲 paneldiv 中添加 active 類名
  4. 每個類名爲 paneldiv 中添加一個 <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>
複製代碼

2、添加 CSS 文件

先初始化頁面git

  1. 設置 *box-sizing: border-box
  2. 設置 body 來使整個項目居中
body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}
複製代碼

介紹一個隨機圖庫github

做用:生產隨機假圖,用於測試web

Lorem Picsum 圖庫官網所有圖片IDmarkdown

  • 用法
  1. 隨機圖片 https://picsum.photos/200/300
  2. 防止重複的隨機圖片 https://picsum.photos/1350/900?random=1
  3. 指定特定的圖片 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;
}
複製代碼

3、添加 JS 文件

主要邏輯oop

  1. 先獲取節點 document.querySelectorAll('.panel')
  2. 經過 forEach 進行遍歷,爲每個類名爲 pancel 的元素都添加上一個 click事件
  3. 這個事件觸發時,又經過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

相關文章
相關標籤/搜索