Использование двух экземпляров подключенных каруселей (jCarousel) на одной странице

Я использую jCarousel, в частности подключенные карусели, которые вы можете увидеть по адресу https://sorgalla.com/jcarousel/examples/connected-carousels/. Для эксперимента по A/B-тестированию мне нужно использовать два экземпляра подключенных каруселей. Первый работает корректно. Второй нет. Во втором случае я нажимаю миниатюры, и большие изображения не меняются. Я не думаю, что официальная документация содержит примеры наличия нескольких подключенных каруселей на одной странице. Любые подсказки о том, как этого достичь? Я попытался выяснить, как https://sorgalla.com/jcarousel/docs/reference/api.html#reload. Я думал о том, чтобы включить это в мой основной файл JavaScript:

$('.jcarousel').jcarousel('reload', {
    animation: 'slow'
});

Однако это не решает проблему для меня. Любые подсказки? Спасибо.

ОБНОВЛЕНИЕ 1:

Исходный код см. на странице https://github.com/jsor/jcarousel/blob/master/examples/connected-carousels/index.html. Теперь посмотрите, что я пытаюсь сделать, добавляя второй экземпляр подключенной карусели:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Connected Carousels - jCarousel Examples</title>

        <!-- Shared assets -->
        <link rel="stylesheet" type="text/css" href="../_shared/css/style.css">

        <!-- Example assets -->
        <link rel="stylesheet" type="text/css" href="jcarousel.connected-carousels.css">

        <script type="text/javascript" src="../../vendor/jquery/jquery.js"></script>
        <script type="text/javascript" src="../../dist/jquery.jcarousel.min.js"></script>

        <script type="text/javascript" src="jcarousel.connected-carousels.js"></script>

    </head>
    <body>

        <div class="wrapper">
            <h1>Connected Carousels</h1>

            <p>This example shows how to connect two carousels together so that one carousels acts as a navigation for the other.</p>

            <div class="connected-carousels">
                <div class="stage">
                    <div class="carousel carousel-stage">
                        <ul>
                            <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img4.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img5.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img6.jpg" width="600" height="400" alt=""></li>
                        </ul>
                    </div>
                    <p class="photo-credits">
                        Photos by <a href="http://www.mw-fotografie.de">Marc Wiegelmann</a>
                    </p>
                    <a href="#" class="prev prev-stage"><span>&lsaquo;</span></a>
                    <a href="#" class="next next-stage"><span>&rsaquo;</span></a>
                </div>

                <div class="navigation">
                    <a href="#" class="prev prev-navigation">&lsaquo;</a>
                    <a href="#" class="next next-navigation">&rsaquo;</a>
                    <div class="carousel carousel-navigation">
                        <ul>
                            <li><img src="../_shared/img/img1_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img2_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img3_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img4_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img5_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img6_thumb.jpg" width="50" height="50" alt=""></li>
                        </ul>
                    </div>
                </div>
            </div>



            <div class="connected-carousels">
                <div class="stage">
                    <div class="carousel carousel-stage">
                        <ul>
                            <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img4.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img5.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img6.jpg" width="600" height="400" alt=""></li>
                        </ul>
                    </div>
                    <p class="photo-credits">
                        Photos by <a href="http://www.mw-fotografie.de">Marc Wiegelmann</a>
                    </p>
                    <a href="#" class="prev prev-stage"><span>&lsaquo;</span></a>
                    <a href="#" class="next next-stage"><span>&rsaquo;</span></a>
                </div>

                <div class="navigation">
                    <a href="#" class="prev prev-navigation">&lsaquo;</a>
                    <a href="#" class="next next-navigation">&rsaquo;</a>
                    <div class="carousel carousel-navigation">
                        <ul>
                            <li><img src="../_shared/img/img1_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img2_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img3_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img4_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img5_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img6_thumb.jpg" width="50" height="50" alt=""></li>
                        </ul>
                    </div>
                </div>
            </div>


        </div>

    </body>
</html>

Что происходит, так это то, что первый ряд миниатюр управляет обеими большими сценическими изображениями, верхней и нижней. Второй ряд миниатюр не работает. Нажимаешь на нее и ничего не происходит:

введите здесь описание изображения

ОБНОВЛЕНИЕ 2:

