mirror of https://github.com/layui/layui
fix(carousel): 修复动态删除条目至最后一个时调用 inst.reload 的异常问题 (#2107)
* fix(carousel): 修复外部动态增删条目时调用 inst.reload 的异常问题 * chore: 更正命名,避免歧义 * fix: 修复 slide 方法在条目数为 1 的滑动异常问题pull/2141/head
parent
12e33cedab
commit
80188cdcd8
|
@ -68,54 +68,56 @@ div[carousel-item]>*:nth-child(2n+1){background-color: #5FB878;}
|
||||||
layui.use('carousel', function(){
|
layui.use('carousel', function(){
|
||||||
var carousel = layui.carousel;
|
var carousel = layui.carousel;
|
||||||
|
|
||||||
//建造实例
|
// 实例
|
||||||
carousel.render({
|
var carInst = carousel.render({
|
||||||
elem: '#test1'
|
elem: '#test1',
|
||||||
,index: 2
|
index: 2,
|
||||||
//,full: true
|
// full: true,
|
||||||
,arrow: 'always'
|
arrow: 'always',
|
||||||
,autoplay: 'always'
|
autoplay: 'always',
|
||||||
,change: function(obj){
|
change: function(obj) {
|
||||||
console.log(obj)
|
console.log(obj)
|
||||||
}
|
},
|
||||||
//,interval: 5000
|
// interval: 5000,
|
||||||
//,autoplay: false
|
// autoplay: false,
|
||||||
//,indicator: 'outside'
|
// indicator: 'outside',
|
||||||
//,trigger: 'hover'
|
// trigger: 'hover'
|
||||||
});
|
});
|
||||||
|
|
||||||
//事件
|
// carInst.goto(1);
|
||||||
|
|
||||||
|
// 事件
|
||||||
/*
|
/*
|
||||||
carousel.on('change(test1)', function(obj){
|
carousel.on('change(test1)', function(obj) {
|
||||||
console.log(obj)
|
console.log(obj)
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
carousel.render({
|
carousel.render({
|
||||||
elem: '#test2'
|
elem: '#test2',
|
||||||
,interval: 1800
|
interval: 1800,
|
||||||
//,full: true
|
// full: true,
|
||||||
,anim: 'fade'
|
anim: 'fade',
|
||||||
,height: '120px'
|
height: '120px'
|
||||||
});
|
});
|
||||||
|
|
||||||
carousel.render({
|
carousel.render({
|
||||||
elem: '#test3'
|
elem: '#test3',
|
||||||
//,full: true
|
// full: true,
|
||||||
,arrow: 'always'
|
arrow: 'always',
|
||||||
//,autoplay: false
|
// autoplay: false,
|
||||||
//,indicator: 'outside'
|
// indicator: 'outside',
|
||||||
//,trigger: 'hover'
|
// trigger: 'hover',
|
||||||
,anim: 'updown'
|
anim: 'updown',
|
||||||
//,full: true
|
// full: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// 图片轮播
|
// 图片轮播
|
||||||
carousel.render({
|
carousel.render({
|
||||||
elem: '#test4'
|
elem: '#test4',
|
||||||
,width: '720px'
|
width: '720px',
|
||||||
,height: '360px'
|
height: '360px',
|
||||||
,interval: 5000
|
interval: 5000
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -113,13 +113,14 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
// 初始焦点状态
|
// 初始焦点状态
|
||||||
that.elemItem.eq(options.index).addClass(THIS);
|
that.elemItem.eq(options.index).addClass(THIS);
|
||||||
|
|
||||||
// 指示器等动作
|
// 指示器、箭头等动作
|
||||||
if(that.elemItem.length <= 1) return;
|
|
||||||
|
|
||||||
that.indicator();
|
that.indicator();
|
||||||
that.arrow();
|
that.arrow();
|
||||||
that.autoplay();
|
that.autoplay();
|
||||||
|
|
||||||
|
if (that.elemItem.length > 1) {
|
||||||
that.events();
|
that.events();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 重置轮播
|
// 重置轮播
|
||||||
|
@ -188,19 +189,23 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
Class.prototype.autoplay = function(){
|
Class.prototype.autoplay = function(){
|
||||||
var that = this;
|
var that = this;
|
||||||
var options = that.config;
|
var options = that.config;
|
||||||
|
var itemsCount = that.elemItem.length;
|
||||||
|
|
||||||
if(!options.autoplay) return;
|
if(!options.autoplay) return;
|
||||||
clearInterval(that.timer);
|
clearInterval(that.timer);
|
||||||
|
|
||||||
|
if (itemsCount > 1) {
|
||||||
that.timer = setInterval(function(){
|
that.timer = setInterval(function(){
|
||||||
that.slide();
|
that.slide();
|
||||||
}, options.interval);
|
}, options.interval);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 箭头
|
// 箭头
|
||||||
Class.prototype.arrow = function(){
|
Class.prototype.arrow = function(){
|
||||||
var that = this;
|
var that = this;
|
||||||
var options = that.config;
|
var options = that.config;
|
||||||
|
var itemsCount = that.elemItem.length;
|
||||||
|
|
||||||
// 模板
|
// 模板
|
||||||
var tplArrow = $([
|
var tplArrow = $([
|
||||||
|
@ -215,7 +220,7 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
if(options.elem.find('.'+ELEM_ARROW)[0]){
|
if(options.elem.find('.'+ELEM_ARROW)[0]){
|
||||||
options.elem.find('.'+ELEM_ARROW).remove();
|
options.elem.find('.'+ELEM_ARROW).remove();
|
||||||
}
|
}
|
||||||
options.elem.append(tplArrow);
|
itemsCount > 1 ? options.elem.append(tplArrow) : tplArrow.remove();
|
||||||
|
|
||||||
// 事件
|
// 事件
|
||||||
tplArrow.on('click', function(){
|
tplArrow.on('click', function(){
|
||||||
|
@ -241,6 +246,7 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
Class.prototype.indicator = function(){
|
Class.prototype.indicator = function(){
|
||||||
var that = this;
|
var that = this;
|
||||||
var options = that.config;
|
var options = that.config;
|
||||||
|
var itemsCount = that.elemItem.length;
|
||||||
|
|
||||||
// 模板
|
// 模板
|
||||||
var tplInd = that.elemInd = $(['<div class="'+ ELEM_IND +'"><ul>',
|
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]){
|
if(options.elem.find('.'+ELEM_IND)[0]){
|
||||||
options.elem.find('.'+ELEM_IND).remove();
|
options.elem.find('.'+ELEM_IND).remove();
|
||||||
}
|
}
|
||||||
options.elem.append(tplInd);
|
|
||||||
|
itemsCount > 1 ? options.elem.append(tplInd) : tplInd.remove();
|
||||||
|
|
||||||
if(options.anim === 'updown'){
|
if(options.anim === 'updown'){
|
||||||
tplInd.css('margin-top', -(tplInd.height()/2));
|
tplInd.css('margin-top', -(tplInd.height()/2));
|
||||||
|
@ -276,11 +283,12 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
Class.prototype.slide = function(type, num){
|
Class.prototype.slide = function(type, num){
|
||||||
var that = this;
|
var that = this;
|
||||||
var elemItem = that.elemItem;
|
var elemItem = that.elemItem;
|
||||||
|
var itemsCount = elemItem.length;
|
||||||
var options = that.config;
|
var options = that.config;
|
||||||
var thisIndex = options.index;
|
var thisIndex = options.index;
|
||||||
var filter = options.elem.attr('lay-filter');
|
var filter = options.elem.attr('lay-filter');
|
||||||
|
|
||||||
if(that.haveSlide) return;
|
if (that.haveSlide || itemsCount <= 1) return;
|
||||||
|
|
||||||
// 滑动方向
|
// 滑动方向
|
||||||
if(type === 'sub'){
|
if(type === 'sub'){
|
||||||
|
@ -330,6 +338,7 @@ layui.define(['jquery', 'lay'], function(exports){
|
||||||
|
|
||||||
if(options.elem.data('haveEvents')) return;
|
if(options.elem.data('haveEvents')) return;
|
||||||
|
|
||||||
|
|
||||||
// 移入移出容器
|
// 移入移出容器
|
||||||
options.elem.on('mouseenter touchstart', function(){
|
options.elem.on('mouseenter touchstart', function(){
|
||||||
if (that.config.autoplay === 'always') return;
|
if (that.config.autoplay === 'always') return;
|
||||||
|
|
Loading…
Reference in New Issue