• 【jQuery】全功能轮播图的实现(本文结尾也有javascript版)


    轮播图

    在这里插入图片描述

    1. 图片自动切换(定时器);
    2. 鼠标悬停在图片上图片不切换(清除定时器)
    3. 鼠标悬停在按钮上时显示对应的图片(鼠标悬停事件)
    4. 鼠标悬停在图片上是现实左右箭头
    5. 点击左键切换到上一张图片,但图片为第一张时,点击切换到最后一张图片
    6. 点击右键切换到下一张图片,但图片为最后一张时,点击切换到第一张图片

    jquery版本3.4.1

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<link rel="stylesheet" href="css/reset.css" />
    		<style>
    			.big{
    				
    				position: relative;
    				width: 450px;
    				height: 270px;
    			}
    			#ulimg{
    				width: 450px;
    				height: 270px;
    				overflow: hidden;
    			}
    			#ulimg li{float: left;}
    			#ulimg ul{
    				width: 500%;
    				height: 270px;
    				display: block;
    				clear: both;
    			}
    			#arrows{
    				position: absolute;
    				top:90px;
    				z-index: 5;
    				width: 100%;
    			}
    			.arrow{
    				width: 30px;
    				height: 60px;
    				background-color: #191E2B;
    				font-size: 25px;
    				text-align: center;
    				line-height: 60px;
    				border-radius: 10px;
    				color: #FFFFFF;
    				float: left;
    			}
    			#rarrow{
    				float: right;
    				
    			}
    			.big #arrows{
    				position: absolute;
    				z-index: -1;
    				transition: z-index 1s;
    				opacity: 1;
    				cursor: pointer;
    			}
    			.btn{
    				width: 20px;
    				height: 20px;
    				background-color: whitesmoke;
    				font-size: 18px;
    				text-align: center;
    				float: left;
    				margin: 10px;
    				border-radius: 5px;
    			}
    			#btns{
    				position: absolute;
    				z-index: 5;
    				top: 220px;
    				left: 220px;
    				cursor: pointer;
    				
    			}
    			#btns .btn2{
    				background: orange;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="big">
    		<div id="ulimg">
    		<ul>
    			<li class="now"><img  src="img/01.jpg" alt=""/></li>
    			<li><img src="img/02.jpg" alt=""/></li>
    			<li><img src="img/03.jpg" alt=""/></li>
    			<li><img src="img/04.jpg" alt=""/></li>
    			<li><img src="img/05.jpg" alt=""/></li>
    		</ul>
    		</div>
    		<div id="arrows">
    		<div class="arrow"><</div>
    		<div class="arrow" id="rarrow">></div>
    		</div>
    		<div id="btns">
    		<div class="btn btn2">1</div>
    		<div class="btn">2</div>
    		<div class="btn">3</div>
    		<div class="btn">4</div>
    		<div class="btn">5</div>
    		</div>
    		</div>
    		<script type="text/javascript" src="js/jquery-3.4.1.min.js" ></script>
    		<script>
    			var timer=setInterval(change,3000);
    			var index=1;
    			function change(){
    				index++;
    				if(index>=6){
    					index=1;
    				}
    				$(".btn").removeClass("btn2");
    				$(".btn").eq(index-1).addClass("btn2");
    				$("#ulimg ul").animate({
    					"marginLeft":"-450px"
    				},1000,function(){
    					$("#ulimg li:eq(0)").appendTo($(this));
    					$(this).css("marginLeft",0);
    				})
    			}
    			function lchange(){
    				$(".btn").removeClass("btn2");				
    				index--;
    				if(index<=0){
    					index=5;
    				}
    				$(".btn").eq(index-1).addClass("btn2");
    				$("#ulimg li:eq(4)").prependTo($("#ulimg ul"));
    			}
    			function rchange(){
    				$(".btn").removeClass("btn2");	
    				index++;
    				if(index>=6){
    					index=1;
    				}
    				$(".btn").eq(index-1).addClass("btn2");
    				$("#ulimg li:eq(0)").appendTo($("#ulimg ul"));
    			}
    			$(".arrow:eq(0)").click(function(){
    				lchange();
    			});
    			$(".arrow:eq(1)").click(function(){
    				rchange();
    			});
    			$(".big").mouseover(function(){
    				clearInterval(timer);
    				$("#arrows").css("zIndex",3);
    			})
    			$(".big").mouseout(function(){
    				clearInterval(timer);
    				timer=setInterval(change,3000);
    				$("#arrows").css("zIndex",-1);
    			})
    			$(".btn").click(function(){
    				$(".btn").removeClass("btn2");
    				$(this).addClass("btn2");
    				var now=$(this).html();
    //				console.log(now);
    //				console.log(index);
    				var btw=now-index;
    				if(btw<0){
    					btw=Math.abs(btw);
    					for(let i=0;i<btw;i++){
    						lchange();
    					}
    				}else if(btw>0){
    					for(let i=0;i<btw;i++){
    						rchange();
    					}
    				}else{
    					
    				}
    			})
    		</script>
    	</body>
    </html>
    
    

    javascript版本

    html+css

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title>轮播图</title>
    		<link rel="stylesheet" href="css/reset.css" />
    		<style>
    			.big{
    				position: absolute;
    				width: 450px;
    				height: 270px;
    			}
    			#ulimg li{
    				position: absolute;
    				z-index: 1;
    				opacity: 0;
    				transition: opacity 1s; 
    			}
    			#ulimg .now{
    				position: absolute;
    				z-index: 2;
    				opacity: 1;
    			}
    			#arrows{
    				position: absolute;
    				top:90px;
    				z-index: 3;
    				width: 100%;
    			}
    			.arrow{
    				width: 30px;
    				height: 60px;
    				background-color: #191E2B;
    				font-size: 25px;
    				text-align: center;
    				line-height: 60px;
    				border-radius: 10px;
    				color: #FFFFFF;
    				float: left;
    			}
    			#rarrow{
    				float: right;
    				
    			}
    			.big #arrows{
    				position: relative;
    				z-index: 1;
    				transition: z-index 1s;
    				opacity: 0;
    				cursor: pointer;
    			}
    			.btn{
    				width: 20px;
    				height: 20px;
    				background-color: whitesmoke;
    				font-size: 18px;
    				text-align: center;
    				float: left;
    				margin: 10px;
    				border-radius: 5px;
    			}
    			#btns{
    				position: relative;
    				z-index: 3;
    				margin-top: 220px;
    				margin-left: 220px;
    				cursor: pointer;
    				
    			}
    			#btns .btn2{
    				background: orange;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="big">
    		<ul id="ulimg">
    			<li class="now"><img  src="img/01.jpg" alt=""/></li>
    			<li><img src="img/02.jpg" alt=""/></li>
    			<li><img src="img/03.jpg" alt=""/></li>
    			<li><img src="img/04.jpg" alt=""/></li>
    			<li><img src="img/05.jpg" alt=""/></li>
    		</ul>
    		<div id="arrows">
    		<div class="arrow"><</div>
    		<div class="arrow" id="rarrow">></div>
    		</div>
    		<div id="btns">
    		<div class="btn btn2">1</div>
    		<div class="btn">2</div>
    		<div class="btn">3</div>
    		<div class="btn">4</div>
    		<div class="btn">5</div>
    		</div>
    		</div>
    		<script src="js/imgs.js"></script>
    	</body>
    </html>
    
    

    js

    var imgs=document.querySelectorAll("#ulimg li");
    var big=document.querySelector(".big");
    var index=0;
    var timer=setInterval(change,1000);
    var arrows=document.getElementById("arrows");
    var arrowsbtns=document.getElementsByClassName("arrow");
    var btns=document.getElementsByClassName("btn");
    function resetbtns(){
    	for(var i=0;i<imgs.length;i++){
    		imgs[i].className="";
    		btns[i].className="btn";
    		btns[i].setAttribute("onclick","changeimg(this)");
    	}
    }
    function change(){
    	resetbtns();
    	index++;
    	if(index>=5){
    		index=0;
    	}
    	imgs[index].className="now";
    	btns[index].className="btn btn2";
    }
    big.onmouseover=function stop(){
    	clearInterval(timer);
    	arrows.style.zIndex=3;
    	arrows.style.opacity=1;
    }
    big.onmouseout=function start(){
    	clearInterval(timer);
    	timer=setInterval(change,1000);
    	arrows.style.zIndex=1;
    	arrows.style.opacity=0;
    }
    arrowsbtns[0].onclick=function leftChange(){
    	resetbtns();
    	index--;
    	if(index<=-1){
    		index=4;
    	}
    	imgs[index].className="now";
    	btns[index].className="btn btn2";
    }
    arrowsbtns[1].onclick=function rightChange(){
    	resetbtns();
    	index++;
    	if(index>=5){
    		index=0;
    	}
    	imgs[index].className="now";
    	btns[index].className="btn btn2";
    }
    
    function changeimg(obj){
    	resetbtns();
    	index=obj.innerHTML*1-1;
    	imgs[index].className="now";
    	btns[index].className="btn btn2";
    }
    
    
  • 相关阅读:
    POJ 1811 大整数素数判断 Miller_Rabin
    hdu 4940 无源汇有上下界最大流
    hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化
    hdu 4971 多校10最大权闭合图
    hdu 4888 最大流给出行列和求矩阵
    poj 3621最优比例生成环(01分数规划问题)
    poj 2728 最优比例生成树(01分数规划)模板
    最优比例生成树最优比率生成树 01分数规划问题
    poj 2553强连通+缩点
    poj 2831 次小生成树模板
  • 原文地址:https://www.cnblogs.com/BIG-BOSS-ZC/p/11807342.html
Copyright © 2020-2023  润新知