原生js无缝轮播插件使用详解


Posted in Javascript onMarch 09, 2020

这篇博文主要讲如何使用原生js来实现一个兼容 IE9+ 的无缝轮播图。主要知识点如下:

  • 面向对象
  • js优化之节流函数
  • 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?obj.currentStyle[attr]:getComputedStyle(obj, false)[attr]; 
 }
 /*
 * @method bind
 * 事件绑定函数
 * bind events 
 */
 D.handler = function(){
 var _th = this,i=0
 if(this.btn){
 this.prev.onclick = function(){
 _th.turnLeft();
 }
 this.next.onclick = function(){
 _th.turnRight();
 }
 }
 if(this.focusBtn){
 for(; i<this.pageList.length; i++){
 this.pageList[i].key = i
 this.pageList[i].function(){
  _th.index = this.key + 1
  _th.toggleClass()
 }
 }
 }
 this.wraper.onmouseover = function(){
 clearInterval(_th.animation);
 }
 this.wraper.onmouseout = function(){
 _th.auto && _th.autoPlay()
 }
 }
 /*
 * func turnRight
 * 向右轮播函数
 * @para 0
 */
 D.turnRight = function(){
 this.index++;
 if(this.index == this.length-1){
 this.index=1;
 this.sliders.style.marginLeft = 0;
 }
 this.toggleClass(); 
 }
 /*
 * func turnLeft
 * 向左轮播函数
 * @para 0
 */
 D.turnLeft = function(){
 this.index--;
 if(this.index == 0){
 this.index = this.length-2;
 this.sliders.style.marginLeft = -this.width * (this.length-1) + "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?-1:1;

 if(Math.abs(ins-currentPosition) < _th.step){
 _th.sliders.style.marginLeft = ins + "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图片轮播

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js使用Replace结合正则替换重复出现的字符串功能示例
Dec 27 Javascript
原生js实现放大镜特效
Mar 08 Javascript
浅谈vue的踩坑路
Aug 31 Javascript
360提示[高危]使用存在漏洞的JQuery版本的解决方法
Oct 27 jQuery
Vue底层实现原理总结
Feb 17 Javascript
常用的 JS 排序算法 整理版
Apr 05 Javascript
angular的输入和输出的使用方法
Sep 22 Javascript
解决ie11 SCRIPT5011:不能执行已释放Script的代码问题
May 05 Javascript
vue项目强制清除页面缓存的例子
Nov 06 Javascript
jquery 插件重新绑定的处理方法分析
Nov 23 jQuery
vue实现户籍管理系统
May 29 Javascript
vue中element 的upload组件发送请求给后端操作
Sep 07 Javascript
微信小程序自定义弹出模态框禁止底部滚动功能
Mar 09 #Javascript
JavaScript享元模式原理与用法实例详解
Mar 09 #Javascript
JavaScript装饰者模式原理与用法实例详解
Mar 09 #Javascript
JavaScript适配器模式原理与用法实例详解
Mar 09 #Javascript
JavaScript中的this基本问题实例小结
Mar 09 #Javascript
Nuxt页面级缓存的实现
Mar 09 #Javascript
JavaScript设计模式之门面模式原理与实现方法分析
Mar 09 #Javascript
You might like
PHP生成网页快照 不用COM不用扩展.
2010/02/11 PHP
php 批量添加多行文本框textarea一行一个
2014/06/03 PHP
php实现有趣的人品测试程序实例
2015/06/08 PHP
php利用imagemagick实现复古老照片效果实例
2017/02/16 PHP
jQuery 过滤not()与filter()实例代码
2012/05/10 Javascript
javascript模块化是什么及其优缺点介绍
2013/09/02 Javascript
JavaScript格式化日期时间的方法和自定义格式化函数示例
2014/04/04 Javascript
用console.table()调试javascript
2014/09/04 Javascript
jQuery轻松实现表格的隔行变色和点击行变色的实例代码
2016/05/09 Javascript
深入探究node之Transform
2017/07/20 Javascript
Vuex实现计数器以及列表展示效果
2018/03/10 Javascript
jQuery+Cookie实现切换皮肤功能【附源码下载】
2018/03/25 jQuery
layui结合form,table的全选、反选v1.0示例讲解
2018/08/15 Javascript
JavaScript中import用法总结
2019/01/20 Javascript
jQuery使用$.extend(true,object1, object2);实现深拷贝对象的方法分析
2019/03/06 jQuery
Vue router传递参数并解决刷新页面参数丢失问题
2020/12/02 Vue.js
Python线程同步的实现代码
2018/10/03 Python
python 列表递归求和、计数、求最大元素的实例
2018/11/28 Python
Python利用sqlacodegen自动生成ORM实体类示例
2019/06/04 Python
获取django框架orm query执行的sql语句实现方法分析
2019/06/20 Python
Django中reverse反转并且传递参数的方法
2019/08/06 Python
python pandas移动窗口函数rolling的用法
2020/02/29 Python
Python selenium抓取虎牙短视频代码实例
2020/03/02 Python
Python通过Pillow实现图片对比
2020/04/29 Python
Django自定义YamlField实现过程解析
2020/11/11 Python
世界各地的旅游、观光和活动:Isango!
2019/10/29 全球购物
致400米运动员广播稿
2014/02/07 职场文书
二年级班级文化建设方案
2014/05/10 职场文书
幼儿园家长安全责任书
2014/07/22 职场文书
生物学专业求职信
2014/07/23 职场文书
奉献家乡演讲稿
2014/09/13 职场文书
2014最新版群众路线四风整改措施
2014/09/24 职场文书
2015年大学辅导员工作总结
2015/05/12 职场文书
教你用Python写一个植物大战僵尸小游戏
2021/04/25 Python
PyTorch 如何自动计算梯度
2021/05/23 Python
Python 可迭代对象 iterable的具体使用
2021/08/07 Python