Мне кажется, что то, что я пытаюсь сделать, является базовой и обычной вещью, с которой библиотека jCarousel должна легко справляться. Тем не менее, я не нашел ничего в примерах или документации для достижения того, чего я хочу, и я рассматриваю возможность изменить https://github.com/jsor/jcarousel/blob/master/examples/connected-carousels/jcarousel.connected-carousels.js так что например вместо только

// Setup the carousels. Adjust the options for both carousels here.
var carouselStage      = $('.carousel-stage').jcarousel();
var carouselNavigation = $('.carousel-navigation').jcarousel();

У меня может быть что-то вроде этого:

// Setup the carousels. Adjust the options for both carousels here.
var carouselStage      = $('.carousel-stage').jcarousel();
var carouselNavigation = $('.carousel-navigation').jcarousel();
var carouselStage1      = $('.carousel-stage1').jcarousel();
var carouselNavigation1 = $('.carousel-navigation1').jcarousel();

Что вы думаете о том, что я планирую попробовать (carouselStage1, carouselNavigation1 и т. д.)? Я не нахожу другого способа, чтобы оба экземпляра jCarousel работали правильно.


person Jaime Montoya    schedule 09.01.2020    source источник


Ответы (1)


Я заставил его работать правильно, изменив все три файла на https://github.com/jsor/jcarousel/tree/master/examples/connected-carousels. Я не уверен, что то, что я сделал, было самым элегантным и эффективным решением, поскольку, как я уже упоминал в своем вопросе, с моей точки зрения, мне нужна была базовая и обычная вещь, с которой библиотека jCarousel должна легко справляться. Тем не менее, я проверил свое решение, и оно работает правильно. Обе jCarousel теперь работают корректно и независимо друг от друга на одной странице. Смотрите мое решение:

index.html

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Connected Carousels - jCarousel Examples</title>

        <!-- Shared assets -->
        <link rel="stylesheet" type="text/css" href="../_shared/css/style.css">

        <!-- Example assets -->
        <link rel="stylesheet" type="text/css" href="jcarousel.connected-carousels.css">

        <script type="text/javascript" src="../../vendor/jquery/jquery.js"></script>
        <script type="text/javascript" src="../../dist/jquery.jcarousel.min.js"></script>

        <script type="text/javascript" src="jcarousel.connected-carousels.js"></script>

    </head>
    <body>

        <div class="wrapper">
            <h1>Connected Carousels</h1>

            <p>This example shows how to connect two carousels together so that one carousels acts as a navigation for the other.</p>

            <div class="connected-carousels">
                <div class="stage">
                    <div class="carousel carousel-stage">
                        <ul>
                            <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img4.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img5.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img6.jpg" width="600" height="400" alt=""></li>
                        </ul>
                    </div>
                    <p class="photo-credits">
                        Photos by <a href="http://www.mw-fotografie.de">Marc Wiegelmann</a>
                    </p>
                    <a href="#" class="prev prev-stage"><span>&lsaquo;</span></a>
                    <a href="#" class="next next-stage"><span>&rsaquo;</span></a>
                </div>

                <div class="navigation">
                    <a href="#" class="prev prev-navigation">&lsaquo;</a>
                    <a href="#" class="next next-navigation">&rsaquo;</a>
                    <div class="carousel carousel-navigation">
                        <ul>
                            <li><img src="../_shared/img/img1_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img2_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img3_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img4_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img5_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img6_thumb.jpg" width="50" height="50" alt=""></li>
                        </ul>
                    </div>
                </div>
            </div>



            <div class="connected-carousels">
                <div class="stage">
                    <div class="carousel carousel-stage1">
                        <ul>
                            <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img4.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img5.jpg" width="600" height="400" alt=""></li>
                            <li><img src="../_shared/img/img6.jpg" width="600" height="400" alt=""></li>
                        </ul>
                    </div>
                    <p class="photo-credits">
                        Photos by <a href="http://www.mw-fotografie.de">Marc Wiegelmann</a>
                    </p>
                    <a href="#" class="prev prev-stage"><span>&lsaquo;</span></a>
                    <a href="#" class="next next-stage"><span>&rsaquo;</span></a>
                </div>

                <div class="navigation">
                    <a href="#" class="prev prev-navigation">&lsaquo;</a>
                    <a href="#" class="next next-navigation">&rsaquo;</a>
                    <div class="carousel carousel-navigation1">
                        <ul>
                            <li><img src="../_shared/img/img1_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img2_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img3_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img4_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img5_thumb.jpg" width="50" height="50" alt=""></li>
                            <li><img src="../_shared/img/img6_thumb.jpg" width="50" height="50" alt=""></li>
                        </ul>
                    </div>
                </div>
            </div>


        </div>

    </body>
