0

0

css实现点击切换图片效果

王林

王林

发布时间:2020-09-11 16:36:39

|

4843人浏览过

|

来源于csdn

转载

css实现点击切换图片效果

我们先来看下效果图:

(相关教程:CSS教程

切换前:

ba59550ce0f8d5e8afb938c05fba548.png

立即学习前端免费学习笔记(深入)”;

svg点击图片切换动画特效
svg点击图片切换动画特效

一款svg点击图片切换动画特效

下载

切换中:
4b90e696a5e4bd9c4456cfeba7a517d.png

切换成功:

7af835e1e8ae016542c805eceaf8e41.png

HTML代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>来自Limou的尝试解读</title>
<link href="CSS/Public.css" rel="stylesheet" type="text/css">
</head>

<body style="background-color:#EAF5FF;">
		<div><h1>来自Limou的尝试解读</h1></div>
		<div class="button">
			<a href="#">1</a>
			<a href="#">2</a>
			<a href="#">3</a>
			<a href="#">4</a>

		<div class="Images">
			<!--选中第一个input-->
			<input id="in-img-1" name="InpitImages" class="cr-in-img-1" type="radio" checked>
			<label for="in-img-1" class="la-checkLabel-1">1</label>

			<input id="in-img-2" name="InpitImages" class="cr-in-img-2" type="radio">
			<label for="in-img-2" class="la-checkLabel-2">2</label>

			<input id="in-img-3" name="InpitImages" class="cr-in-img-3" type="radio">
			<label for="in-img-3" class="la-checkLabel-3">3</label>

			<input id="in-img-4" name="InpitImages" class="cr-in-img-4" type="radio">
			<label for="in-img-4" class="la-checkLabel-4">4</label>
			<div class="SpanStyle">
				<div>
					<span>Images1</span>
					<span>Images2</span>
					<span>Images3</span>
					<span>Images4</span>
				</div>
				<div>
					<span>Images1</span>
					<span>Images2</span>
					<span>Images3</span>
					<span>Images4</span>
				</div>
				<div>
					<span>Images1</span>
					<span>Images2</span>
					<span>Images3</span>
					<span>Images4</span>
				</div>
				<div>
					<span>Images1</span>
					<span>Images2</span>
					<span>Images3</span>
					<span>Images4</span>
				</div>
			</div>
			<div class="h3Span">
				<h3><span>我的世界</span><span>钻石</span></h3>
				<h3><span>小动物</span><span>松鼠</span></h3>
				<h3><span>上古卷轴</span><span>雪漫城随从</span></h3>
				<h3><span>守望先锋</span><span>不知道是谁</span></h3>
			</div>
		</div>
    </div>
</body>

	
</html>

CSS代码:

@charset "utf-8";
/* CSS Document */

h1{
	text-align: center;
	color:deepskyblue;
	text-shadow: 1px 1px 1px rgba(0,0,0,0.1);/*设置字体的阴影*/
}
/*顶部a标签动画与样式  开始*/
.button{
	text-align: center;
	height: 80px;
	width: 100%;
	display: block;
}
.button a{
	height: 60px;
	width: 60px;
	display: inline-block;/*布局属性:内联-块,
	使其成为并排显示的块级元素,让其宽高可以编辑,且宽高和内外边距可以超过父级元素*/
	text-decoration: none;/*去除下划线*/
	line-height: 60px;
	border-radius: 50%;
	/*设置字符的行高,通常用于自定义的某些样式,字符在顶部或底部时使用,让其上下居中*/
	background-color: rgba(104,171,194,0.5);
	transition:background-color 0.15s linear;/*组合写法*/
	/*transition-property	规定设置过渡效果的 CSS 属性的名称。
				-duration	规定完成过渡效果需要多少秒或毫秒。
				-timing-function	规定速度效果的速度曲线。
				-delay	定义过渡效果开始时间。*/
	color: white;
}
.button a:hover{
	background-color: rgba(255,255,255);
	color:#68ABC2;
	transition:background-color 0.15s linear;
}
/*顶部a标签动画与样式  开始*/


/*图片的样式与动画  开始*/
.SpanStyle{
	width: 100%;
	height: 400px;
	z-index: 1;
	position: absolute;
	top: 0;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	background-position: 0 0;
}
.SpanStyle div{
	width: 25%;
	height: 100%;
	float: left;
	position: relative;
	overflow: hidden;
	background-repeat: no-repeat;
}
.SpanStyle div span{
	position: absolute;
	width: 100%;
	height: 100%;
	top: 0px;
	left: -100%;/*设置元素初始位置,为动画准备*/
	z-index: 2;
	color: rgba(255,255,255,0);
	background-repeat: no-repeat;
	background-size: 400% 100%;
}

/*设置图片在元素的位置*/
.SpanStyle div:nth-child(1) span{
	background-position: 0% 0px;
}
.SpanStyle div:nth-child(2) span{
	background-position: 33.5% 0px;
}
.SpanStyle div:nth-child(3) span{
	background-position: 66.5% 0px;
}
.SpanStyle div:nth-child(4) span{
	background-position: 100% 0px;
}

/*父级元素下的 某个input标签和 它的类名:选中后匹配元素(单复选框专用) ~ 
一个父级元素下的div里面的Span元素:匹配元素(第N个) */
/*定义指定的input标签点击后将匹配的元素绑定新图片*/
.Images input.cr-in-img-1:checked ~ .SpanStyle, 
.SpanStyle div span:nth-child(1){
	background-image: url(../images/1.jpg);
}
.Images input.cr-in-img-2:checked ~ .SpanStyle,
.SpanStyle div span:nth-child(2){
	background-image: url(../images/2.jpg);
}
.Images input.cr-in-img-3:checked ~ .SpanStyle,
.SpanStyle div span:nth-child(3){
	background-image: url(../images/3.jpg);
}
.Images input.cr-in-img-4:checked ~ .SpanStyle,
.SpanStyle div span:nth-child(4){
	background-image: url(../images/4.jpg);
}

/*定义指定的input标签点击后将绑定的新图片所在的元素插入*/
.Images input.cr-in-img-1:checked ~ .SpanStyle div span:nth-child(1),
.Images input.cr-in-img-2:checked ~ .SpanStyle div span:nth-child(2),
.Images input.cr-in-img-3:checked ~ .SpanStyle div span:nth-child(3),
.Images input.cr-in-img-4:checked ~ .SpanStyle div span:nth-child(4)
{
	animation: none;
	transition: left 0.8s ease-in-out;
	left: 0%;
	z-index: 10;
}

/*定义指定的input标签点击后将要替换的旧图片所在的元素撤出*/
.Images input:checked ~ .SpanStyle div span{
	animation: slideOut 0.8s ease-in-out;
}

@keyframes slideOut{
	0%{ left: 0%; }
	100%{ left: 100%; }
}
/*图片的样式与动画  结束*/


/*文本动画与样式  开始*/
.Images{
	height: 400px;
	width: 40%;
	text-align: center;
	position: relative;
	margin: 2% auto 0 auto;
	border: 20px solid #fff;
	box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}
.Images input{
	display: none;
}
.Images label{
	font-style: italic; /*字体样式:倾斜*/
	margin-top: 350px;
	text-align: center;
	width: 25%;
	height: 30px;
	cursor: pointer;/*光标:手*/
	color: #fff; /*字体颜色*/
	position: relative;
	float: left;
	line-height: 34px;
	font-size: 24px;/*字号,字体大小*/
	z-index: 5;
}
.Images label:before{
	content:'';/*绑定文本也可以绑定文件*/
	width: 34px;
	height: 34px;
	background: rgba(130,195,217,0.9);
	position: absolute;
	left: 50%;
	margin-left: -14px;
	border-radius: 50%;
	box-shadow: 0px 0px 0px 4px rgba(255,255,255,0.3);
	z-index:-1;
}
.Images label:after{
	width: 1px;
	height: 400px;
	content: '';
	background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%);
    position: absolute;
	bottom: -20px;
	right: 0px;
}

