这篇博文主要讲如何使用原生js来实现一个兼容 IE9+ 的无缝轮播图。主要知识点如下:
- 面向对象
- js优化之节流函数
- js运动
效果
html结构
<div class="sliders-wraper" id="rotation-1"> <ul class="sliders clear"> <li class="slider" style="background:purple">5</li> <li class="slider" style="background:pink">1</li> <li class="slider" style="background:beige">2</li> <li class="slider" style="background:gold">3</li> <li class="slider" style="background:skyblue">4</li> <li class="slider" style="background:purple">5</li> <li class="slider" style="background:pink">1</li> </ul> <div class="pagenation"> <div class="page page-active"><a></a></div> <div class="page"><a></a></div> <div class="page"><a></a></div> <div class="page"><a></a></div> <div class="page"><a></a></div> </div> <span class='prev rotation-btn'><</span> <span class='next rotation-btn'</span> </div>
css样式
*{margin: 0;padding: 0;box-sizing: border-box;}
.clear{zoom: 0;}
.clear:after{content: '';display: block;overflow: hidden;clear: both;widows: 0;height: 0;}
.sliders-wraper{width: 100%;height: 400px;line-height: 400px;
overflow: hidden;position: relative;text-align: center;}
.sliders{position: absolute;list-style: none;font-size: 50px;}
.slider{float: left;}
.rotation-btn{position: absolute;top: 50%;width: 50px;height: 50px;
line-height: 50px;margin-top: -25px;font-size: 30px;color: #ccc;cursor: pointer;}
.prev{left:0;}
.next{right:0;}
.pagenation{position: absolute;bottom: 10px;width: 100%;height: 25px;line-height: 25px;}
.pagenation .page{width: 40px;height: 25px;display: inline-block;cursor: pointer;}
.pagenation .page a{display: block;width: 30px;height: 5px;border: 1px solid #ccc;
border-radius: 5px;background: transparent;margin: 10px auto;}
.pagenation .page-active a{border-color: #0076ff;background-color: #0076ff;}
js
;(function(doc, win){
function Rotation(obj){
this.wraper = doc.getElementById(obj.el) //窗口
this.sliders = this.wraper.getElementsByClassName('sliders')[0] //图片父盒子
this.slideList = this.sliders.getElementsByClassName('slider') //所有图片
this.length = this.slideList.length
this.index = 1 //当前显示的图片的索引
this.timer = null //单张图片运动定时器
this.animation = null //自动轮播定时器
// 在obj中可以自定义的参数
this.mode = 'easy-in-out'//动画曲线,可选 'linear'
this.step = 5 //匀速运动滚动步长
this.delay = 2500 //轮播间隔
this.duration = 40 //单张图片动画时长
this.auto = true //是否开启自动轮播
this.btn = false //是否有左右按钮
this.focusBtn = true //是否支持焦点事件
for(var k in obj)
this[k] = obj[k]
if(this.btn){
this.prev = this.wraper.getElementsByClassName('prev')[0]
this.next = this.wraper.getElementsByClassName('next')[0]
}
if(this.focusBtn){
this.pagenation = this.wraper.getElementsByClassName('pagenation')[0]
this.pageList = this.pagenation.getElementsByClassName('page')
this.activePage = 0 //当前的焦点的索引
}
this.init()
}
var D = Rotation.prototype
/*
* func init
* 初始化函数
* @para 0
*/
D.init = function(){
this.width = this.wraper.clientWidth
if(this.mode == 'linear')
this.duration = 1
for(var i=0; i<this.length; i++)
this.slideList[i].style.width = this.width + 'px'
this.sliders.style.width = this.width * this.length + 'px'
this.sliders.style.marginLeft = -this.width + "px";
this.handler()
this.auto && this.autoPlay()
}
D.getStyle = function(obj, attr){
return obj.currentStyle"px";
}
this.toggleClass();
}
/*
* func toggleClass
* 改变数字焦点样式,轮播动画核心函数调用
* @para 0
*/
D.toggleClass = function(){
if(this.focusBtn){
this.pageList[this.activePage].className = "page";
this.pageList[this.index-1].className = "page page-active";
this.activePage = this.index-1;
}
this.slide(-this.index * this.width);
}
/*
* func slide
* 图片滚动函数,核心函数
* @param ins 滚动终点
*/
D.slide = function(ins){
var _th = this
// 防止动画过度过程中计算错误
if(_th.timer) clearInterval(_th.timer);
_th.timer = setInterval(move,_th.duration);
function move(){
var currentPosition = parseFloat(_th.sliders.style.marginLeft);
// 根据起始点和目标位置的比较确定向哪个方向移动
var n = ins-currentPosition<0"px";
clearInterval(_th.timer);
}else{
// 变速运动关键,注释变为匀速运动
if(_th.mode == 'easy-in-out')
_th.step = Math.abs(ins-currentPosition)/5
_th.sliders.style.marginLeft = currentPosition + n*_th.step + "px";
}
}
}
/*
* func autoPlay
* 自动轮播函数
* @para 0
*/
D.autoPlay = function(){
var _th = this
clearInterval(_th.animation)
_th.animation = setInterval(function(){
_th.turnRight();
},_th.delay)
}
/*
* func debounce
* 节流函数
* @para fn<要执行的函数> delay<节流时间>
* @value func
*/
D.debounce = function(fn,delay){
var timer = null
return function(){
if(timer){
clearTimeout(timer)
}
timer = setTimeout(fn,delay)
}
}
/*
* func refresh
* 自动刷新函数,这里采用节流是防止刷新操作太过于频繁导致性能下降
* @para 0
*/
D.refresh = function(){
var _th = this
this.debounce(function(){
_th.init()
_th.toggleClass()
},100)()
}
/*
* func rotation
* 实例化函数
* @para obj 实例化的具体参数
* @value 返回具体实例
*/
win.rotation = function(obj){
return new Rotation(obj)
}
})(document, window)
调用方式
var r2 = rotation({
el: 'rotation-1',
mode: 'easy-in-out', //运动曲线
auto: true,//开启自动轮播
btn: true, //左右按钮
focusBtn: false//焦点
})
window.onresize = function(){
r2 && r2.refresh()
}
精彩专题分享:jQuery图片轮播 JavaScript图片轮播 Bootstrap图片轮播
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“原生js无缝轮播插件使用详解”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
2025年11月02日
2025年11月02日
- 小骆驼-《草原狼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]