</html>

jcarousel.connected-carousels.css

/** Stage container **/

.connected-carousels .stage {
    width: 620px;
    margin: 20px auto;
    position: relative;
}

.connected-carousels .photo-credits {
    position: absolute;
    right: 15px;
    bottom: 0;
    font-size: 13px;
    color: #fff;
    text-shadow: 0 0 1px rgba(0, 0, 0, 0.85);
    opacity: .66;
}

.connected-carousels .photo-credits a {
    color: #fff;
}

/** Navigation container **/

.connected-carousels .navigation {
    width: 260px;
    margin: 20px auto;
    position: relative;
}

/** Shared carousel styles **/

.connected-carousels .carousel {
    overflow: hidden;
    position: relative;
}

.connected-carousels .carousel ul {
    width: 10000em;
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}

.connected-carousels .carousel li {
    float: left;
}

/** Stage carousel specific styles **/

.connected-carousels .carousel-stage {
    height: 400px;
    border: 10px solid #fff;
    -webkit-border-radius: 5px;
       -moz-border-radius: 5px;
            border-radius: 5px;
    -webkit-box-shadow: 0 0 2px #999;
       -moz-box-shadow: 0 0 2px #999;
            box-shadow: 0 0 2px #999;
}

.connected-carousels .carousel-stage1 {
    height: 400px;
    border: 10px solid #fff;
    -webkit-border-radius: 5px;
       -moz-border-radius: 5px;
            border-radius: 5px;
    -webkit-box-shadow: 0 0 2px #999;
       -moz-box-shadow: 0 0 2px #999;
            box-shadow: 0 0 2px #999;
}

/** Navigation carousel specific styles **/

    .connected-carousels .carousel-navigation {
        height: 60px;
        width: 240px;
        background: #fff;
        border: 10px solid #fff;
        -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
                border-radius: 5px;
        -webkit-box-shadow: 0 0 2px #999;
           -moz-box-shadow: 0 0 2px #999;
                box-shadow: 0 0 2px #999;
    }

    .connected-carousels .carousel-navigation1 {
        height: 60px;
        width: 240px;
        background: #fff;
        border: 10px solid #fff;
        -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
                border-radius: 5px;
        -webkit-box-shadow: 0 0 2px #999;
           -moz-box-shadow: 0 0 2px #999;
                box-shadow: 0 0 2px #999;
    }

    .connected-carousels .carousel-navigation li {
        cursor: pointer;
    }

    .connected-carousels .carousel-navigation1 li {
        cursor: pointer;
    }

    .connected-carousels .carousel-navigation li img {
        display: block;
        border: 5px solid #fff;
    }

    .connected-carousels .carousel-navigation1 li img {
        display: block;
        border: 5px solid #fff;
    }

    .connected-carousels .carousel-navigation li.active img {
        border-color: #ccc;
    }

    .connected-carousels .carousel-navigation1 li.active img {
        border-color: #ccc;
    }

    /** Stage carousel controls **/

    .connected-carousels .prev-stage,
    .connected-carousels .next-stage {
        display: block;
        position: absolute;
        top: 0;
        width: 305px;
        height: 410px;
        color: #fff;
    }

    .connected-carousels .prev-stage {
        left: 0;
    }

    .connected-carousels .next-stage {
        right: 0;
    }

    .connected-carousels .prev-stage.inactive,
    .connected-carousels .next-stage.inactive {
        display: none;
    }

    .connected-carousels .prev-stage span,
    .connected-carousels .next-stage span {
        display: none;
        position: absolute;
        top: 50%;
        width: 30px;
        height: 30px;
        text-align: center;
        background: #4E443C;
        color: #fff;
        text-decoration: none;
        text-shadow: 0 0 1px #000;
        font: 24px/27px Arial, sans-serif;
        -webkit-border-radius: 30px;
           -moz-border-radius: 30px;
                border-radius: 30px;
        -webkit-box-shadow: 0 0 2px #999;
           -moz-box-shadow: 0 0 2px #999;
                box-shadow: 0 0 2px #999;
    }

    .connected-carousels .prev-stage span {
        left: 20px;
    }

    .connected-carousels .next-stage span {
        right: 20px;
    }

    .connected-carousels .prev-stage:hover span,
    .connected-carousels .next-stage:hover span {
        display: block;
    }

    /** Navigation carousel controls **/

    .connected-carousels .prev-navigation,
    .connected-carousels .next-navigation {
        display: block;
        position: absolute;
        width: 30px;
        height: 30px;
        background: #4E443C;
        color: #fff;
        text-decoration: none;
        text-shadow: 0 0 1px #000;
        font: 16px/29px Arial, sans-serif;
        -webkit-border-radius: 30px;
           -moz-border-radius: 30px;
                border-radius: 30px;
        -webkit-box-shadow: 0 0 2px #999;
           -moz-box-shadow: 0 0 2px #999;
                box-shadow: 0 0 2px #999;
    }

    .connected-carousels .prev-navigation {
        left: -15px;
        top: 22px;
        text-indent: 6px;
    }

    .connected-carousels .next-navigation {
        right: -15px;
        top: 22px;
        text-indent: 20px;
    }

    .connected-carousels .prev-navigation.inactive,
    .connected-carousels .next-navigation.inactive {
        opacity: .5;
        cursor: default;
    }

