知识要点
1.实现无限循环的原理:
以偏移的距离来判断是否跳回第一张和最后一张
也可以利用循环判断图片的当前索引值
var newLeft=parseInt(list.style.left)+offset;//当前的偏移量+下一次的偏移量=新的偏移量
list.style.left=newLeft+"px";//当前的偏移值=新的偏移值
//以偏移的距离来判断是否跳回第一张和最后一张
if(newLeft>-600){
list.style.left=-3000+"px";
}
if (newLeft<-3000){
list.style.left=-600+"px";
}
2.当前图片轮播的圆点变色显示:
因为每次点击index+1 所以当前的index-1就是button的索引
//添加on前先清空on
for(var i=0;i<buttons.length;i++){
if(buttons[i].className=="on"){
buttons[i].className="";
break;
}
}
buttons[index-1].className="on";
3.实现动画滚动效果:
原理就是把每次的偏移量分为多次完成比如一次600px那就分为10次每次偏移60px
就要用到setTimeout(go,10);//10毫秒再次调用go函数,一直到不满足条件就停
var newLeft=parseInt(list.style.left)+offset;//当前的偏移量+下一次的偏移量=新的偏移量
var time=300;//位移总时间
var interval=10;//位移间隔时间
//动画效果自定义公式: 每次位移的距离=单次偏移距离/位移次数
var speed=offset/(time/interval);
//递归函数 直到不满足条件(跳到辅助图)
//递归就是把600偏移量分为多次完成偏移
function go(){
//speed<0 并且 当前偏移量>下一次偏移量 就是向左偏移 || 反之向右偏移
if ((speed<0 &&parseInt(list.style.left)>newLeft) || (speed>0 &&parseInt(list.style.left)<newLeft)) {
list.style.left=parseInt(list.style.left)+speed+"px";//每次位移的值
setTimeout(go,interval);//10毫秒再次调用go函数
}else{
animated=false;
list.style.left=newLeft+"px";//当前的偏移值=新的偏移值
if(newLeft>-600){
list.style.left=-3000+"px";
}
if (newLeft<-3000){
list.style.left=-600+"px";
}
}
}
4.点击圆点按钮执行动画:
原理获取当前的按钮位置再获取要点击的按钮的位置
用(点击的——当前的)*-600=需要跳转的正负距离(向左或向右)
for(var i=0;i<buttons.length;i++){
buttons[i].onclick=function(){
if(this.className=="on"){
return;
}
//要点击的index属性的值 转换为整数
var myIndex=parseInt(this.getAttribute("index"));
//偏移量=-600*(要点击的位置-当前所在的位置),当前的位置就是index循环所得
var os=-600*(myIndex-index);
//切换完成后,把点击的index位置变成当前的index位置
index=myIndex;
showButton();
if(!animated){
animate(os);
}
}
}
5.自动播放:
给外层容器加个onmouseover事件再调用setInterval方法
//给方法定义全局变量是因为停止的时候要使用
function play(){
timer=setInterval(function(){
next.onclick();
},3000);
}
clearInterval(timer)
完整代码
注:图片链接本地替换一下
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
#container{width: 600px; height: 400px; overflow: hidden; position: relative; }
#list{width: 4200px; height: 400px; position: absolute; z-index: 1;}
#list img{float: left;}
#buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}
#buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}
#buttons .on { background: orangered;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px; position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#container:hover .arrow { display: block;}
#prev { left: 20px;}
#next { right: 20px;}
</style>
</head>
<body>
<div id="container">
<div id="list" style="left: -600px;">
<img src="/UploadFiles/2021-04-02/5.jpg">
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“原生js实现无限循环轮播图效果”评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]