Jens Nielsen Jens Nielsen - 4 months ago 48
CSS Question

Slick slider change background-color on each slide

The setup is this.
A slider with client logos, showen one at a time.
I would like to change the background-color of my parent div to the specific brandcolor from each client(and the H1). animateded if possible.

How would i go about doing that?

My HTML - I would like to change the background-color of the section div and the h1 in the content-wrapper.

<div class="section clients section-text-right">
<div class="content-wrapper">
<h1>Clients</h1>
<div class="frontpage-client-slick slick-theme-frontpage">
<div class="scouts"><img src="images/client-logos/logo_scouts@2x.png"></div>
<div class="jjd"><img src="images/client-logos/logo_jjd_white@2x.png"></div>
<div class="unipension"><img src="images/client-logos/logo_unipension@2x.png"></div>
<div class="dr"><img src="images/client-logos/logo_dr@2x.png"></div>
<div class="verdensborn"><img src="images/client-logos/logo_verdensboern@2x.png"></div>
<div class="fi"><img src="images/client-logos/logo_fi@2x.png"></div>
<div class="midtlink"><img src="images/client-logos/logo_midtlink@2x.png"></div>
</div>
</div>
</div>


Slick slider - i know i need to do something in the beforeChange, i just dont know what.

$('.frontpage-client-slick').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
infinite: true,
speed: 1000,
autoplay: true,
autoplaySpeed: 4000,
fade: true,
centerMode: true
});
$('.frontpage-client-slick').on('beforeChange', function(event, slick, currentSlide, nextSlide){
console.log(nextSlide);
});

Answer

You need to get the slide elements and set the background color via jQuery and .css() for example.

The slick object stores it's slides in $slides so you can access them via the index provided through nextSlide.

I've created a working fiddle for you:

var $slideContainter = $('.frontpage-client-slick'),
    $slider = $slideContainter.slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: true,
        infinite: true,
        speed: 1000,
        autoplay: true,
        autoplaySpeed: 4000,
        fade: true,
        centerMode: true
    }),
    colorSettings = {
        headline: ['red', 'blue', 'yellow','red', 'blue', 'yellow','blue'],
        section: ['blue', 'yellow','red', 'blue', 'yellow','blue', 'red']
    },
    changeColors = function (slide) {
        $slideContainter.siblings('h1').animate({
        color: colorSettings.headline[slide]
    }, 1000 );

    $slideContainter.parentsUntil('.section').animate({
        backgroundColor: colorSettings.section[slide]
    }, 1000 );
    };

// Initial call to set color
changeColors(0);

$slider.on('beforeChange', function(event, slick, currentSlide, nextSlide){
    changeColors(nextSlide);
});
Comments