jcarousel.connected-carousels.js

(function($) {
    // This is the connector function.
    // It connects one item from the navigation carousel to one item from the
    // stage carousel.
    // The default behaviour is, to connect items with the same index from both
    // carousels. This might _not_ work with circular carousels!
    var connector = function(itemNavigation, carouselStage) {
        return carouselStage.jcarousel('items').eq(itemNavigation.index());
    };

    $(function() {
        // Setup the carousels. Adjust the options for both carousels here.
        var carouselStage      = $('.carousel-stage').jcarousel();
        var carouselNavigation = $('.carousel-navigation').jcarousel();

        var carouselStage1      = $('.carousel-stage1').jcarousel();
        var carouselNavigation1 = $('.carousel-navigation1').jcarousel();

        // We loop through the items of the navigation carousel and set it up
        // as a control for an item from the stage carousel.
        carouselNavigation.jcarousel('items').each(function() {
            var item = $(this);

            // This is where we actually connect to items.
            var target = connector(item, carouselStage);

            item
                .on('jcarouselcontrol:active', function() {
                    carouselNavigation.jcarousel('scrollIntoView', this);
                    item.addClass('active');
                })
                .on('jcarouselcontrol:inactive', function() {
                    item.removeClass('active');
                })
                .jcarouselControl({
                    target: target,
                    carousel: carouselStage
                });
        });

        carouselNavigation1.jcarousel('items').each(function() {
            var item = $(this);

            // This is where we actually connect to items.
            var target = connector(item, carouselStage1);

            item
                .on('jcarouselcontrol:active', function() {
                    carouselNavigation1.jcarousel('scrollIntoView', this);
                    item.addClass('active');
                })
                .on('jcarouselcontrol:inactive', function() {
                    item.removeClass('active');
                })
                .jcarouselControl({
                    target: target,
                    carousel: carouselStage1
                });
        });


        // Setup controls for the stage carousel
        $('.prev-stage')
            .on('jcarouselcontrol:inactive', function() {
                $(this).addClass('inactive');
            })
            .on('jcarouselcontrol:active', function() {
                $(this).removeClass('inactive');
            })
            .jcarouselControl({
                target: '-=1'
            });

        $('.next-stage')
            .on('jcarouselcontrol:inactive', function() {
                $(this).addClass('inactive');
            })
            .on('jcarouselcontrol:active', function() {
                $(this).removeClass('inactive');
            })
            .jcarouselControl({
                target: '+=1'
            });

        // Setup controls for the navigation carousel
        $('.prev-navigation')
            .on('jcarouselcontrol:inactive', function() {
                $(this).addClass('inactive');
            })
            .on('jcarouselcontrol:active', function() {
                $(this).removeClass('inactive');
            })
            .jcarouselControl({
                target: '-=1'
            });

        $('.next-navigation')
            .on('jcarouselcontrol:inactive', function() {
                $(this).addClass('inactive');
            })
            .on('jcarouselcontrol:active', function() {
                $(this).removeClass('inactive');
            })
            .jcarouselControl({
                target: '+=1'
            });
    });
})(jQuery);
person Jaime Montoya    schedule 09.01.2020