Getting Index of Current Item in JCarousel

前端 未结 11 1712
夕颜
夕颜 2021-02-05 20:25

I am attempting to get the index of the current item in a JCarousel so that I can display the current position within the Carousel to the user. For example, \'13/20\'.

H

11条回答
  •  忘了有多久
    2021-02-05 20:58

    I too have found that jCarousel returns unusable .first, .last, .prevFirst and .prevLast properties at the times that you need them.

    Hence, I had to do it the dirty way and decided to write a function that returns the ID of whichever li tag is currently the first in the container, by reading whether or not it's left-offset is above zero. If so, and it's the first one with an above-zero left position, it's my current slide.

    The following code assumes that you've put an id="listitem-N" in the list tags in your foreach() loop, where N is the current iteration.

    var currSlide = 0;
    
    function getCurrentCarouselItem(){
    
        $("ul#use-cases li.use-case").each(function(){
    
            var leftPos = $(this).offset().left;
    
            if( leftPos >= 0){
    
                var id = $(this).attr('id').split("-");
    
                currSlide =  id[1];
    
                return false;
    
            }
    
        });
    
        return currSlide;
    
    }
    

    The reason I don't return the id in the each() is because each() is a function and the return will only return for that function, not getCurrentCarouselItem().

提交回复
热议问题