.h3Span h3{
	position: absolute;
	width: 100%;
	top: 45%;
	z-index: 10;
	text-align: center;
	opacity: 0;
	color: #fff;
	text-shadow: 1px 1px 1px rgba(0,0,0,0.1);
	transition: opacity 0.8s ease-in-out;
}
.h3Span h3 span{
	z-index: 10;
	position: absolute;
	width: 100%;
	left: 0px;
	text-align: center;
	opacity: 1;
}
.h3Span h3 span:nth-child(1){
	font-family: 'NSimSun';
	font-size: 50px;
    display: block;
    letter-spacing: 7px;
}
.h3Span h3 span:nth-child(2){
	margin-top: 84px;
	letter-spacing: 0px;
	background: rgba(104,171,194,0.9);
	font-size: 14px;
	padding: 10px 0px;
	font-style: italic;
	transition: opacity 0.8s ease-in-out;
}

.Images input.cr-in-img-1:checked ~ .h3Span h3:nth-child(1),
.Images input.cr-in-img-2:checked ~ .h3Span h3:nth-child(2),
.Images input.cr-in-img-3:checked ~ .h3Span h3:nth-child(3),
.Images input.cr-in-img-4:checked ~ .h3Span h3:nth-child(4){
	opacity: 1;
}
/*文本的动画与样式  结束*/


/*页面缩小显示复选框*/
@media screen and (max-width: 1200px) {
	.Images input{
		display: inline;
		width: 22%;
		margin-top: 350px;
		z-index: 10;
		position: relative;
	}
	.Images label{
		display: none;
	}
}

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

2

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

56

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

30

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

59

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

25

2026.03.03

Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

79

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

61

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

50

2026.02.28

Golang 并发编程模型与工程实践:从语言特性到系统性能
Golang 并发编程模型与工程实践:从语言特性到系统性能

本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。

47

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.9万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.5万人学习

CSS教程
CSS教程

共754课时 | 39.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号