Skip to content

Commit

Permalink
fix(carousel): 修复动态删除条目至最后一个时调用 inst.reload 的异常问题 (layui#2107)
Browse files Browse the repository at this point in the history
* fix(carousel): 修复外部动态增删条目时调用 inst.reload 的异常问题

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

* fix: 修复 slide 方法在条目数为 1 的滑动异常问题
  • Loading branch information
sentsim authored Jul 26, 2024
1 parent 12e33ce commit 80188cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
66 changes: 34 additions & 32 deletions examples/carousel.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,54 +68,56 @@
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>
Expand Down
29 changes: 19 additions & 10 deletions src/modules/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

// 重置轮播
Expand Down Expand Up @@ -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 = $([
Expand All @@ -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(){
Expand All @@ -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>',
Expand All @@ -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));
Expand All @@ -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'){
Expand Down Expand Up @@ -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(){
Expand Down

0 comments on commit 80188cd

Please sign in to comment.