Table of Contents
简介
效果图
技术点
原理说明
代码
Home Web Front-end HTML Tutorial 用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose

用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose

Jun 21, 2016 am 09:08 AM

之前发了两篇关于CSS的文章,大家都比较关注,看来大家对这块东西都是很感兴趣的,现在趁热打铁再来一发~~

简介

一个用纯CSS实现的简单幻灯片效果,仅供实验,要看Demo请自备Chrome浏览器,勿用于生产环境,不然后果自付,咩哈哈哈哈哈~~

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

效果图

效果图录出来很烂,不上传了,请看Demo :(

技术点

  • :target伪类
  • object-fit属性

原理说明

:target伪类可以指定当前锚点目标元素的样式,比如下面:

<a href="#image-1">查看图片</a><img  src="/static/imghw/default1.png"  data-src="xxx.jpg"  class="lazy"  id="image-1" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >
Copy after login
#image-1{  display: none;}#image-1:target{  display: block;}
Copy after login

点一下"显示图片",原本隐藏起来的#image-1就会突然冒出来,是不是很神奇?(噗→_→)

CSS中的object-fit属性在本Demo里面只是辅助作用,其作用是指定object类元素(img、video等)中的实际内容在元素中的填充方式,跟background-size: contain/cover有点点类似。
更具体的介绍请看张鑫旭的文章:半深入理解CSS3 object-position/object-fit属性

既然知道有: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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/alarm%20politie.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-2">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/flowers.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-3">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/her%20camera.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-4">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/little%20fox.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-5">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/map.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-6">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/ship.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-7">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/snow%20mountain.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>      <li>        <a href="#image-8">          <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/ship%20and%20shepherd.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        </a>      </li>    </ul>    <!--大图容器-->    <div class="hidden-images-wrapper">      <div id="image-1">        <img  src="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/alarm%20politie.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/flowers.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/her%20camera.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/little%20fox.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/map.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/ship.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/snow%20mountain.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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="/static/imghw/default1.png"  data-src="http://7rf38t.com1.z0.glb.clouddn.com/ship%20and%20shepherd.jpg"  class="lazy" alt="用纯CSS实现简单的相册幻灯片_html/css_WEB-ITnose" >        <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>
Copy after login

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;}
Copy after login

就这些。。。
最后丢一个单个图片放大展示的效果:http://output.jsbin.com/goyeju/2,有性趣的同学可以自行研究原理,也很简单的

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles