原生JS实现焦点轮播图的核心逻辑与代码解析
本文详细解析使用原生JavaScript实现焦点轮播图的五大关键点,包括前后按钮切换、辅助图处理、底部按钮联动、动画防抖以及无限循环逻辑。重点介绍了利用CSS3 transform属性实现底部按钮从底部向上生长的动画效果,并提供了完整的HTML、CSS及JS代码示例,帮助开发者掌握无依赖库的轮播图实现方案。
原生JS焦点轮播图实现要点
使用原生JavaScript实现焦点轮播图时,需要重点关注以下核心要素:
首先,实现左右切换按钮的功能,同时必须妥善处理辅助图片(克隆图片)的逻辑,以确保视觉上的无缝衔接。
其次,中间的指示器按钮(button)需随图片切换同步更新状态。用户点击任意指示器按钮时,应能直接跳转到对应的图片索引位置。
第三,实现定时自动播放与无限循环播放功能,确保轮播图能够持续运行且首尾相接。
第四,在动画执行过程中必须禁用按钮交互,防止用户快速点击导致多个动画队列堆积,确保当前动画完全结束后才能触发下一个动作。
第五,切换图片时,底部指示器按钮的动画效果需设计为从底部向上生长。这需要结合CSS3的 transform:scale() 和 transform-origin:0 100% 两个属性来实现,具体代码实现如下所示:
完整代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewpoint" content="width=device-width,initial-scale=1,user-scalable="no">
<title>20170101</title>
<style type="text/css">
a{text-decoration:none;color:#3DBBF5;}
.wrapper{width:750px;height:350px;background:#001032;margin:20px auto;text-align:center;box-shadow:0 0 12px 2px hsla(0,20%,30%,0.5);padding:10px 15px;position:relative;}
.effect{position:relative;cursor:pointer;}
.effect:hover{color:#02a0e9;}
.effect:before{width:100%;display:inline-block !important;position:absolute;height:1px;background:#02a0e9;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(0,1);content:'';bottom:-5px;left:0;}
.effect:hover:before{transform:scale(1);-webkit-transform:scale(1);}
#lunBo{margin-top:20px;overflow:hidden;height:300px;width:750px;position:relative;}
#list{position:absolute;z-index:22;height:300px;width:5250px;}
#list img{float:left;}
#buttons { position: absolute; height: 20px; width: 150px; z-index: 99; bottom: 20px; left: 40%;}
span { cursor: pointer; float: left; width: 10px; height: 5px; background: #333; margin-right: 10px;}
.on { background: yellow;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(1,4);-ms-transform:scale(1,4);-moz-transform:scale(1,4);-webkit-transform:scale(1,4);transform-origin:0% 0%;-webkit-transform-origin:0% 100%;-moz-transform-origin:0% 100%;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 100px; line-height:100px;position: absolute; z-index: 92; top: 30%; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#lunBo:hover .arrow { display: block;}
#prev { left: 0px;}
#next { right: 0px;}
</style>
</head>
<body>
<div class="wrapper">
<a class="effect" href="#">2016完了,2017来了</a>
<div id="lunBo">
<div id="list" style="left:-750px;">
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856m1bhxxx1d8jfnblb.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856z48mfrrr8u064rf6.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856e95yze236lvq7y2a.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.jpg" alt=""/>
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
</div>
<script>
window.onload = function(){
var lunBo = document.getElementById('lunBo');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
//console.log(buttons);
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
var animated = false;
var interval = 3000;
var timer;
//显示按钮的索引
function showButton(){
for(var i = 0 ; i < buttons.length ; i++){
if( buttons[i].className == 'on' ){
buttons[i].className = '';
break;
};
};
buttons[index - 1].className='on';
};
function play(){
timer = setTimeout(function () {
next.onclick();
play();
}, interval);
};
function stop(){
clearTimeout(timer);
};
//向前按钮
next.onclick = function () {
if (animated) {
return;
}
if (index == 5) {
index = 1;
}
else {
index += 1;
}
animate(-750);
showButton();
};
prev.onclick = function () {
if (animated) {
return;
}
if (index == 1) {
index = 5;
}
else {
index -= 1;
}
animate(750);
showButton();
};
//parseInt()转换为纯数值
function animate(offset){
animated = true;
var newLeft = parseInt(list.style.left) + offset; //目标值
var time = 300; //位移总时间为300
var interval = 10; //
var speed = offset/(Math.floor(time/interval)); //每次位移量
function go(){
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);
}else{
animated = false;
list.style.left = newLeft+ 'px'; //现在的位移
if( newLeft > -750){ //假的辅助图
list.style.left = -3750 + 'px';
}
if( newLeft < -3750){
list.style.left = -750 + 'px';
}
}
};
go();
};
//小按钮
for(var i=0;i < buttons.length;i++){
buttons[i].onclick = function(){
if(this.className == 'on'){
return;
};
var myIndex = parseInt(this.getAttribute('index'));
var offset = -750 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
lunBo.onmouseout = play;
lunBo.onmouseover = stop;
play();
}
</script>
</body>
</html>
以上内容涵盖了原生JS焦点轮播图的主要实现细节,希望对您的学习或工作有所帮助。如果您觉得有用,欢迎继续支持本站!
JavaScript闭包与类(Class)的区别及适用场景对比
2026-11-05 15:48:57
JavaScript入门要学ES6吗?let与箭头函数等新特性解析
2026-11-05 14:49:16
JavaScript 8种数据类型详解:基本类型与引用类型的区别
2026-11-05 14:25:15
怎么在JavaScript中使用async/await优化Ajax异步流程
2026-11-05 13:56:15
JavaScript replace回调函数实现动态正则替换逻辑
2026-11-05 13:52:13
