Slick.js appendarrows不适用于多个轮播

时间:2016-03-04 14:07:04

标签: javascript jquery html slick.js

我正在使用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个旋转木马显示(如上所示),每当我显示页面时,它会返回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选项只显示类名的BEGINNING然后我可以运行一个简单的PHP循环来为每个类添加数值,但我担心我的jQuery不是那个划伤。

除非你有更好的主意?

2 个答案:

答案 0 :(得分:3)

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

$( 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
        } );
    } );
} );

答案 1 :(得分:0)

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

$('.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