Caspert
Caspert

Reputation: 4363

How to highlight active slides in Flexslider carousel?

Is it possible to highlight the current items in Flexslider carousel, so I can add a class to the active ones?

I use the basic setup of the Flexslider carousel. Now I would like to have a script that will added active to each slide inside the viewport that is visible.

This example I use: http://flexslider.woothemes.com/basic-carousel.html

I now how to get the Flexslider properties in the start function. When I give the function the parameter I can ask for - the total amount of items in the ul, how many items it will move, and how many paging there will be. I don't know if it is possible to write a formule to calculate which one is active with the given variables, so I can highlight them.

$('.flexslider').flexslider({
        animation: "slide",
        animationLoop: false,
        slideshow: false,
        itemWidth: 460,
        itemMargin: 30,
        minItems: 2,
        maxItems: 6,
        controlNav: false,
        customDirectionNav: ".custom-direction li a",
        start: function(slider) {
            var pagingCount = slider.pagingCount;
            var move = slider.move;
            var visibleSlides = slider.visible;
            var totalSlides = slider.count;

            console.log(slider);
            console.log(slider.move);
            console.log(slider.update);


            if (totalSlides) {
                $('.slides li').addClass('active-slides');

            };
            // console.log(slider.count);
            // console.log(slider.currentSlide);
        }

    });

I have made a start, but doesn't know how the if statement or for loop should look like to get this done.

Any ideas? Thanks in advance.

Upvotes: 4

Views: 4000

Answers (2)

Typhado
Typhado

Reputation: 143

Used the accepted answer above but needed to make a couple amendments to make it work for me shown below.

Mostly handling some exceptions, dealing with half shown slides and when the move/visible are different.

window.addCurrentSlidesClass = function (slider) {
  jQuery('.flexslider li').removeClass("active-slide");

  // get visible by math rather than slider variable due to half shown slides not counting
  // var visible = slider.visible;
  var visible = Math.ceil(slider.w / slider.itemT);

  var startli = (slider.move * (slider.currentSlide));
  // var endli = (slider.move * (slider.currentSlide + 1));
  var endli = startli + visible;

  // Don't push past when you reach end of list
  if(endli > slider.count) {
    startli = startli - (endli - slider.count);
    endli = endli - (endli - slider.count);
  }

  for (i = startli + 1; i <= endli; i++) {
    jQuery('.flexslider li:nth-child(' + i + ')').addClass('active-slide');
  }
};

Upvotes: 0

Kishore Sahasranaman
Kishore Sahasranaman

Reputation: 4230

use the events start and 'after' to handle this. see the below example.

$('.flexslider').flexslider({
    animation: "slide",
    animationLoop: false,
    slideshow: false,
    itemWidth: 460,
    itemMargin: 30,
    minItems: 2,
    maxItems: 6,
    controlNav: false,
    customDirectionNav: ".custom-direction li a",
    start: function (slider) {
        window.addCurrentSlidesClass(slider);
    },
    after: function (slider) {
        window.addCurrentSlidesClass(slider);
    }
});

window.addCurrentSlidesClass = function (slider) {
    //debugger;
    $('.flexslider li').removeClass("active-slides");
    var startli = (slider.move * (slider.currentSlide));
    var endli = (slider.move * (slider.currentSlide + 1));
    for (i = startli + 1; i <= endli; i++) {
        $('.flexslider li:nth-child(' + i + ')').addClass('active-slides');
    }
}
<div class="flexslider carousel">
    <div class="flex-viewport" style="overflow: hidden; position: relative;">
        <ul class="slides" style="width: 2400%; transition-duration: 0.6s; transform: translate3d(0px, 0px, 0px);">
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_donut.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_donut.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_donut.jpg" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
            </li>
        </ul>
    </div>
    <ol class="flex-control-nav flex-control-paging">
        <li><a class="flex-active">1</a>

        </li>
        <li><a class="">2</a>

        </li>
        <li><a class="">3</a>

        </li>
    </ol>
    <ul class="flex-direction-nav">
        <li class="flex-nav-prev"><a class="flex-prev flex-disabled" href="#" tabindex="-1">Previous</a>

        </li>
        <li class="flex-nav-next"><a class="flex-next" href="#" tabindex="-1">Next</a>

        </li>
    </ul>
    <div class="flex-pauseplay"><a class="flex-pause">Pause</a>

    </div>
</div>

DEMO

Upvotes: 2

Related Questions