标签:function useInt 06 14 li nowIndex 2021 change left
jQuery实现轮播图效果
前提是你下载了jquery的js文件,下载地址为http://jquery.com/download
如果没下也可以联网链接
准备5张图片命名为1到5.jpg
直接源代码使用
欢迎交流讨论
1.先看HTML和CSS代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图片轮播效果</title>
<script src="js/jquery-3.6.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/图片轮播.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 500px;
height: 900px;
margin: 30px auto;
overflow: hidden;
position: relative;
}
#ulList{
list-style: none;
width: 1000%;
position: absolute;
}
#ulList li{
width: 500px;
height: 300px;
float: left;
}
.olList{
width: 300px;
height: 40px;
position: absolute;
left: 100px;
bottom: 30px;
list-style: none;
}
.olList li{
float: left;
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
margin: 0 10px;
background-color: #fff;
border-radius: 50%;
cursor: pointer;
}
.olList.now{
background-color: red;
color: #fff;
}
#left,#right{
position: absolute;
background: #0000FF;
color: white;
font-size: 30px;
font-weight: bold;
text-align: center;
}
#left{left: 0;top: 45%;}
#right{right: 0;top: 45%;}
</style>
</head>
<body>
<div class="box">
<ul id="ulList">
<li><img src="img/1.jpg" alt="" /></li>
<li><img src="img/2.jpg" alt="" /></li>
<li><img src="img/3.jpg" alt="" /></li>
<li><img src="img/4.jpg" alt="" /></li>
<li><img src="img/5.jpg" alt="" /></li>
</ul>
<ol class="olList">
<li class="now">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
<div id="left"></div>
<div id="right"></div>
</div>
</body>
</html>
再来JavaScript代码
$(function(){
var nowIndex = 0;
var liNumber = $("#ulList li").length;
function change (index) {
var ulMove = index*500;//设置图片移动距离;
$("#ulList").animate({left:"-"+ulMove+"px"},500);
$(".olList").find("li").removeClass("now").eq(index).addClass("now");
}
var useInt = setInterval(function(){
if(nowIndex<liNumber-1){
nowIndex++;
}else{
nowIndex = 0;
}
change(nowIndex);
},2500);
function useIntAgain () {
useInt = setInterval(function(){
if(nowIndex<liNumber-1){
nowIndex++;
}else{
nowIndex = 0;
}
change(nowIndex);
},2500);}
$("#left").hover(function(){
clearInterval(useInt);
},function(){
useIntAgain();
});
$("#left").click(function(){
nowIndex = (nowIndex > 0)?(--nowIndex):(liNumber-1);
change(nowIndex);
})
$("#right").hover(function(){
clearInterval(useInt);
},function(){
useIntAgain();
});
$("#right").click(function(){
nowIndex = (nowIndex < liNumber-1)?(++nowIndex):0;
change(nowIndex);
});
$(".olList li").each(function(item){
$(this).hover(function(){
clearInterval(useInt);
nowIndex = item;
change(item);
},function(){
useIntAgain();
});
});
})
标签:function,useInt,06,14,li,nowIndex,2021,change,left 来源: https://blog.csdn.net/qq_46073710/article/details/117907417
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。