从小到大owl-轮播中心模式

From small to big owl-carousel center mode

我的目标是制作一个如下所示的旋转木马:

从小到大

如果您不知道自己在看什么,这里有 5 个 images/items,但只有中间那个以全尺寸显示。靠近中心的图像较小,外面的图像更小。

但实际上实现这一点的 owl 轮播 类 (medium, big) 已经被弃用了。

我想知道是否可以在我的 owl-carousel 中制作类似的东西?

这是我在 jsfiddle

中所做的 link

请更改我的 fiddle .

反对者请先评论

/**
 * Plugin for linking multiple owl instances
 * @version 1.0.0
 * @author David Deutsch
 * @license The MIT License (MIT)
 */
;(function($, window, document, undefined) {
    /**
     * Creates the Linked plugin.
     * @class The Linked Plugin
     * @param {Owl} carousel - The Owl Carousel
     */
    var Linked = function(carousel) {
        /**
         * Reference to the core.
         * @protected
         * @type {Owl}
         */
        this._core = carousel;

        /**
         * All event handlers.
         * @protected
         * @type {Object}
         */
        this._handlers = {
            'dragged.owl.carousel changed.owl.carousel': $.proxy(function(e) {
                if (e.namespace && this._core.settings.linked) {
                    this.update(e);
                }
            }, this),
            'linked.to.owl.carousel': $.proxy(function(e, index, speed, standard, propagate) {
                if (e.namespace && this._core.settings.linked) {
                    this.toSlide(index, speed, propagate);
                }
            }, this)
        };

        // register event handlers
        this._core.$element.on(this._handlers);

        // set default options
        this._core.options = $.extend({}, Linked.Defaults, this._core.options);
    };

    /**
     * Default options.
     * @public
     */
    Linked.Defaults = {
        linked: false
    };

    /**
     * Updated linked instances
     */
    Linked.prototype.update = function(e) {
        this.toSlide( e.relatedTarget.relative(e.item.index) );
    };

    /**
     * Carry out the to.owl.carousel proxy function
     * @param {int} index
     * @param {int} speed
     * @param {bool} propagate
     */
    Linked.prototype.toSlide = function(index, speed, propagate) {
        var id = this._core.$element.attr('id'),
            linked = typeof(this._core.settings.linked) === 'string' ? this._core.settings.linked.split(',') : this._core.settings.linked;

        if ( typeof propagate == 'undefined' ) {
            propagate = true;
        }

        index = index || 0;

        if ( propagate ) {
            $.each(linked, function(i, el){
                $(el).trigger('linked.to.owl.carousel', [index, 300, true, false]);
            });
        } else {
            this._core.$element.trigger('to.owl.carousel', [index, 300, true]);

            if ( this._core.settings.current ) {
                this._core._plugins.current.switchTo(index);
            }
        }
    };

    /**
     * Destroys the plugin.
     * @protected
     */
    Linked.prototype.destroy = function() {
        var handler, property;

        for (handler in this._handlers) {
            this.$element.off(handler, this._handlers[handler]);
        }
        for (property in Object.getOwnPropertyNames(this)) {
            typeof this[property] != 'function' && (this[property] = null);
        }
    };

    $.fn.owlCarousel.Constructor.Plugins.linked = Linked;

})(window.Zepto || window.jQuery, window, document);


$(".carousel_1").owlCarousel({
    nav: false,
    items: 1,
    margin:1,
    linked: ".carousel_2"
});
var sync2 = $(".carousel_2");
$(sync2).owlCarousel({
    nav: true,
    loop:true,
    items: 4,
    margin:10,
    center:true,
    linked: sync2.prev()
}).on('initialized.owl.carousel linked.to.owl.carousel', function() {
    sync2.find('.owl-item.current').removeClass('current');
    var current = sync2.find('.owl-item.active.center').length ? sync2.find('.owl-item.active.center') : sync2.find('.owl-item.active').eq(0);
    current.addClass('current');
});

我试过了!!!

我试图在第一个 carousel(carousel_1) 配置结束时添加这部分代码

在那里我尝试选择第一个和最后一个项目并给它们类名,在这个类名中我将更改它们 widthheight .

但是这个解决方案只需要第一个和最后一个项目我需要从左到右到中心的所有项目

另外,在这个解决方案中,这样写也行不通

if(index == 1) 而不是第一个任何 index 都可以。这是将两个 类 添加到一个元素。

checkClasses();
$(".carousel_2").on('translated.owl.carousel', function(event) {
    checkClasses();
});

function checkClasses(){
    var total = $('.carousel_2 .owl-stage .owl-item.active').length;

    $('.carousel_2 .owl-stage .owl-item').removeClass('firstActiveItem lastActiveItem');

    $('.carousel_2 .owl-stage .owl-item.active').each(function(index){

        if (index === 0) {
            // this is the first one
        $(this).addClass('firstActiveItem');
        }
        if (index === total - 1 && total>1) {
            // this is the last one
            $(this).addClass('firstActiveItem');
        }

    });
}

您 CSS 中的一段代码将适用,

.owl-carousel .owl-stage {
    max-height: 200px;
    display: flex;
    align-items: center;
}
.carousel_2 .active.center + .active,.carousel_2 .active:first-child,
.carousel_2 :not(.active) + .active{
   height:150px;
}
.carousel_2 .active.center{
   height:200px;
}
.owl-carousel .owl-item img{height:100%}

Fiddle

Update 对于 7 and/or n 数项,css 将不起作用,但您可以使用 jQuery 代码如

$('.h-150px').removeClass('h-150px');
$('.h-200px').removeClass('h-200px');
// add class to just next/prev active element
current.next('.active').addClass('h-150px').end().prev('.active').addClass('h-150px');
// add class to just second next/prev active element
current.next('.active').next('.active').addClass('h-110px').end()
      .end().prev('.active').prev('.active').addClass('h-110px');

Updated Fiddle