优化 carousel 代码书写规范

This commit is contained in:
贤心 2023-08-17 15:52:00 +08:00
parent 8239401823
commit dda323caa0

View File

@ -14,28 +14,39 @@ layui.define(['jquery', 'lay'], function(exports){
// 外部接口
var carousel = {
config: {} //全局配置项
config: {}, // 全局配置项
// 设置全局项
,set: function(options){
set: function(options){
var that = this;
that.config = $.extend({}, that.config, options);
return that;
}
},
// 事件
,on: function(events, callback){
on: function(events, callback){
return layui.onevent.call(this, MOD_NAME, events, callback);
}
}
};
// 字符常量
,MOD_NAME = 'carousel', ELEM = '.layui-carousel', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled'
var MOD_NAME = 'carousel';
var ELEM = '.layui-carousel';
var THIS = 'layui-this';
var SHOW = 'layui-show';
var HIDE = 'layui-hide';
var DISABLED = 'layui-disabled'
,ELEM_ITEM = '>*[carousel-item]>*', ELEM_LEFT = 'layui-carousel-left', ELEM_RIGHT = 'layui-carousel-right', ELEM_PREV = 'layui-carousel-prev', ELEM_NEXT = 'layui-carousel-next', ELEM_ARROW = 'layui-carousel-arrow', ELEM_IND = 'layui-carousel-ind'
var ELEM_ITEM = '>*[carousel-item]>*';
var ELEM_LEFT = 'layui-carousel-left';
var ELEM_RIGHT = 'layui-carousel-right';
var ELEM_PREV = 'layui-carousel-prev';
var ELEM_NEXT = 'layui-carousel-next';
var ELEM_ARROW = 'layui-carousel-arrow';
var ELEM_IND = 'layui-carousel-ind';
// 构造器
,Class = function(options){
var Class = function(options){
var that = this;
that.config = $.extend({}, that.config, carousel.config, options);
that.render();
@ -43,16 +54,16 @@ layui.define(['jquery', 'lay'], function(exports){
// 默认配置
Class.prototype.config = {
width: '600px'
,height: '280px'
,full: false //是否全屏
,arrow: 'hover' //切换箭头默认显示状态hover/always/none
,indicator: 'inside' //指示器位置inside/outside/none
,autoplay: true //是否自动切换
,interval: 3000 //自动切换的时间间隔不能低于800ms
,anim: '' //动画类型default/updown/fade
,trigger: 'click' //指示器的触发方式click/hover
,index: 0 //初始开始的索引
width: '600px',
height: '280px',
full: false, // 是否全屏
arrow: 'hover', // 切换箭头默认显示状态hover/always/none
indicator: 'inside', // 指示器位置inside/outside/none
autoplay: true, // 是否自动切换
interval: 3000, // 自动切换的时间间隔不能低于800ms
anim: '', // 动画类型default/updown/fade
trigger: 'click', // 指示器的触发方式click/hover
index: 0 // 初始开始的索引
};
// 轮播渲染
@ -85,15 +96,15 @@ layui.define(['jquery', 'lay'], function(exports){
// 是否全屏模式
if(options.full){
options.elem.css({
position: 'fixed'
,width: '100%'
,height: '100%'
,zIndex: 9999
position: 'fixed',
width: '100%',
height: '100%',
zIndex: 9999
});
} else {
options.elem.css({
width: options.width
,height: options.height
width: options.width,
height: options.height
});
}
@ -104,6 +115,7 @@ layui.define(['jquery', 'lay'], function(exports){
// 指示器等动作
if(that.elemItem.length <= 1) return;
that.indicator();
that.arrow();
that.autoplay();
@ -120,32 +132,34 @@ layui.define(['jquery', 'lay'], function(exports){
// 获取上一个等待条目的索引
Class.prototype.prevIndex = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
var prevIndex = options.index - 1;
if(prevIndex < 0){
prevIndex = that.elemItem.length - 1;
}
return prevIndex;
};
// 获取下一个等待条目的索引
Class.prototype.nextIndex = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
var nextIndex = options.index + 1;
if(nextIndex >= that.elemItem.length){
nextIndex = 0;
}
return nextIndex;
};
// 索引递增
Class.prototype.addIndex = function(num){
var that = this
,options = that.config;
var that = this;
var options = that.config;
num = num || 1;
options.index = options.index + num;
@ -158,8 +172,8 @@ layui.define(['jquery', 'lay'], function(exports){
// 索引递减
Class.prototype.subIndex = function(num){
var that = this
,options = that.config;
var that = this;
var options = that.config;
num = num || 1;
options.index = options.index - num;
@ -172,8 +186,8 @@ layui.define(['jquery', 'lay'], function(exports){
// 自动轮播
Class.prototype.autoplay = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
if(!options.autoplay) return;
clearInterval(that.timer);
@ -185,13 +199,13 @@ layui.define(['jquery', 'lay'], function(exports){
// 箭头
Class.prototype.arrow = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
// 模板
var tplArrow = $([
'<button class="layui-icon '+ ELEM_ARROW +'" lay-type="sub">'+ (options.anim === 'updown' ? '&#xe619;' : '&#xe603;') +'</button>'
,'<button class="layui-icon '+ ELEM_ARROW +'" lay-type="add">'+ (options.anim === 'updown' ? '&#xe61a;' : '&#xe602;') +'</button>'
'<button class="layui-icon '+ ELEM_ARROW +'" lay-type="sub">'+ (options.anim === 'updown' ? '&#xe619;' : '&#xe603;') +'</button>',
'<button class="layui-icon '+ ELEM_ARROW +'" lay-type="add">'+ (options.anim === 'updown' ? '&#xe61a;' : '&#xe602;') +'</button>'
].join(''));
// 预设基础属性
@ -205,8 +219,8 @@ layui.define(['jquery', 'lay'], function(exports){
// 事件
tplArrow.on('click', function(){
var othis = $(this)
,type = othis.attr('lay-type')
var othis = $(this);
var type = othis.attr('lay-type')
that.slide(type);
});
};
@ -225,19 +239,19 @@ layui.define(['jquery', 'lay'], function(exports){
// 指示器
Class.prototype.indicator = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
// 模板
var tplInd = that.elemInd = $(['<div class="'+ ELEM_IND +'"><ul>'
,function(){
var tplInd = that.elemInd = $(['<div class="'+ ELEM_IND +'"><ul>',
function(){
var li = [];
layui.each(that.elemItem, function(index){
li.push('<li'+ (options.index === index ? ' class="layui-this"' : '') +'></li>');
});
return li.join('');
}()
,'</ul></div>'].join(''));
}(),
'</ul></div>'].join(''));
// 预设基础属性
options.elem.attr('lay-indicator', options.indicator);
@ -260,11 +274,11 @@ layui.define(['jquery', 'lay'], function(exports){
// 滑动切换
Class.prototype.slide = function(type, num){
var that = this
,elemItem = that.elemItem
,options = that.config
,thisIndex = options.index
,filter = options.elem.attr('lay-filter');
var that = this;
var elemItem = that.elemItem;
var options = that.config;
var thisIndex = options.index;
var filter = options.elem.attr('lay-filter');
if(that.haveSlide) return;
@ -300,9 +314,9 @@ layui.define(['jquery', 'lay'], function(exports){
// 回调返回的参数
var params = {
index: options.index
,prevIndex: thisIndex
,item: elemItem.eq(options.index)
index: options.index,
prevIndex: thisIndex,
item: elemItem.eq(options.index)
};
typeof options.change === 'function' && options.change(params);
@ -311,8 +325,8 @@ layui.define(['jquery', 'lay'], function(exports){
// 事件处理
Class.prototype.events = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
if(options.elem.data('haveEvents')) return;