简体   繁体   English

使用 Slick Slide 触发按钮单击时的滑块不起作用?

[英]Trigger Slider on Button click with Slick Slide Not working?

I have been trying to make a slider that has custom buttons using the Slick Slide library.我一直在尝试使用 Slick Slide 库制作一个具有自定义按钮的滑块。 When you trigger the "#slider-swap" the slide should change to the second slide which has the same button (different name) that swaps the slide back to the first.当您触发“#slider-swap”时,幻灯片应更改为具有相同按钮(不同名称)的第二张幻灯片,该按钮将幻灯片切换回第一张。

I did make my own flipbox but ended up sticking with this as it will do the job without loading any additional JS.我确实制作了自己的翻转盒,但最终坚持使用它,因为它无需加载任何额外的 JS 就可以完成这项工作。

However when I trigger the button, nothing happens.但是,当我触发按钮时,什么也没有发生。 I am not sure what I am doing wrong.我不确定我做错了什么。 I have a feeling its something with the HTML markup and slick is getting confused to what to flick between.我有一种感觉,它与 HTML 标记有关,而 slick 对要在其中轻弹的内容感到困惑。

Any ideas?有任何想法吗? It might because it's quite late and I have been working on this project since 8am :X这可能是因为它已经很晚了,我从早上 8 点开始就一直在做这个项目: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>

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

Resolved it.解决了。

So for one, .slickGoTo is depreciated function, no longer works (afaik?).因此,一方面,.slickGoTo 是贬值的功能,不再有效(afaik?)。 I have changed it to use indexVal with .data('id');我已将其更改为使用 indexVal 和 .data('id'); and html data-id attributes for the button.和按钮的 html data-id 属性。

Now when you trigger the button, it will go between each slide.现在,当您触发按钮时,它将在每张幻灯片之间移动。

<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;
        }*/

    });

Here is the final code.这是最终的代码。 https://pastebin.com/fH68Uw8W https://pastebin.com/fH68Uw8W

I have checked the UI in the shared jsfiddle link.我已经检查了共享 jsfiddle 链接中的 UI。 The slider is not applying to which it is applied.滑块不适用于它所应用的对象。 Please add slick js CDN.请添加 slick js CDN。

You can use slickGoTo like below:您可以像下面这样使用slickGoTo

$(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" ).slick('slickGoTo',indexVal);
    indexVal=indexVal+1;
    if(indexVal>2){
    indexVal=1;
    }
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM