用CSS3写一个旋转轮播图

用CSS3写一个旋转轮播图


今天用css3写一个不一样的轮播图3D效果。
先看下最后效果吧

不一样的轮播图
1.旋转轮播图结构制作

// html
<body>
	<section>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
	</section>
</body>
// css
<style>
section { /*这里放的图片是正中间那张*/
	width: 400px;
	height: 300px;
	background: url("img/1.jpg") no-repeat; /*根据自己图片的路径,设置为背景图片*/
	margin: 200px auto;/*这里是为了让section居中对齐*/
}
</style>

现在能看到的是这个效果
在这里插入图片描述
2.现在往每个div内设置背景图片

// css
section div {
	width: 100%;/*继承父盒子的宽度*/
	height: 100%;/*继承父盒子的高度*/
	background: url('img/2.jpg') no-repeat;/*暂且将每个div内的背景图片设置成一样,先看看效果*/
}

在这里插入图片描述
因为div是块级元素,所以现在的效果就是竖着排列,但是我们这些图片用到了定位,所以我们在 section div 中添加position: absolute;top:0;left:0;因为子盒子中用了绝对定位,所以我们要给父盒子添加相对定位 在 section 中添加position:relative;

此时效果如下,所有图片都重叠在一起
在这里插入图片描述
我们知道事物都是近大远小的,这是我们就要用css3 的 transfrom:translateZ()

// css
section div:nth-child(1) {
	/*rotateY:沿着 Y 轴的 3D 旋转。第一张图片是正对着我们的,所以我们沿Y轴的旋转为0度*/
	/*translateZ(400px)这个什么意思呢? 以Y轴作为中心轴,第一张图片距离中心轴400px距离,后面图片都要距离中心轴400px*/
	transform: rotateY(0deg) translateZ(400px); 
}

在这里插入图片描述
但我们发现写了transform: rotateY(0deg) translateZ(400px);之后,好像没有变化。
这时,我们要在 body 中添加一句代码 perspective: 1000px;

perspective是什么呢???
perspective 属性定义 3D 元素距视图的距离,以像素计。该属性允许您改变 3D 元素查看 3D 元素的视图。 当为元素定义 perspective 属性时,其子元素会获得透视效果,而不是元素本身。 注释:perspective 属性只影响 3D 转换元素。
添加之后,效果如下
在这里插入图片描述
那同样,我们这时对剩下的图片实现3d效果

/*因为我们这里有6张图片,一个圆有360度,每张图片沿Y轴旋转的度数差为360/6,同理,如果只有5张,度数差为360/5*/
section div:nth-child(1) {
	transform: rotateY(0deg) translateZ(400px);
}
section div:nth-child(2) {
	transform: rotateY(60deg) translateZ(400px);
}
section div:nth-child(3) {
	transform: rotateY(120deg) translateZ(400px);
}
section div:nth-child(4) {
	transform: rotateY(180deg) translateZ(400px);
}
section div:nth-child(5) {
	transform: rotateY(240deg) translateZ(400px);
}
section div:nth-child(16) {
	transform: rotateY(300deg) translateZ(400px);
}

这时就已经有点3D的感觉了,但是看着很奇怪
在这里插入图片描述
这时我们又要添加代码了

section {
	...
	transfrom-style: preserve-3d;
}

transfrom-style: preserve-3d;
preserve-3d 表示 子元素将保留其 3D 位置。
这时候就美滋滋了
在这里插入图片描述
最后我们可以写鼠标经过,开始旋转的效果了。

// css
section {
	...
	transition: all 10s linear; /*linear表示匀速运动*/
}
section:hover { /*鼠标移入section盒子中,沿Y轴转360度*/
    transform: rotateY(360deg);
}

最后,我们整理一下,将代码写全。

// html
<body>
	<section>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
		<div></div>
	</section>
</body>

// css
body {
	perspective: 1000px;
}
section { /*这里放的图片是正中间那张*/
	width: 400px;
	height: 300px;
	background: url("img/1.jpg") no-repeat; /*根据自己图片的路径,设置为背景图片*/
	margin: 200px auto;/*这里是为了让section居中对齐*/
	transfrom-style: preserve-3d;
	position: relative;
    transition: all 10s linear;
}
section:hover { /*鼠标移入section盒子中,沿Y轴转360度*/
    transform: rotateY(360deg);
}
section div {
	width: 100%
	height: 100%
	background: url('img/2.jpg') no-repeat;/*暂且将每个div内的背景图片设置成一样,先看看效果*/
	position: absolute;
    top: 0;
    left: 0;
}
section div:nth-child(1) {
	/*background: url('img/3.jpg') no-repeat; 给每个div添加不同图片背景*/
	transform: rotateY(0deg) translateZ(400px);
}
section div:nth-child(2) {
	transform: rotateY(60deg) translateZ(400px);
}
section div:nth-child(3) {
	transform: rotateY(120deg) translateZ(400px);
}
section div:nth-child(4) {
	transform: rotateY(180deg) translateZ(400px);
}
section div:nth-child(5) {
	transform: rotateY(240deg) translateZ(400px);
}
section div:nth-child(16) {
	transform: rotateY(300deg) translateZ(400px);
}

finish!

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 HTML 和 CSS 3D 轮播图片示例,其中包含四张图片 : ```html <!DOCTYPE html> <html> <head> <title>3D轮播图片</title> <style> body { margin: 0; padding: 0; overflow: hidden; } .container { position: relative; width: 100vw; height: 100vh; perspective: 1000px; transform-style: preserve-3d; } .item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; font-size: 5em; color: #fff; background-color: #333; transform-style: preserve-3d; transition: transform 1s; } .item:nth-child(1) { transform: rotateY(0deg) translateZ(500px); } .item:nth-child(2) { transform: rotateY(90deg) translateZ(500px); } .item:nth-child(3) { transform: rotateY(180deg) translateZ(500px); } .item:nth-child(4) { transform: rotateY(270deg) translateZ(500px); } .container:hover .item:nth-child(1) { transform: rotateY(-90deg) translateZ(500px); } .container:hover .item:nth-child(2) { transform: rotateY(0deg) translateZ(500px); } .container:hover .item:nth-child(3) { transform: rotateY(90deg) translateZ(500px); } .container:hover .item:nth-child(4) { transform: rotateY(180deg) translateZ(500px); } </style> </head> <body> <div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> </div> </body> </html> ``` 在这个示例中,我们使用了 CSS3D 变换来实现轮播图片的效果。每个图片元素都有不同的旋转角度和 3D 位移量。鼠标悬停在容器上时,通过修改旋转角度来实现图片的切换效果。 您可以将每个图片元素的内容替换成自己的图片链接或其他内容。如果您需要添加更多的图片,可以复制 `.item` 元素并根据需要修改旋转角度和位移量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值