Browse Source

fix(carousel): 修复动态删除条目至最后一个时调用 inst.reload 的异常问题 (#2107)

* fix(carousel): 修复外部动态增删条目时调用 inst.reload 的异常问题

* chore: 更正命名,避免歧义

* fix: 修复 slide 方法在条目数为 1 的滑动异常问题
pull/2141/head
贤心 4 months ago committed by GitHub
parent
commit
80188cdcd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 66
      examples/carousel.html
  2. 29
      src/modules/carousel.js

66
examples/carousel.html

@ -68,54 +68,56 @@ div[carousel-item]>*:nth-child(2n+1){background-color: #5FB878;}
layui.use('carousel', function(){
var carousel = layui.carousel;
//建造实例
carousel.render({
elem: '#test1'
,index: 2
//,full: true
,arrow: 'always'
,autoplay: 'always'
,change: function(obj){
// 实例
var carInst = carousel.render({
elem: '#test1',
index: 2,
// full: true,
arrow: 'always',
autoplay: 'always',
change: function(obj) {
console.log(obj)
}
//,interval: 5000
//,autoplay: false
//,indicator: 'outside'
//,trigger: 'hover'
},
// interval: 5000,
// autoplay: false,
// indicator: 'outside',
// trigger: 'hover'
});
// carInst.goto(1);
//事件
// 事件
/*
carousel.on('change(test1)', function(obj){
carousel.on('change(test1)', function(obj) {
console.log(obj)
});
*/
carousel.render({
elem: '#test2'
,interval: 1800
//,full: true
,anim: 'fade'
,height: '120px'
elem: '#test2',
interval: 1800,
// full: true,
anim: 'fade',
height: '120px'
});
carousel.render({
elem: '#test3'
//,full: true
,arrow: 'always'
//,autoplay: false
//,indicator: 'outside'
//,trigger: 'hover'
,anim: 'updown'
//,full: true
elem: '#test3',
// full: true,
arrow: 'always',
// autoplay: false,
// indicator: 'outside',
// trigger: 'hover',
anim: 'updown',
// full: true
});
// 图片轮播
carousel.render({
elem: '#test4'
,width: '720px'
,height: '360px'
,interval: 5000
elem: '#test4',
width: '720px',
height: '360px',
interval: 5000
});
});
</script>

29
src/modules/carousel.js

@ -113,13 +113,14 @@ layui.define(['jquery', 'lay'], function(exports){
// 初始焦点状态
that.elemItem.eq(options.index).addClass(THIS);
// 指示器等动作
if(that.elemItem.length <= 1) return;
// 指示器、箭头等动作
that.indicator();
that.arrow();
that.autoplay();
that.events();
if (that.elemItem.length > 1) {
that.events();
}
};
// 重置轮播
@ -188,19 +189,23 @@ layui.define(['jquery', 'lay'], function(exports){
Class.prototype.autoplay = function(){
var that = this;
var options = that.config;
var itemsCount = that.elemItem.length;
if(!options.autoplay) return;
clearInterval(that.timer);
that.timer = setInterval(function(){
that.slide();
}, options.interval);
if (itemsCount > 1) {
that.timer = setInterval(function(){
that.slide();
}, options.interval);
}
};
// 箭头
Class.prototype.arrow = function(){
var that = this;
var options = that.config;
var itemsCount = that.elemItem.length;
// 模板
var tplArrow = $([
@ -215,7 +220,7 @@ layui.define(['jquery', 'lay'], function(exports){
if(options.elem.find('.'+ELEM_ARROW)[0]){
options.elem.find('.'+ELEM_ARROW).remove();
}
options.elem.append(tplArrow);
itemsCount > 1 ? options.elem.append(tplArrow) : tplArrow.remove();
// 事件
tplArrow.on('click', function(){
@ -241,6 +246,7 @@ layui.define(['jquery', 'lay'], function(exports){
Class.prototype.indicator = function(){
var that = this;
var options = that.config;
var itemsCount = that.elemItem.length;
// 模板
var tplInd = that.elemInd = $(['<div class="'+ ELEM_IND +'"><ul>',
@ -260,7 +266,8 @@ layui.define(['jquery', 'lay'], function(exports){
if(options.elem.find('.'+ELEM_IND)[0]){
options.elem.find('.'+ELEM_IND).remove();
}
options.elem.append(tplInd);
itemsCount > 1 ? options.elem.append(tplInd) : tplInd.remove();
if(options.anim === 'updown'){
tplInd.css('margin-top', -(tplInd.height()/2));
@ -276,11 +283,12 @@ layui.define(['jquery', 'lay'], function(exports){
Class.prototype.slide = function(type, num){
var that = this;
var elemItem = that.elemItem;
var itemsCount = elemItem.length;
var options = that.config;
var thisIndex = options.index;
var filter = options.elem.attr('lay-filter');
if(that.haveSlide) return;
if (that.haveSlide || itemsCount <= 1) return;
// 滑动方向
if(type === 'sub'){
@ -329,6 +337,7 @@ layui.define(['jquery', 'lay'], function(exports){
var options = that.config;
if(options.elem.data('haveEvents')) return;
// 移入移出容器
options.elem.on('mouseenter touchstart', function(){

Loading…
Cancel
Save