用純CSS實現簡單的相冊幻燈片

以前發了兩篇關於CSS的文章,你們都比較關注,看來你們對這塊東西都是很感興趣的,如今趁熱打鐵再來一發~~css

簡介

一個用純CSS實現的簡單幻燈片效果,僅供實驗,要看Demo請自備Chrome瀏覽器,勿用於生產環境,否則後果自付,咩哈哈哈哈哈~~html

Demo地址:http://output.jsbin.com/tewusocss3

效果圖

效果圖錄出來很爛,不上傳了,請看Demo :(瀏覽器

技術點

  • :target僞類
  • object-fit屬性

原理說明

:target僞類能夠指定當前錨點目標元素的樣式,好比下面:app

<a href="#image-1">查看圖片</a>
<img id="image-1" src="xxx.jpg">
#image-1{
  display: none;
}
#image-1:target{
  display: block;
}

點一下"顯示圖片",本來隱藏起來的#image-1就會忽然冒出來,是否是很神奇?(噗→_→)ide


CSS中的object-fit屬性在本Demo裏面只是輔助做用,其做用是指定object類元素(img、video等)中的實際內容在元素中的填充方式,跟background-size: contain/cover有點點相似。
更具體的介紹請看張鑫旭的文章:半深刻理解CSS3 object-position/object-fit屬性wordpress


既然知道有:target這麼個神奇的東西在,那咱們徹底能夠用CSS實現「點一個小圖顯示一個大圖」的效果,再把HTML寫得溜~一點,實現上下圖連續查看也是能夠的。佈局

代碼

先上HTML部分,能夠看到其實這種方式實現的幻燈片效果並不太靈活,每個上下圖按鈕都要寫出來,固然用程序生成此類代碼也是很方便的~性能

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="container">
    <!--縮略圖列表-->
    <ul class="gallery-wrapper">
      <li>
        <a href="#image-1">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/alarm%20politie.jpg">
        </a>
      </li>
      <li>
        <a href="#image-2">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/flowers.jpg">
        </a>
      </li>
      <li>
        <a href="#image-3">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/her%20camera.jpg">
        </a>
      </li>
      <li>
        <a href="#image-4">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/little%20fox.jpg">
        </a>
      </li>
      <li>
        <a href="#image-5">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/map.jpg">
        </a>
      </li>
      <li>
        <a href="#image-6">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/ship.jpg">
        </a>
      </li>
      <li>
        <a href="#image-7">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/snow%20mountain.jpg">
        </a>
      </li>
      <li>
        <a href="#image-8">
          <img src="http://7rf38t.com1.z0.glb.clouddn.com/ship%20and%20shepherd.jpg">
        </a>
      </li>
    </ul>
    <!--大圖容器-->
    <div class="hidden-images-wrapper">
      <div id="image-1">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/alarm%20politie.jpg">
        <a class="img-prev" href="#image-8">PREV</a>
        <a class="img-next" href="#image-2">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-2">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/flowers.jpg">
        <a class="img-prev" href="#image-1">PREV</a>
        <a class="img-next" href="#image-3">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-3">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/her%20camera.jpg">
        <a class="img-prev" href="#image-2">PREV</a>
        <a class="img-next" href="#image-4">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-4">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/little%20fox.jpg">
        <a class="img-prev" href="#image-3">PREV</a>
        <a class="img-next" href="#image-5">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-5">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/map.jpg">
        <a class="img-prev" href="#image-4">PREV</a>
        <a class="img-next" href="#image-6">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-6">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/ship.jpg">
        <a class="img-prev" href="#image-5">PREV</a>
        <a class="img-next" href="#image-7">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-7">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/snow%20mountain.jpg">
        <a class="img-prev" href="#image-6">PREV</a>
        <a class="img-next" href="#image-8">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
      <div id="image-8">
        <img src="http://7rf38t.com1.z0.glb.clouddn.com/ship%20and%20shepherd.jpg">
        <a class="img-prev" href="#image-7">PREV</a>
        <a class="img-next" href="#image-1">NEXT</a>
        <a class="img-close" href="#"></a>
      </div>
    </div>
  </div>
</body>
</html>

CSS代碼,很大部分都是用於定位啊佈局什麼的,另外還有至關一部分用於過渡動畫效果,貌似有點影響性能~動畫

html,body{
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #f0f0f0;
}

.gallery-wrapper{
  display: block;
  list-style: none;
  width: 800px;
  height: 400px;
  margin: 60px auto 0 auto;
  padding: 10px;
  background-color: #fff;
}
.gallery-wrapper li{
  display: block;
  float: left;
  list-style: none;
  width: 180px;
  height: 180px;
  padding: 10px;
}
.gallery-wrapper a{
  display: block;
  width: 180px;
  height: 180px;
  overflow: hidden;
}
.gallery-wrapper img{
  display: block;
  width: 180px;
  height: 180px;
  object-fit: cover;
  background-color: #eee;
  transition: .5s;
}
.gallery-wrapper a:hover img{
  transform: scale(1.1);
}

.hidden-images-wrapper > div{
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0,0,0,.8);
  opacity: 0;
  transition: .6s;
  pointer-events: none;
}
.hidden-images-wrapper > div:target{
  opacity: 1;
  pointer-events: auto;
}
.hidden-images-wrapper img{
  display: block;
  position: absolute;
  z-index: 3;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 740px;
  height: 500px;
  max-width: 90%;
  max-height: 90%;
  margin: auto;
  padding: 30px;
  background-color: #fff;
  border-radius: 5px;
  object-fit: contain;
  transition: .7s;
  transform: translateY(-30px);
}
.hidden-images-wrapper div:target img{
  transform: translateY(0);
}
.img-prev,
.img-next{
  display: block;
  position: absolute;
  z-index: 4;
  top: 0;
  bottom: 0;
  height: 50px;
  margin: auto 0;
  padding: 0 20px;
  color: #333;
  font-size: 16px;
  line-height: 50px;
  text-decoration: none;
  background-color: #fff;
}
.img-prev:hover,
.img-next:hover{
  background-color: #eee;
}
.img-prev{
  left: 0;
  border-radius: 0 5px 5px 0;
}
.img-next{
  right: 0;
  border-radius: 5px 0 0 5px;
}
.img-close{
  position: absolute;
  z-index: 2;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  cursor: default;
}

就這些。。。
最後丟一個單個圖片放大展現的效果:http://output.jsbin.com/goyeju/2,有性趣的同窗能夠自行研究原理,也很簡單的

相關文章
相關標籤/搜索