首页 > 其他分享 >js轮播图功能完善版

js轮播图功能完善版

时间:2024-08-18 14:27:10浏览次数:6  
标签:function 功能完善 轮播 imgList js flag var thisIndex true

功能如下:

1.鼠标移出自动轮播

2.鼠标移入停止轮播

3.鼠标移入时监听鼠标上下滚轮移动

4.小圆点导航

代码:

<!DOCTYPE html>
<html lang="en">
 
<head>
	<meta charset="UTF-8">
	<title>轮播图</title>
	<!-- <link rel="stylesheet" href="style.css"> -->
	<style>
		* {
			margin: 0px;
			padding: 0px;
		}
 
		.banner {
			width: 600px;
			margin: auto;
			border: 10px solid greenyellow;
			height: 350px;
			position: relative;
			overflow: hidden;
		}
 
		.imgList {
			list-style: none;
			/* width: 2480px; */
			position: absolute;
			/* left:-620px; */
		}
 
		.imgList img {
			width: 600px;
			height: 350px;
		}
 
		.imgList li {
			float: left;
			margin-right: 20px;
		}
 
		.circle {
			position: absolute;
			bottom: 15px;
			left: 50%;
			transform: translateX(-50%);
		}
 
		.circle a {
			width: 15px;
			height: 15px;
			background: yellow;
			display: block;
			border-radius: 50%;
			opacity: .5;
			float: left;
			margin-right: 5px;
			cursor: pointer;
		}
 
		.circle a.hover {
			background: black;
			opacity: .8;
		}
	</style>
</head>
 
<body>
	<div class="banner">
		<ul class="imgList">
			<li><img src="../1.png" alt=""></li>
			<li><img src="../2.jpg" alt=""></li>
			<li><img src="../3.jpg" alt=""></li>
			<li><img src="../4.jpg" alt=""></li>
		</ul>
		<div class="circle">
			<!-- <a href=""></a> 
		<a href=""></a> 
		<a href=""></a> 
		<a href=""></a>  -->
		</div>
	</div>
	<script>
		window.onload = function () {
			var imgList = document.querySelector('.imgList');
			var circle = document.querySelector('.circle');
			var thisIndex = 0;
			var imgListLi = imgList.children;
			var circleA = circle.children;
			var flag = true;
            var lunbovalid = true;
			imgList.style.width = imgList.children.length * 620 + 'px';
			for (var i = 0; i < imgList.children.length; i++) {
				var aNode = document.createElement('a');
				aNode.setAttribute('index', i);	//设置自定义属性
				if (i == 0) {
					aNode.className = 'hover';
				}
				circle.appendChild(aNode);
			}
			circle.addEventListener('click', function (e) {
				if (flag) {
					flag = false;
					// console.log(e.target);
					if (e.target.nodeName != 'A') {
						return false;
					}
					thisIndex = e.target.getAttribute('index');
					// imgList.style.left = -thisIndex * 620 + 'px';
					slow(imgList, -thisIndex * 620, function () {
						flag = true;
					});
					circleChange();
				}
			})
			function antoChange() {
				setInterval(function () {
					if (flag == true && lunbovalid == true) {
						flag = false;
						if (thisIndex >= circleA.length) {
							thisIndex = 0;
						}
						slow(imgList, -thisIndex * 620, function () {
							flag = true;
						});
						circleChange();
						thisIndex++;
					}
			    	}, 2000);
			}
			function circleChange() {
				for (var i = 0; i < circleA.length; i++) {
					circleA[i].className = '';
				}
				circleA[thisIndex].className = 'hover';
			}
			function slow(obj, target, callback) {
				obj.myInter = setInterval(function () {
					var offsetLeft = obj.offsetLeft;
					var num = (target - offsetLeft) / 10;
					num > 0 ? num = Math.ceil(num) : num = Math.floor(num);
					if (offsetLeft == target) {
						clearInterval(obj.myInter);
						callback && callback();
					} else {
						obj.style.left = offsetLeft + num + 'px';
					}
				}, 10)
			}

            var demovalid = true

            function demo(e){
            if(e.wheelDelta){
                if(e.wheelDelta > 0 && demovalid == true){
                    demovalid = false
                    setTimeout(demo2,500)
                }
                if(e.wheelDelta < 0 && demovalid == true){
                    demovalid = false
                    setTimeout(demo1,500)
                }
            }
        }
            
            function demo1(){
                    console.log("down");
                    flag = false
                    thisIndex++
                    if (thisIndex >= circleA.length) {
							thisIndex = 0;
						}
                    slow(imgList, -thisIndex * 620, function () {
							flag = true;
						});
                    circleChange();

                    demovalid = true
            }

            function demo2(){
                    console.log("up");
                    flag = false
                    thisIndex--
                    if (thisIndex >= circleA.length) {
							thisIndex = 0;
						}
                    slow(imgList, -thisIndex * 620, function () {
							flag = true;
						});
                    circleChange();

                    demovalid = true
            }


            document.querySelector(".banner").addEventListener("mouseenter",function(){
                lunbovalid = false
            })

            document.querySelector(".banner").addEventListener("mouseleave",function(){
                lunbovalid = true
            })

            document.querySelector(".banner").addEventListener("mousewheel",demo)

            antoChange();

		}
            
            
	</script>
