移动轮播 横屏切换效果

达成移动效果有两种方式 推荐学习第一种 如果看不懂的话可以把 .box 的 overflow: hidden; 去掉再看效果 就比较好理解

<!doctype html>
<html lang="en">

	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
				list-style: none;
			}
			
			body {
				position: relative;
			}
			
			.box {
				position: absolute;
				width: 200px;
				height: 200px;
				left: 50%;
				overflow: hidden;
			}
			
			.box ul {
				position: absolute;
				left: -200px;
				display: flex;
			}
			
			.box li {
				width: 200px;
				height: 200px;
			}
			
			.but {
				width: 200px;
				position: absolute;
				left: 50%;
				top: 200px;
			}
			
			button {
				width: 50px;
				height: 50px;
			}
		</style>
	</head>
  
	<body>
		<div class="box">
			<ul>
				<li style="background: red;">4</li>
				<li style="background:pink;">1</li>
				<li style="background: blue;">2</li>
				<li style="background: black;">3</li>
				<li style="background: red;">4</li>
				<li style="background: pink;">1</li>

			</ul>
		</div>
		<div class="but">
			<button><</button>
			<button>></button>

		</div>
		<script type="text/javascript">
			var but = document.getElementsByTagName("button")
			var oul = document.getElementsByTagName('ul')[0]
			var li = document.getElementsByTagName("li")[0]
			var win = li.offsetWidth
			console.log(li.offsetWidth)
			var a = 0
			var k = true
			but[1].addEventListener("click", function() {
				if (k) { 
					k = false
					a++
					oul.style.left = -(a + 1) * win + "px"
					oul.style.transition = "1s ease"
					console.log(oul.style.left)
					setTimeout(function() {
						k = true
						if (oul.style.left == "-1000px") {
							a = 0
							oul.style.left = -1 * win + "px"
							oul.style.transition = ""
						}
					}, 1200);
				}
				setTimeout(k, 1000)
			})
			but[0].addEventListener("click", function() {
				console.log(k)
				if (k) {
					k = false
					a--
					//	  		console.log(a)
					oul.style.left = -(a + 1) * win + "px"
					oul.style.transition = "1s ease"
					setTimeout(function() {
						k = true
						if (oul.style.left == "0px") {
							a = 3
							oul.style.left = -4 * (win) + "px"
							oul.style.transition = "none"
						}
						alert
					}, 1200);
				}
				setTimeout(k, 1000)
			})
		</script>
	</body>

</html>

简单理解的话 就是加5个图片 超出DIV的地方隐藏 然后改变UL的位置 形成一个横向切换的效果

<!doctype html>
<html lang="en">

	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
				list-style: none;
			}
			
			body {
				position: relative;
			}
			
			.box {
				position: absolute;
				width: 200px;
				height: 200px;
				left: 50%;
				overflow: hidden;
			}
			
			.box ul {
				position: absolute;
				left: -200px;
				display: flex;
			}
			
			.box li {
				width: 200px;
				height: 200px;
			}
			
			.but {
				width: 200px;
				position: absolute;
				left: 50%;
				top: 200px;
			}
			
			button {
				width: 50px;
				height: 50px;
			}
		</style>
	</head>
  
	<body>
		<div class="box">
			<ul>
				<li style="background: red;">4</li>
				<li style="background:pink;">1</li>
				<li style="background: blue;">2</li>
				<li style="background: black;">3</li>
				<li style="background: red;">4</li>
				<li style="background: pink;">1</li>

			</ul>
		</div>
		<div class="but">
			<button><</button>
			<button>></button>

		</div>
		<script type="text/javascript">
			var but = document.getElementsByTagName("button")
			var oul = document.getElementsByTagName('ul')[0]
			var li = document.getElementsByTagName("li")[0]
			var win = li.offsetWidth
			console.log(li.offsetWidth)
			var a = 0
			var k = true
			but[1].addEventListener("click", function() {
				if (k) { 
					k = false
					a++
					oul.style.left = -(a + 1) * win + "px"
					oul.style.transition = "1s ease"
					console.log(oul.style.left)
					setTimeout(function() {
						k = true
						if (oul.style.left == "-1000px") {
							a = 0
							oul.style.left = -1 * win + "px"
							oul.style.transition = ""
						}
					}, 1200);
				}
				setTimeout(k, 1000)
			})
			but[0].addEventListener("click", function() {
				console.log(k)
				if (k) {
					k = false
					a--
					//	  		console.log(a)
					oul.style.left = -(a + 1) * win + "px"
					oul.style.transition = "1s ease"
					setTimeout(function() {
						k = true
						if (oul.style.left == "0px") {
							a = 3
							oul.style.left = -4 * (win) + "px"
							oul.style.transition = "none"
						}
						alert
					}, 1200);
				}
				setTimeout(k, 1000)
			})
		</script>
	</body>

</html>

这是第二种方法 简易理解就是让每个已经切换的图片跳到ul的末尾
但是他的索引值会改变不是很好找
推荐第一个方法 简单易懂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现轮播图飞屏效果,需要用到CSS3动画和Vue的过渡动画。具体实现步骤如下: 1. 给轮播图容器添加一个overflow:hidden的样式,隐藏超出容器的部分。 2. 使用Vue的过渡动画,在每次切换轮播图时,给当前轮播图和下一个轮播图添加不同的过渡动画class,从而实现轮播图的飞屏效果。 3. 在CSS中定义过渡动画class,为轮播图添加动画效果。可以使用CSS3中的translate3d和transition属性,实现从左到右的滑动动画效果。 4. 最后,设置轮播图容器的宽度和高度,以适应不同的屏幕尺寸和轮播图大小。 以下是一个基本的轮播图飞屏效果的Vue组件代码: ```html <template> <div class="carousel-container"> <transition-group tag="ul" name="carousel"> <li v-for="(image, index) in images" :key="index" :style="{ backgroundImage: 'url(' + image + ')' }"></li> </transition-group> </div> </template> <script> export default { data() { return { images: [ 'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg' ], currentIndex: 0, timer: null } }, mounted() { this.startCarousel() }, methods: { startCarousel() { this.timer = setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.images.length }, 3000) } } } </script> <style scoped> .carousel-container { position: relative; width: 100%; height: 400px; overflow: hidden; } .carousel-container ul { position: absolute; top: 0; left: 0; width: 100%; height: 100%; list-style: none; margin: 0; padding: 0; } .carousel-container li { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-size: cover; background-position: center; will-change: transform; } .carousel-enter-active, .carousel-leave-active { transition: transform 0.5s ease; } .carousel-enter, .carousel-leave-to { transform: translate3d(100%, 0, 0); } .carousel-enter-to, .carousel-leave { transform: translate3d(-100%, 0, 0); } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值