三种js轮播实现方式详解(看一遍就会)

这篇具有很好参考价值的文章主要介绍了三种js轮播实现方式详解(看一遍就会)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

js轮播的三种实现方式

1、替换scr(入门级)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.wrapper{
				width: 600px;
				height: 350px;
				margin: 0 auto;
				position: relative;
			}
			img{
				width: 100%;
				height: 100%;
			}
			.btn{
				width:150px;
				display: flex;
				justify-content: space-around;
				position: absolute;
				left:225px;
				bottom:10px;
			}
			
			.btn span{
				display: block;
				width:15px;
				height:15px;
				border: 3px solid white;
				border-radius: 50%;
			}
			.active{
				background-color: red;
			}
		</style>
	</head>
	<body>
		<div class="wrapper">
			<img src="./imgs/1.png" alt="">
			<div class="btn">
				<span class="active"></span>
				<span></span>
				<span></span>
				<span></span>
				<span></span>
			</div>
		</div>
		<script>
			var _img=document.querySelector("img");
			var _wrapper=document.querySelector(".wrapper");
			var _spots=document.querySelectorAll(".btn span");
			var imgs=['./imgs/1.png','./imgs/2.png','./imgs/3.png','./imgs/4.png','./imgs/5.png']
			
			//自动播放
			autoplay();
			var id;
			var index=1;
			function autoplay(){
				id=setInterval(function(){
					_img.src=imgs[index];
					//控制小圆点
					spots();
					index++;
					if(index>=5){
						index=0;
					}
					
				},2000)
				
			}
			
			//小圆点变化
			function spots(){
				for (var i = 0; i < _spots.length; i++) {
					if(i==index){
						_spots[i].className="active"
					}else{
						_spots[i].className=""
					}
				}
			}
			
			//悬浮时停止
			_wrapper.onmouseover=function(){
				clearInterval(id);
			}
			//离开时继续
			_wrapper.onmouseout=function(){
				autoplay();
			}
		</script>
	</body>
</html>

效果图:
三种js轮播实现方式详解(看一遍就会)

2、滚动条(进阶版)

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.wrapper {
			width: 900px;
			height: 350px;
			overflow: hidden;
			overflow: auto;
			margin: 0 auto;
		}

		img {
			width: 900px;
			height: 350px;
		}

		.contain {
			display: flex;
			width: 5400px;
		}
	</style>
</head>

<body>
	<div class="wrapper">
		<div class="contain">
			<img src="./img/10011.jpg" />
			<img src="./img/10012.jpg" />
			<img src="./img/10013.jpg" />
			<img src="./img/10014.jpg" />
			<img src="./img/10015.jpg" />
			<img src="./img/10011.jpg" />
		</div>
	</div>
	<script>
		var _wrapper = document.querySelector(".wrapper");
		var index = 0;
		var num = setInterval(function () {
			//滚动一张
			var moveLength = 0; //用标识是否走完一张
			var id = setInterval(function () {
				_wrapper.scrollLeft += 12;
				moveLength += 12
				if (moveLength >= 900) {
					clearInterval(id);
				}
			}, 20) //一张需要2250毫秒
			index++;
			// 走完所有下标和滚动都要从0开始
			if (index >= 6) {
				index = 0;
				_wrapper.scrollLeft = 0;
			}

		}, 3000)

	</script>
</body>

</html>

效果图:
三种js轮播实现方式详解(看一遍就会)

3、定位(豪华版)

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.wrapper {
			width: 900px;
			height: 350px;
			overflow: hidden;
			margin: 0 auto;
			position: relative;
		}

		img {
			width: 900px;
			height: 350px;
		}

		.contain {
			display: flex;
			width: 4500px;
			position: absolute;
			left: 0;
			top: 0;
		}

		.btn {
			width: 150px;
			display: flex;
			justify-content: space-around;
			position: absolute;
			left: 375px;
			bottom: 10px;
		}

		.btn span {
			display: block;
			width: 15px;
			height: 15px;
			border: 3px solid white;
			border-radius: 50%;
		}

		.wrapper a {
			text-decoration: none;
			font-size: 50px;
			color: red;
			position: absolute;
			top: 35%;
		}

		.wrapper a:nth-of-type(1) {
			left: 10px;

		}

		.wrapper a:nth-of-type(2) {
			right: 10px;
		}

		.active {
			background-color: red;
		}
	</style>
</head>

