Slick.js 附加箭头不适用于多个轮播

Slick.js appendarrows doesn't work for multiple carousels

我正在使用 Slick jQuery 轮播,但每当我使用 "appendArrows" 选项时都会遇到问题:

$(document).ready(function(){
  $('.post-slider').slick({
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 2,
    slidesToScroll: 1,
    appendArrows: '.button-container',
    centerMode: true
});
});

你看,我需要输出多个轮播,但我显示的轮播数量也是每个轮播中 appendArrows 函数似乎 运行 的次数。

<div id="slidersort">
<div class="slider">
    <span class="drag-handle">☰</span>
    <div class="wrap_lined_header"><h2>News</h2><div class="button-container"></div></div>

    <div class="clr"></div>
    <div class="post-slider">
        <?php 
        $args = array('post_type' => 'news');
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
        <div>
            <a class="post-link" href="<?php the_permalink() ?>"> </a>
            <h2><?php the_title(); ?></h2>
            <div class="post-date"><?php the_date('d/m/Y') ?></div>
            <div class="entry-content">
                <p><?php the_field('summary'); ?></p>
            </div>
        </div>
        <?php endwhile;?>

    </div>
</div>

<div class="slider">
    <span class="drag-handle">☰</span>
    <div class="wrap_lined_header"><h2>Weather</h2><div class="button-container"></div></div>

    <div class="clr"></div>
    <div class="post-slider">
        <?php 
        $args = array('post_type' => 'news');
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
        <div>
            <a class="post-link" href="<?php the_permalink() ?>"> </a>
            <h2><?php the_title(); ?></h2>
            <div class="post-date"><?php the_date('d/m/Y') ?></div>
            <div class="entry-content">
                <p><?php the_field('summary'); ?></p>
            </div>
        </div>
        <?php endwhile;?>

    </div>
</div>

<div class="slider">
    <span class="drag-handle">☰</span>
    <div class="wrap_lined_header"><h2>Sports</h2><div class="button-container"></div></div>

    <div class="clr"></div>
    <div class="post-slider">
        <?php 
        $args = array('post_type' => 'news');
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
        <div>
            <a class="post-link" href="<?php the_permalink() ?>"> </a>
            <h2><?php the_title(); ?></h2>
            <div class="post-date"><?php the_date('d/m/Y') ?></div>
            <div class="entry-content">
                <p><?php the_field('summary'); ?></p>
            </div>
        </div>
        <?php endwhile;?>

    </div>
</div>

假设我有 3 个轮播显示(如上),每当我显示页面时,它 returns 我有 3 个这样的按钮:

<div class="button-container">
<button type="button" data-role="none" class="slick-prev slick-arrow" aria-label="Previous" role="button">Previous</button>
<button type="button" data-role="none" class="slick-prev slick-arrow" aria-label="Previous" role="button">Previous</button>
<button type="button" data-role="none" class="slick-prev slick-arrow" aria-label="Previous" role="button">Previous</button>
<button type="button" data-role="none" class="slick-next slick-arrow" aria-label="Next" role="button">Next</button>
<button type="button" data-role="none" class="slick-next slick-arrow" aria-label="Next" role="button">Next</button>
<button type="button" data-role="none" class="slick-next slick-arrow" aria-label="Next" role="button">Next</button>
</div>

关于如何改变原来的 jQuery 调用以更好地使用 3 个轮播,有什么想法吗?我在想如何让 appendArrows 选项只显示 class 名称的开头,然后我可以 运行 一个简单的 PHP 循环来为每个名称添加数值,但是我恐怕我的 jQuery 还达不到标准。

除非你有更好的主意?

看起来这是一个老问题,但这是我的解决方案,以防有人遇到同样的问题。我所做的是初始化每个元素,然后使用 $(this) 向上遍历和 select 元素。

$( document ).ready( function() {
    $( '.post-slider' ).each( function() {
        $( this ).slick( {
            dots: true,
            infinite: true,
            speed: 500,
            slidesToShow: 2,
            slidesToScroll: 1,
            appendArrows: $(this).parents('.slider').find('.button-container'),
            centerMode: true
        } );
    } );
} );

我使用以下解决方案从滑块包装器中找到每个导航自定义箭头。

$('.slider-wrap').each(function (index, sliderWrap) {
    var $slider = $(sliderWrap).find('.slider');
    var $next = $(sliderWrap).find('.next');
  var $prev = $(sliderWrap).find('.prev');
    $slider.slick({
        dots: true,
      slidesToShow: 2.5,
        nextArrow: $next,
      prevArrow: $prev
    });
});

在此处查看 Noah Rodenbeek 的钢笔:https://codepen.io/nominalaeon/pen/gwAdjd