原生js编写3D轮播图

原生js编写3D版轮播图

在日常开发中,为了开发的快捷性基本上都是把UI组件中的组件拿过来用即可,但是有时可能无法实现我们的需求,这时我们就需要去造轮子。

今天我们用原生的js和css3以及html来编写一个3D轮播图,如下:

第一步,编写轮播图的大致结构

使用html把创建轮播图结构

<div id="box">
<!--
	创建轮播图中图片的容器,注意图片的存放位置
-->
	<div class="item item1">
		<img src="./1.gif" class="swipe_item1">
	</div>
	<div class="item item2">
		<img src="./2.gif" class="swipe_item2">
	</div>
	<div class="item item3">
		<img src="./3.gif" class="swipe_item3">
	</div>
	<div class="item item4">
		<img src="./4.gif" class="swipe_item4">
	</div>
	<div class="item item5">
		<img src="./5.gif" class="swipe_item5">
	</div>
	<div class="item item6">
		<img src="./6.gif" class="swipe_item6">
	</div>
	<div class="item item7">
		<img src="./7.gif" class="swipe_item7">
	</div>
<!--
	创建轮播图切换的按钮,由于我们的轮播图中没有设置点击图片切换的功能,所以使用一个div覆盖在了轮播图之上
-->
	<div id="button" style="position: absolute;top: 0;width: 100%;z-index: 999;">
		<button type="button" onclick="left(event)" class="left switch"></button>
		<button type="button" onclick="right(event)" class="right switch"></button>
	</div>
</div>

使用css将轮播图结构的排列样式优化好