<body>
	<div class="wrapper">
		<div class="contain">
			<img src="./img/10011.jpg" />
			<img src="./img/10012.jpg" />
			<img src="./img/10013.jpg" />
			<img src="./img/10014.jpg" />
			<img src="./img/10015.jpg" />
		</div>
		<div class="btn">
			<span class="active"></span>
			<span></span>
			<span></span>
			<span></span>
			<span></span>
		</div>
		<a href="javascript:void(0);">&lt;</a>
		<a href="javascript:void(0);">&gt;</a>
	</div>
	<script>
		var _wrapper = document.querySelector(".wrapper");
		var _contain = document.querySelector(".contain");
		var _btn = document.querySelector(".btn");
		//下一张按钮
		var _nextPic = document.querySelector(".wrapper a:nth-of-type(2)");
		// 上一张按钮
		var _prevPic = document.querySelector(".wrapper a:nth-of-type(1)");

		var _btn = document.querySelector(".btn");
		//获取所有小圆点
		var _spots = document.querySelectorAll(".btn span");


		// 下一张
		_nextPic.onclick = function () {
			next_pic();
		}
		var index = 0;
		function next_pic() {
			_contain.style.left = _contain.offsetLeft - 900 + "px";
			if (_contain.offsetLeft <= -4500) {
				_contain.style.left = 0;
			}
			index++;
			if (index > 4) {
				index = 0;
			}
			spots();
		}

		// 上一张
		_prevPic.onclick = function () {
			prev_pic();
		}
		function prev_pic() {
			_contain.style.left = _contain.offsetLeft + 600 + "px";
			if (_contain.offsetLeft > 0) {
				_contain.style.left = -3600 + "px";
			}
			index--;
			if (index < 0) {
				index = 4;
			}
			spots();
		}

		//自动轮播
		autoplay();
		var id;
		function autoplay() {
			id = setInterval(function () {
				next_pic();
			}, 2000)
		}

		//小圆点变化
		function spots() {
			for (var i = 0; i < _spots.length; i++) {
				if (i == index) {
					_spots[i].className = "active"
				} else {
					_spots[i].className = ""
				}
			}
		}

		//悬停控制
		_wrapper.onmouseover = function () {
			clearInterval(id);
		}
		_wrapper.onmouseout = function () {
			autoplay();
		}


		//悬浮小圆点更新图片
		_btn.onmouseover = function (event) {
			//获取悬浮的小圆点
			var target = event.srcElement || event.target;
			if (target.nodeName == 'SPAN') {
				//查询小圆点下标
				var n = Array.from(_spots).findIndex(function (tag) {
					return tag == target
				})
				//更新下标
				index = n;
				//更新位移
				_contain.style.left = -900 * n + "px";
				//更新颜色
				spots();
			}
		}

	</script>
</body>

</html>

效果图:
三种js轮播实现方式详解(看一遍就会)文章来源地址https://www.toymoban.com/news/detail-469328.html

到了这里,关于三种js轮播实现方式详解(看一遍就会)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • List 集合遍历过程中删除元素。这个坑踩一遍就不要再踩了!

    作为一名后端开发,不管采用什么语言 使用 List 集合的频率都非常高。 对 List 集合的遍历和遍历中操作数据也是家常便饭。 我从我使用的过程中对于此问题的思考与实践形成记录,与大家交流。有不对的地方,恳请大家指正。 List 集合的遍历有很多种方式,每一种遍历方式

    2024年02月12日
    浏览(33)
  • js实现base64、url和blob之间相互转换的三种方式

    Blob对象表示一个不可变、原始数据的类文件对象,Blob表示的不一定是JavaScript原生格式的数据,下面这篇文章主要给大家介绍了关于js实现base64、url和blob之间相互转换的三种方式 url 转 base64 Blob 转 url Blob 转 base64 base64 转Blob base64 转 url 一般来说前端展示图片会通过三种方式:

    2024年02月03日
    浏览(37)
  • 【网页前端实现多张图片轮播或者切换】三种方法实现

    然后我们通过css里面的一些设置将图片样式大小进行修饰。 最后用js动画实现三种方式的图片轮播效果:自动播放,下方按钮点击切换图片以及左右两侧的点击切换上下两张图片。 最后希望能帮到大家,这是一个网页中小小的一个功能啊,不理解的朋友可以留言或者私信,有

    2024年02月11日
    浏览(29)
  • js中三种URI编码方式比较

    数据传递常需要编码后传递,接收还需反编译,定义url: var url = \\\"https://www.cnblogs.com/?username=\\\'小森森\\\'password=\\\'666666\\\'\\\"; https%3A//www.cnblogs.com/%3Fusername%3D%27%u5C0F%u68EE%u68EE%27%26password%3D%27666666%27 https%3A%2F%2Fwww.cnblogs.com%2F%3Fusername%3D\\\'%E5%B0%8F%E6%A3%AE%E6%A3%AE\\\'%26password%3D\\\'666666\\\' https://www.cnblogs

    2024年02月04日
    浏览(34)
  • 原生 JS 实现一个简单轮播图

    在动手实现轮播图之前,我们先来明确一下要实现的效果。 默认自动轮播,每隔4秒切换一张图片 鼠标点击任一个小圆点即可切换到对应的图片 鼠标移入轮播区域时,两侧出现切换图片的按钮,点击按钮分别切换到上(下)一张图片 因此,轮播图可以分为三个主要部分,首

    2024年02月09日
    浏览(46)
  • js对url进行编码解码(三种方式)

    目录 前言 一、为什么要编码 二、需要编码的字符 三、编码的三种方式 第一种:escape和 unescape 第二种:encodeURI 和 decodeURI 第三种: encodeURIComponent 和 decodeURIComponent 三、总结 我们在项目开发中用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发

    2024年03月10日
    浏览(41)
  • js实现左右切换轮播图效果

    实现效果: 自动向右循环播放 鼠标悬停,移出继续播放 点击向右按钮,跳到下一张 点击向左按钮,跳到上一张 保证图片的过渡效果完整呈现后,才能跳到下一张 底部圆点随图片位置切换激活状态 实现思路: 把第一张图片复制到最后一张 当播放到最后一张时,无缝切换到

    2024年01月20日
    浏览(43)
  • 使用vant+video.js实现轮播图图片和视频轮播播放

    先上效果图 1. 安装 2. 在需要用到的页面引入 3. 具体页面使用 假设传给子组件的数组结构 按照步骤来使用,其他地方视频播放情况都还好,至少安卓是好的,只是点击播放和暂停时候,ios需要点击多下才能触发点击事件; 然后以为是video.js插件可能没更新,ios版本迭代超过

    2023年04月08日
    浏览(30)
  • JS常用插件 Swiper插件 实现轮播图

    Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架 中文官网地址: https://www.swiper.com.cn/ 点击查看 Swiper演示 ,里面的功能和样式十分丰富,根据自己的需求选择 中文教程 中详细介绍了如何使用Swiper API文档 中介绍了各个模块以及参数的详细信息,遇到不明白的参数可以

    2024年02月01日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包