简体   繁体   English

如何为Slick轮播中的当前幻灯片更新CSS?

[英]How do I update the CSS for the current slide in the Slick carousel?

I'm creating a site to showcase my client's photography. 我正在创建一个网站来展示客户的摄影作品。 I have several different main images that rotate in a slideshow. 我有几个不同的主图像在幻灯片中旋转。 I want the link to that specific category to change to a different color when it's corresponding image is shown. 我希望显示该特定图像的链接时,该特定类别的链接可以更改为其他颜色。 My function works perfectly for the 1st image but it won't work for my second one. 我的功能非常适合第一张图片,但不适用于我的第二张图片。 Can someone read my code and see what may be wrong? 有人可以阅读我的代码,看看有什么问题吗? I'm not getting any errors in the console and I believe it should work since it's literally the same function as the first one. 我没有在控制台中遇到任何错误,并且我相信它应该可以正常工作,因为它实际上与第一个功能相同。 Thank you so much!! 非常感谢!!

<!-- Photo Slider -->
        <div id="maincarousel" class="carousel slide" data-ride="carousel">
          <div class="carousel-inner" role="listbox">
            <div class="item active">
              <img class="d-block img-fluid natureMain" src="bppnature/Hope.jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid buildingMain" src="bppbuildings/here comes the sun (around the way).jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid fashionMain" src="bppfashion/Eye Eternal.jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid productMain" src="Main Images/Proper Prepardness.jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid abstractMain" src="bppabstract/Interconnected.jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid coverMain" src="bppcoverart/Black EGYptian BastARD (right).jpg">
            </div>
            <div class="item">
              <img class="d-block img-fluid graffittiMain" src="bppgraffitti/Gang'N'em.jpg">
            </div>
            <ol class="carousel-indicators">
                <li data-target="#maincarousel" data-slide-to="0" class="active"></li>
                <li data-target="#maincarousel" data-slide-to="1"></li>
                <li data-target="#maincarousel" data-slide-to="2"></li>
                <li data-target="#maincarousel" data-slide-to="3"></li>
                <li data-target="#maincarousel" data-slide-to="4"></li>
                <li data-target="#maincarousel" data-slide-to="5"></li>
                <li data-target="#maincarousel" data-slide-to="6"></li>
              </ol>
          </div>
        </div>

$(document).ready(function(){
                // $("#mainslider").slick({
                //   infinite: true,
                //   slidesToShow: 1,
                //   slidesToScroll: 1,
                //   centerMode: true,
                // //   autoplay: true,
                // //   autoplaySpeed: 2000,
                //   pauseOnHover: true
                // });
                highlightNatureMain();
                highlightBuildingMain();
                $('.slickcarousel').slick({
                  infinite: true,
                  slidesToShow: 4,
                  slidesToScroll: 1,
                  centerMode: true,
                //   autoplay: true,
                //   autoplaySpeed: 2000,
                  pauseOnHover: true
                });
                carouselDisplay();
                // animationNature();
                // animationBuilding();
                // animationFashion();
                // animationProducts();
                // animationAbstract();
                // animationCovers();
                // animationGraffiti();
                // animationProjects();
                // stopHover();
                // imageHover();
                setInterval(function(){ nameFlash();}, 3000);

            });

function highlightNatureMain() {
                if ($(".natureMain").parent("div").hasClass("active")) {
                    $('.nav-nature').css('color', 'purple');
                }
                else {
                    $(".nav-nature").css("color","white");
                }
            }
            function highlightBuildingMain() {
                if ($(".buildingMain").parent("div").hasClass("active")) {
                    console.log("br");
                    $('.nav-building').css('color', 'purple');
                }
            }

If all you want to do is change the CSS on the slides, you are over-complicating things - you don't need javascript at all! 如果您要做的只是更改幻灯片上的CSS,就使事情变得过于复杂-根本不需要JavaScript!

CSS-only 仅CSS

You already have classes associated with your slides, so you just need to create rules for those classes, eg 您已经有与幻灯片关联的类,因此只需要为这些类创建规则,例如

.item .nav-nature{ color: purple;}    /* default colour for this element */
.item.active .nav-nature{ color: white;}    /* colour when this item is active */
.item .nav-building{ color: purple;}
.item.active .nav-nature{ color: white;}

jQuery Callback functions for Slick slider 光滑滑块的jQuery回调函数

However if you did need to do something in jQuery, slick slider has callback functions beforeChange and afterChange , so you could use those, eg 但是,如果您确实需要在jQuery中afterChange ,那么光滑的滑块具有回调函数beforeChangeafterChange ,因此您可以使用它们,例如

jQuery('#mainslider').on('beforeChange', function(event, slick, currentSlide, nextSlide){

    switch(nextSlide){
        case 1: /* next slide is the first slide */
           [... do something...]
           break;
        case 2: /* second slide */
           [...]
           break;
    }
}

Use Callback to set link colour 使用回调设置链接颜色

If you do need to set the CSS for the links through jQuery, a handy way to do it is to use an array for the colours - the currentSlide and nextSlide are the slide positions, so you can use them as the key to the array, eg 如果您确实需要通过jQuery设置链接的CSS,那么一种简便的方法是使用颜色数组currentSlidenextSlide是幻灯片位置,因此您可以将它们用作数组的键,例如

CSS - set the default colours for the links CSS-设置链接的默认颜色

.item .nav-nature{ color: purple;} 
.item .nav-building{ color: purple;}

jQuery - set the colour for the active slide jQuery-设置活动幻灯片的颜色

/* an array of the link colours, corresponding to the slides */
var linkcolours = ["white", "white", "blue", "red", etc...];

jQuery('#mainslider').on('beforeChange', function(event, slick, currentSlide, nextSlide){
    /* set the active slide colour to the position of the corresponding slide 
       e.g. get the 1st element from the array (index=0) for slide 1 etc */
    $(".item.active a").css("color", colours[nextSlide-1]);
});

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

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