</body>
</html>

标签:function,功能完善,轮播,imgList,js,flag,var,thisIndex,true
From: https://blog.csdn.net/qq_33607094/article/details/141299458

相关文章

  • js数组排序
    快速排序:每次循环使用数组中间位置的值按照大小,将数组分成左右两个部分,使用递归,分别对左右两个部分执行分割操作1//快速排序2functionquickSort(arr){3if(arr.length<=1){4returnarr;5}6constmidIndex=Math.floor(arr.length......
  • UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaf...--web逆向execjs读取j
    背景做web逆向的时候我们通常是纯python模拟js思路或js+python直接逆向,第二种情况下我们要先获取到想要的js代码,js文件内测试接口后,通过python中的`execjs`模块实现相应接口的调用。通常我们会直接从网站扣下需要的代码(分析后硬扣或通过webpack),然后稍加删改和补环境就直接使用......
  • 723java jsp SSM医院住院管理系统(源码+文档+运行视频+讲解视频)
    项目技术:SSM+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows7/8/10......
  • 721java jsp SSM办公管理系统通讯录考勤打卡日程信息管理(源码+文档+PPT+开题+任务书+
    项目技术:SSM+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows7/8/10......
  • 642java jsp SSM疫情防控管理系统(源码+文档+任务书+运行视频+讲解视频)
     项目技术:SSM+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows7/8/1......
  • 698java jsp SSM网络办公系统共享文件会议信息工作日程管理(源码+文档+运行视频+讲解视
     项目技术:SSM+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows7/8/1......
  • js深拷贝
    功能如下:WeakMap解决循环引用问题支持拷贝的数据类型:基本数据类型:number、string、boolean、undefined、null、symbol引用数据类型:Date、RegExp、Function、Object、Map、Set代码实现:1functionclone(obj,map=newWeakMap){2if(map.has(obj))......
  • JSP基于JAVAWEB医院挂号系统jep98(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统功能:用户,医生,护士,科室信息,预约时间段,出诊信息,预约挂号,门诊病历,医生打卡,护士打卡,就诊评价技术要求:开发语言:JSP前端使用:HTML5,CSS,JSP动态网页技......
  • JSP基于JavaWeb的CRM系统684y6程序+源码+数据库+调试部署+开发环境
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统功能:公告信息,部门,职位,用户,客户分组,客户信息,拜访记录,商机信息,订单信息技术要求:开发语言:JSP前端使用:HTML5,CSS,JSP动态网页技术后端使用SpringBoo......
  • JSP惠民一网通nwzyz--(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统功能:户主,新生入证明,迁入户籍,死亡证明,迁出户籍,本地人员流动,外来人员流动,民生业务,评分信息,应急电话开题报告内容一、项目背景随着信息化技术的飞速......