/*设置整个轮播图水平居中*/
body{
	display: flex;
	justify-content: center;
	align-items: center;
}
/*预防按钮点击时出现丑陋的边框*/
*{
	outline: none;
}
#box{
	margin-top: 300px;
	width: 1350px;
	height: auto;
	position: relative;
}
/*设置定位方式,和垂直居中*/
.item{
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
/*设置每个图片容器的位置:横向位置、z轴位置*/
.item1{
	left: 0px;
	z-index: 9;
}
.item2{
	left: 100px;
	z-index: 18;
}
.item3{
	left: 250px;
	z-index: 27;
}
.item4{
	left: 450px;
	z-index: 36;
}
.item5{
	right: 250px;
	z-index: 27;
}
.item6{
	right: 100px;
	z-index: 18;
}
.item7{
	right: 0;
	z-index: 9;
}
/*设置图片的宽度*/
.swipe_item1{
	width: 300px;
}
.swipe_item2{
	width: 350px;
}
.swipe_item3{
	width: 400px;
}
.swipe_item4{
	width: 450px;
}
.swipe_item5{
	width: 400px;
}
.swipe_item6{
	width: 350px;
}
.swipe_item7{
	width: 300px;
}
/*为图片设置阴影,更好的凸显3D效果*/
img{
	box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	border-radius: 10px;
}
/*设置切换按钮的位置,及样式*/
.left{
	left: 100px;
}
.right{
	right: 100px;
}
.switch{
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	width: 50px;
	height: 50px;
	border-radius: 50%;
	border: none;
	background-color: rgba(0,0,0,0.4);
	color: white;
}
.switch:hover{
	background-color: rgba(0,0,0,0.7);
}

至此轮播图的基本结构已经完成,此时的效果如下:
在这里插入图片描述
我们总结一下上面的主要代码:

  1. 为图片容器设置绝对(absolute)定位
  2. 设置图片容器垂直居中
  3. 根据绝对定位为图片容器设置所处位置(横向)和z轴位置
  4. 切换按钮始终悬浮

第二步,使用css3写切换动画

我们以最突出的图片class="item4"为例,来编写动画。
首先,确定to和from,即动画中的“起点”和“终点”,把item4移动到item3的位置上。确定需要变化的属性:

  1. 图片的width
  2. 容器大width
  3. 容器的位置(left)
    查看item3和item4的css代码,如下:
/*容器*/
.item3{
	left: 250px;
	z-index: 27;
}
.item4{
	left: 450px;
	z-index: 36;
}
/*图片*/
.swipe_item3{
	width: 400px;
}
.swipe_item4{
	width: 450px;
}

因此动画css代码如下:

.animation4 {
	animation: item4_animation 1s; 
	animation-fill-mode: forwards; /*设置动画结束后,元素停在终点*/
}

.a4 {
	animation: item4_animation_index 1s;
	animation-fill-mode: forwards;
}

@keyframes item4_animation {
	from {
		width: 450px;
	}

	to {
		width: 400px;
	}
}

@keyframes item4_animation_index {
	from {
		width: 450px;
		left: 450px;
	}

	to {
		width: 400px;
		left: 250px;
	}
}

第三步,改变元素的z-index属性

在第二步中,我们改变了容器和图片的位置和大小,但是z-index并没有改变:我们使用js来改变z-index

document.getElementsClassName("item4").style.zIndex = 27

至此,我们已经将item4通过动画的方式移动到了item3的位置上,效果如下:
在这里插入图片描述
问题:

  1. 移动后的item4覆盖了item3
  2. 如果我们下次再移动item4,item4会自动跳到的初始位置,然后再开始动画

上面的两个问题如何解决呢?
第一个问题,轮播图的最终效果是所有图片一起移动,因此不会出现覆盖问题
第二个问题,我们可以在动画结束之后,通过js把class=“item4”变成class=“item3”(由于我们现在向左移动)

以上都是以item4这一个元素的移动来进行分析

第四步,分析

  1. 使用css为每一个容器的移动,编写动画代码,动画分为左和右
  2. 使用js来改变移动后的容器的 “z-index” 属性
  3. 使用js来切换对应的class,并删除当前的class,根据向左和向右改变class,例:
    向左移动:item4 => item3 | item1 => item7
    向右移动:item4 => item5 | item7 => item1

第五步,js动态设置切换

我们以代码为例:

let flag = true
let autoFlag = true
function left(e){
//只有上一个动画结束后,才能继续切换
	if(!flag){
		return
	}
	flag = false
	//获取页面中所有的item,即所有的图片容器
	let arr = document.getElementsByClassName("item")
	for(let i=1;i<=arr.length;i++){ //由于没有item0,所以从1开始
		let item = document.getElementsByClassName("item"+i)[0] //通过("item"+i)拼接得到对应的class,如item1
		let swipe_item = document.getElementsByClassName("swipe_item"+i)[0]//同理可得swipe_item1
		swipe_item.classList.add("animation"+i) //为图片添加width改变动画
		item.classList.add("a"+i) //为容器添加width和位移改变动画
		//如下:根据不同i,实际是不同的("item"+i),来设置对应的z-index
		setTimeout(()=>{
			switch(i){
				case 7:
					item.style.zIndex = 18
					break
				case 6:
					item.style.zIndex = 27
					break
				case 5:
					item.style.zIndex = 36
					break
				case 4:
					item.style.zIndex = 27
					break
				case 3:
					item.style.zIndex = 18
					break
				case 2:
					item.style.zIndex = 9
					break
			}
		},500) //时间可变,根据需求
		//当动画加载完成后,我们来切换class
		setTimeout(()=>{
		//注意:在切换class时,一定要配合 animation-fill-mode: forwards; 使用
			//我们先把图片和容器的动画class和原始class删除
			swipe_item.classList.remove("animation"+i)
			swipe_item.classList.remove("swipe_item"+i)
			item.classList.remove("a"+i)
			item.classList.remove("item"+i)
			//由于是向左移动,item1要移动到item7的位置上,所以item1需要特殊处理
			if(i != 1){
				swipe_item.classList.add("swipe_item"+(i-1))
				item.classList.add("item"+(i-1))
			}else{
			//如果i = 1 时,该容器(移动的容器和图片)添加swipe_item7和item7到classList中
				swipe_item.classList.add("swipe_item"+7)
				item.classList.add("item"+7)
			}
			flag = true
		},1000) //对应动画执行的时间
	}
}

至此我们便可以随意向左切换:
向右移动的方式参考向左移动的方法,需要注意z-index的设置,和切换class,代码如下:

function right(e){
	if(!flag){
		return
	}
	flag = false
	let arr = document.getElementsByClassName("item")
	for(let i=1;i<=arr.length;i++){
		let item = document.getElementsByClassName("item"+i)[0]
		let swipe_item = document.getElementsByClassName("swipe_item"+i)[0]
		swipe_item.classList.add("r_animation"+i)
		item.classList.add("r_a"+i)
		setTimeout(()=>{
			switch(i){
				case 6:
					item.style.zIndex = 9
					break
				case 5:
					item.style.zIndex = 18
					break
				case 4:
					item.style.zIndex = 27
					break
				case 3:
					item.style.zIndex = 36
					break
				case 2:
					item.style.zIndex = 27
					break
				case 1:
					item.style.zIndex = 18
					break
			}
			
		},500)
		setTimeout(()=>{
			swipe_item.classList.remove("r_animation"+i)
			swipe_item.classList.remove("swipe_item"+i)
			item.classList.remove("r_a"+i)
			item.classList.remove("item"+i)
			if(i != 7){
				swipe_item.classList.add("swipe_item"+(i+1))
				item.classList.add("item"+(i+1))
			}else{
				swipe_item.classList.add("swipe_item"+1)
				item.classList.add("item"+1)
			}
			flag = true
		},1000)
	}
}

自动滚动和鼠标事件:

document.getElementById("box").onmouseover = ()=>{
autoFlag = false
}
document.getElementById("box").onmouseout = ()=>{
	autoFlag = true
}
setInterval(()=>{
	if(autoFlag){
		right()
	}
},5000)

至此,我们所有的功能都以实现

源码

3D轮播图的源码:(css、js、html)
源码链接

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以提供一些指导,但这取决于你有多少 JavaScript 经验。首先,你需要准备一组图片,然后为每张图片创建一个<img>元素。接下来,你需要创建一个 JavaScript 函数来控制图片的显示和隐藏,以及设置一个定时器来自动触发图片切换动作。最后,你可以添加一些按钮来控制轮播图的播放和暂停。 ### 回答2: 轮播图是网页中常见的一种展示多张图片或者内容的效果,下面是使用原生JS编写的一个简单轮播图的示例: HTML结构: ```html <div class="slider"> <ul class="slider-list"> <li><img src="image1.jpg" alt="image1"></li> <li><img src="image2.jpg" alt="image2"></li> <li><img src="image3.jpg" alt="image3"></li> </ul> <button class="prev-btn">上一页</button> <button class="next-btn">下一页</button> </div> ``` CSS样式: ```css .slider { position: relative; width: 400px; height: 300px; overflow: hidden; } .slider-list { list-style: none; margin: 0; padding: 0; width: 100%; height: 100%; display: flex; transition: all 0.5s ease-in-out; } .slider-list li { flex: 0 0 100%; } .prev-btn, .next-btn { position: absolute; top: 50%; transform: translateY(-50%); } .prev-btn { left: 10px; } .next-btn { right: 10px; } ``` JavaScript代码: ```javascript window.addEventListener("load", function() { var sliderList = document.querySelector(".slider-list"); var prevBtn = document.querySelector(".prev-btn"); var nextBtn = document.querySelector(".next-btn"); var imageWidth = sliderList.firstElementChild.clientWidth; var currentImageIndex = 0; nextBtn.addEventListener("click", function() { if (currentImageIndex < sliderList.children.length - 1) { currentImageIndex++; var translateXValue = -currentImageIndex * imageWidth; sliderList.style.transform = "translateX(" + translateXValue + "px)"; } }); prevBtn.addEventListener("click", function() { if (currentImageIndex > 0) { currentImageIndex--; var translateXValue = -currentImageIndex * imageWidth; sliderList.style.transform = "translateX(" + translateXValue + "px)"; } }); }); ``` 以上代码实现了一个简单的轮播图,点击“上一页”和“下一页”按钮可以切换图片。代码通过监听按钮的点击事件,在点击事件中改变轮播图的`translateX`属性来实现滑动效果。 ### 回答3: 轮播图是网页中常见的组件之一,可以用来展示多张图片或内容的切换效果。下面是用原生JS写一个简单的轮播图的步骤: 1. HTML结构: 首先,在HTML中创建一个包含轮播图的容器,用来展示图片。可以使用`<div>`元素,并为其设置一个唯一的id,例如`<div id="slider">`。在容器内部,创建一个`<ul>`元素,并为其添加一个唯一的id,例如`<ul id="slider-list">`。然后,再在`<ul>`中创建多个`<li>`元素,每个`<li>`元素代表一个要展示的图片或内容。 2. CSS样式: 使用CSS样式来设置轮播图容器`<div>`和内部的`<ul>`元素的样式,例如设置宽度、高度、背景色等。还可以设置`<li>`元素的样式,例如设置宽度、高度、定位等,来控制图片或内容的展示方式。 3. JS逻辑: 使用原生JS来实现轮播图的逻辑部分。 - 首先,获取轮播图容器和内部`<ul>`元素的引用,可以使用`document.getElementById()`方法获取元素的引用。 - 获取所有的`<li>`元素的引用,并存储在一个数组中。 - 定义变量来存储当前展示的图片的索引,默认为0。 - 定义一个函数来切换图片。可以使用`style.display`属性来控制`<li>`元素的显示或隐藏状态。切换到下一张图片时,将当前展示的图片隐藏,将下一张图片显示。切换到最后一张图片时,再切换到第一张图片。 - 使用`setInterval()`函数来定时调用切换图片的函数,实现自动播放的效果。 4. 绑定事件: 可以为轮播图容器添加鼠标移入和移出的事件,分别停止和开始自动播放。 以上是用原生JS实现一个简单的轮播图的大致步骤,通过编写、测试和调试代码,可以使轮播图正常运行并展示多张图片或内容的切换效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值