简体   繁体   English

随着jQuery / JS中轮播中幻灯片的变化在特定的div中添加和删除类

[英]Adding and removing classes in specific div with the change of slides in carousel in jquery/ js

I am using slick slider and i want to add/remove "active" in a div which is outside (somewhere else in the page) slick slider code. 我正在使用平滑滑块,并且我想在平滑滑块代码之外(页面中其他位置)的div中添加/删除“活动”。 For example: I have this code for the slider. 例如:我有此滑块代码。

<div class="slider">
    <div class="showcase-1"><img src="img/1.jpg" alt="" /></div>
    <div class="showcase-2"><img src="img/2.jpg" alt="" /></div>
    <div class="showcase-3"><img src="img/3.jpg" alt="" /></div>
</div>

Now on change of slide, "active" class should be added to div and then the "active" class should be removed by itself when the slide is changed and it should be added to second div which is for second slide. 现在,在更改幻灯片时,应将“活动”类添加到div,然后在更改幻灯片时应自行删除“活动”类,并将其添加到用于第二张幻灯片的第二个div中。 Note that every div is for each slide. 请注意,每个div适用于每张幻灯片。 Like first div is for "showcase-1" slide and second div for "showcase-2". 就像第一个div用于“ showcase-1”幻灯片,第二个div用于“ showcase-2”。

<div class="showcase"></div>
<div class="showcase"></div>
<div class="showcase"></div>

I tried this jquery and it's working good but it's not changing the classes on change of slides.:D i am sure this is not a right way! 我试过这个jQuery,它运行良好,但是在更改幻灯片时并没有改变类。:D我确信这不是正确的方法!

$(function() {
 if ($(".slider .showcase-1").hasClass("slick-current")) {
     $("div.showcase-1").addClass('active');
 } else {
     $('div.showcase-1').removeClass('active');
 }
});

It looks like Slick slider developers have already found and fixed this bug where all slides were getting the "slick-active" class. 看起来Slick滑块开发人员已经发现并修复了所有幻灯片都获得“ slick-active”类的错误。 Now all slides get slick-active but only the current slide gets "slick-slide slick-current slick-active" classes. 现在,所有幻灯片都可以激活,但只有当前幻灯片可以使用“ slick-slide slick-current slick-active”类。

If you use the latest version of Slick you will receive this improvement and won't have to write your own. 如果您使用的是最新版本的Slick,则将获得此改进,而不必自己编写。

See the Slick slider demos here and inspect them and you will see these more helpful, updated classes. 在此处查看Slick滑块演示并进行检查,您将看到这些更有用的,更新的类。

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

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