使用CSS3实现的幻灯图片效果

1 篇文章 0 订阅

一、代码

1.html代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>CSS3</title>
		<link rel="stylesheet" type="text/css" href="./ppt.css"/>
	</head>
	<body>
		<div class="container">
			<header>
				<h1>使用CSS3实现幻灯图片的效果</h1>
				<p class="codrops-demos">&nbsp;</p>
				<section class="cr-container">
					<input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1" checked />
					<label for="select-img-1" class="cr-lable-img-1">1</label>
					
					<input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2" checked />
					<label for="select-img-2" class="cr-lable-img-2">2</label>
					
					<input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3" checked />
					<label for="select-img-3" class="cr-lable-img-3">3</label>
					
					<input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4" checked />
					<label for="select-img-4" class="cr-lable-img-4">4</label>
					
					<div class="clr"></div>
						<div class="cr-bgimg">
							<div>
							<span>Slice 1 - Image 1</span>
							<span>Slice 1 - Image 2</span>
							<span>Slice 1 - Image 3</span>
							<span>Slice 1 - Image 4</span>
							</div>
							
							<div>
							<span>Slice 2 - Image 1</span>
							<span>Slice 2 - Image 2</span>
							<span>Slice 2 - Image 3</span>
							<span>Slice 2 - Image 4</span>
							</div>
							
							<div>
							<span>Slice 3 - Image 1</span>
							<span>Slice 3 - Image 2</span>
							<span>Slice 3 - Image 3</span>
							<span>Slice 3 - Image 4</span>
							</div>
							
							<div>
							<span>Slice 4 - Image 1</span>
							<span>Slice 4 - Image 2</span>
							<span>Slice 4 - Image 3</span>
							<span>Slice 4 - Image 4</span>
							</div>
						</div>
					
					<div class="cr-titles"></div>
					
				</section>
			</header>
		</div>
	</body>
</html>

2.CSS代码

/* 设计section样式 */
.cr-container{
	width:600px;
	height:400px;
	position:relative;
	margin:0 auto;
	border:20px solid #fff;
	box-shadow:1px 1px 3px rgba(0,0,0,0.1);
}
/* 对label进行设置 */
.cr-container label{
	font-style:italic; 
	width: 150px;
	height:30px;
	cursor:pointer;
	color:#fff;
	line-height:32px;
	font-size:24px;
	float:left;
	position:relative;
	margin-top:350px;
	z-index:1000;
}
/* 对label进行美化:添加小圈,添加一个虚假元素并且居中 */
.cr-container label:before{
	content:'';
	width: 34px;
	height: 34px;
	background:rgba(130,195,217,0.9);
	position:absolute;
	left:50%;
	margin-left:-17px;
	border-radius:50%;
	box-shadow:0px 0px 0px 4px rgba(255,255,255,0.3);
	z-index:-1;
}
/* 为label创建,另一个虚假元素并且扩展到整个面板。使用渐变直线向上淡出 */
.cr-container label:after{
	width: 1px;
	height:400px;
	content:'';
	background: -moz-linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
	background: -webkit-gradient(linear,left top,left bottom,color-stop(0%,rgba(255,255,255,0)),color-stop(100%,rgba(255,255,255,1)));
	background: -webkit-linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
	background: -o-linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
	background: -ms-linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
	background: linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00ffffff',endColorstr='#ffffff',GradientType=0);
	position:absolute;
	bottom:-20px;
	right:0px;
}
/* 面板需要去除那条直线,所以设置宽度为0,同时将input隐藏 */
.container label.cr-label-img-4:after{
	width: 0px;
}
.cr-container input{
	display:none;
}
/* 不管是否单击label,分开的输入都会被checked。现在将使用一个统一的sibling属性选择器来处理分开的label
 因此修改选择的颜色*/
.cr-container label.cr-label-img-4:after{
	width: 0px;
}
.cr-container input.cr-selector-img-1 ~ label.cr-label-img-1,
.cr-container input.cr-selector-img-2 ~ label.cr-label-img-2,
.cr-container input.cr-selector-img-3 ~ label.cr-label-img-3,
.cr-container input.cr-selector-img-4 ~ label.cr-label-img-4{
	color:#68abc2;
}
/* 同时修改背景颜色和box阴影 */
.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1:before,
.cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2:before,
.cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3:before,
.cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4:before{
	background:#fff;
	box-shadow:0px 0px 0px 4px rgba(104,171,194,0.6);
}
/* 图片面板的容器将会占据所有的宽度并且绝对定位。这个容器将会在稍后使用,为了将图片设置为选择的图片,
 需要这样做来使图片默认可以显示*/
