From 80188cdcd88e043d7c450f48254a6530239afb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Fri, 26 Jul 2024 10:34:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(carousel):=20=E4=BF=AE=E5=A4=8D=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=88=A0=E9=99=A4=E6=9D=A1=E7=9B=AE=E8=87=B3=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E6=97=B6=E8=B0=83=E7=94=A8=20inst.r?= =?UTF-8?q?eload=20=E7=9A=84=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98=20(#2107)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(carousel): 修复外部动态增删条目时调用 inst.reload 的异常问题 * chore: 更正命名,避免歧义 * fix: 修复 slide 方法在条目数为 1 的滑动异常问题 --- examples/carousel.html | 66 +++++++++++++++++++++-------------------- src/modules/carousel.js | 29 +++++++++++------- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/examples/carousel.html b/examples/carousel.html index b74fb72e..347e737c 100644 --- a/examples/carousel.html +++ b/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 }); }); diff --git a/src/modules/carousel.js b/src/modules/carousel.js index e55abdb6..ee24b779 100644 --- a/src/modules/carousel.js +++ b/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 = $(['