使用“平滑滑动”在按钮单击时触发滑动器不起作用?

时间:2018-11-29 23:14:43

标签: javascript jquery html slider slick

我一直在尝试使用Slick Slide库制作具有自定义按钮的滑块。当您触发“#slider-swap”时,幻灯片应更改为第二张幻灯片,该幻灯片具有相同的按钮(不同名称),可以将幻灯片交换回第一张幻灯片。

我确实制作了自己的翻页盒,但最终坚持使用了它,因为它可以完成此工作而无需加载任何其他JS。

但是,当我触发按钮时,什么也没发生。我不确定自己在做什么错。我对HTML标记有种感觉,光滑的东西让它们之间的滑动变得困惑。

有什么想法吗?可能是因为已经很晚了,我从8am:X

开始从事这个项目。
<div class="cs-challenge">
<div class="front"> 
    <div class="cs-heading chalbg">
        <i class="fal fa-bullseye"></i>
        <h3>The Challenge</h3>
    </div>
    <div class="cs-content solbg chaltxt">
        <ul>
            <li>An uneven fouling covered at least 70% of the convection bank finned 
            surface.</li>
            <li>Deposit was airborne hydro carbon dust and refractory fines – packed tightly
            between all rows of convection side fined heater tubes</li>
            <li>Severely restricted access to the convection section.</li>
            <li>To clean the tube side using studded pigging at the same time.</li>
        </ul>
        <a class="btn" id="slider-swap" href="#">Change Slide</a>
    </div>
</div>
<div class="back">
    <div class="cs-heading solbg">
        <i class="fal fa-bullseye-arrow"></i>
        <h3>Solution</h3>
    </div>
    <div class="cs-content chalbg soltxt">
        <ul>
            <li>An uneven fouling covered at least 70% of the convection bank finned 
            surface.</li>
            <li>Deposit was airborne hydro carbon dust and refractory fines – packed tightly
            between all rows of convection side fined heater tubes</li>
            <li>Severely restricted access to the convection section.</li>
            <li>To clean the tube side using studded pigging at the same time.</li>
        </ul>
        <a class="btn" id="slider-swap" href="#">Change Slide</a>
    </div>
</div> 
</div>

<script>
    $(document).ready(function() {
        $(".cs-challenge").slick({
          autoplay: false,
          speed: 250,
          slidesToShow: 1,
          slidesToScroll: 1,
          fade: true,
          arrows: false,
          cssEase: 'linear',
          infinite: true
        });

    });
    var indexVal=1;
    $("#slider-swap").click(function(e){
            e.preventDefault();
            $( ".cs-challenge" ).slickGoTo(indexVal);
            indexVal=indexVal+1;
            if(indexVal>2){
            indexVal=1;
            }
        });
</script>

这是jsfiddle: http://jsfiddle.net/no30vq8h/

1 个答案:

答案 0 :(得分:0)

解决了。

因此,.slickGoTo是已贬值的函数,不再起作用(afaik?)。我已将其更改为将indexVal与.data('id');一起使用和html的data-id属性。

现在,当您触发按钮时,它将在每张幻灯片之间移动。

<a class="btn slider-swap" data-id="2" href="#">Challenge</a>

    //var indexVal=1;
jQuery(".slider-swap").click(function(e){
        e.preventDefault();
        var indexVal = jQuery(this).data('id');
        console.log(indexVal);
        jQuery( ".cs-challenge" ).slick('slickGoTo',indexVal);
        /*indexVal=indexVal+1;
        if(indexVal>2){
        indexVal=1;
        }*/

    });

这是最终代码。 https://pastebin.com/fH68Uw8W