.cr-bgimg{
	width: 600px;
	height:400px;
	position:absolute;
	left:0px;
	top:0px;
	z-index:1;
	background-repeat:no-repeat;
	background-position:0 0;
}
/* 因为有4个面板图片,一个面板将有150px的宽度(600除4).面板会左漂移,隐藏overflow */
.cr-bgimg div{
	width: 150px;
	height:400px;
	position:absolute;
	float:left;
	overflow:hidden;
	background-repeat:no-repeat;
}
/* 每个显示快都是初始绝对定位,它们将通过left:-150px来被隐藏 */
.cr-bgimg div span{
	position:absolute;
	width:100%;
	height:100%;
	top:0px;
	left:-150px;
	z-index:2;
	text-indent:-9000;
}
/* 接下来是背景图片容器和单独的图片显示块 */
.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(1){
	background-image:url(./images/1.jpg);
}
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(2){
	background-image:url(./images/2.jpg);
}
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(3){
	background-image:url(./images/3.jpg);
}
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(4){
	background-image:url(./images/4.jpg);
}
/* 需要根据面板来设置图片块到正确位置 */
.cr-bgimg div:nth-child(1) span{
	background-position;0px 0px;
}
.cr-bgimg div:nth-child(2) span{
	background-position;-150px 0px;
}
.cr-bgimg div:nth-child(3) span{
	background-position;-300px 0px;
}
.cr-bgimg div:nth-child(4) span{
	background-position;-450px 0px;
}
/* 当单击label时简单的移动所有内容块到右边 */
.cr-container input:checked ~ .cr-bgimg div span{
	-webkit-animation:slideOut 0.6s ease-in-out;
	-moz-animation:slideOut 0.6s ease-in-out;
	-o-animation:slideOut 0.6s ease-in-out;
	-ms-animation:slideOut 0.6s ease-in-out;
	animation:slideOut 0.6s ease-in-out;
}
@-webkit-keyframes slideOut{
	0%{left:0px;}
	100%{left:150px;}
}
@-moz-keyframes slideOut{
	0%{left:0px;}
	100%{left:150px;}
}
@-o-keyframes slideOut{
	0%{left:0px;}
	100%{left:150px;}
}
@-ms-keyframes slideOut{
	0%{left:0px;}
	100%{left:150px;}
}
@keyframes  slideOut{
	0%{left:0px;}
	100%{left:150px;}
}
/* 所有的块都使用分开的图片,从-150px到0px.最后在浏览器中浏览可以看到效果 */
.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg div span:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg div span:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg div span:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg div span:nth-child(4){
	-webkit-transition:left 0.5s ease-in-out;
	-moz-transition:left 0.5s ease-in-out;
	-o-transition:left 0.5s ease-in-out;
	-ms-transition:left 0.5s ease-in-out;
	transition:left 0.5s ease-in-out;
	-webkit-animation:none;
	-moz-animation:none;
	-o-animation:none;
	-ms-animation:none;
	animation:none;
	left:0px;
	z-index:10;
}

二、效果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现幻灯图片加切割效果,可以使用CSS3的transform属性和clip-path属性。我们可以将图片设置为绝对定位,并使用transform属性和clip-path属性来实现切割和动画效果。 以下是一个使用transform和clip-path属性实现幻灯图片加切割效果的示例代码: ```html <div class="slider"> <div class="slide slide1"></div> <div class="slide slide2"></div> <div class="slide slide3"></div> </div> ``` ```css .slider { position: relative; width: 800px; height: 400px; overflow: hidden; } .slide { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-size: cover; background-position: center center; background-repeat: no-repeat; clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 100%); transform-origin: left; animation: slide 12s ease-in-out infinite; } .slide1 { background-image: url('slide1.jpg'); } .slide2 { background-image: url('slide2.jpg'); } .slide3 { background-image: url('slide3.jpg'); } @keyframes slide { 0% { transform: scaleX(0); clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 100%); } 16.67% { transform: scaleX(1); clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 100%); } 33.33% { transform: scaleX(1); clip-path: polygon(0 0, 75% 0, 100% 100%, 0% 100%); } 50% { transform: scaleX(1); clip-path: polygon(0 0, 100% 0, 100% 100%, 25% 100%); } 66.67% { transform: scaleX(1); clip-path: polygon(25% 0, 100% 0, 100% 100%, 0% 100%); } 83.33% { transform: scaleX(1); clip-path: polygon(0 0, 100% 0, 100% 75%, 0% 100%); } 100% { transform: scaleX(0); clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 100%); } } ``` 上述代码中,我们创建了一个幻灯图容器,并在其中添加了三个幻灯片元素。我们使用clip-path属性将每个幻灯片裁剪成不同的形状,并使用动画将它们进行幻灯片切换。 clip-path属性的参数是一个形状,可以使用polygon()函数来定义多边形形状。函数的参数是多边形的各个点的坐标,以顺时针顺序定义。 我们还使用了transform属性来实现幻灯片的切换效果。transform属性可以用来对元素进行变换,其中transform-origin属性用来设置变换的中心点。在本例中,我们将变换的中心点设置为左侧边缘。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值