0

Got my hands dirty on custom image carousel. Wrote some jquery to make it work but stuck with one issue.

Issue lies when "prev" & "next" links are clicked:

  • when click "next", if last visible item is reached then thumbnails container DIV will scroll to negative left.
  • Total scroll value depends on the two factors
  • First, if more than 5 thumbnails are remaining then DIV should scroll to (itemWidth x 5)
  • Second, if remaining items are less then 5 then scroll value will be (itemWidth x remainingItems)

How to calculate the scroll value on click if "prev" & "Next" button click?

For reference : http://jsfiddle.net/ylokesh/8bHZq/2/

JavaScript Code:

var extCarousel = {
        backBtn : $('.backward'),
        fwdBtn : $('.forward'),
        thumbItems : $('.slidetabcontent a'),
        carouselItem : $('.slide'),
        currentItemIndex : $('.currentIndex'),
        totalItemsCount : $('.currentMax'),
        lastItemIndex : $('.slidetabcontent a:last').index(),
        firstItemIndex : $('.slidetabcontent a:first').index(),
        defaultThumbnailSet : 5,

        generateCarousel : function() {
            var _this = this;
            _this.thumbItems.eq('0').addClass('current');
            _this.carouselItem.hide().filter(':first').addClass('active').show().find(_this.currentItemIndex).html($('.slide.active').index() + 1);

            _this.totalItemsCount.html(_this.thumbItems.length);
            _this.controlNavigation();              
        },

        controlNavigation : function() {

            var _this = this,
                currentIndex, lastItem, firstItem;

            // Back Button
            _this.backBtn.on('click', function(e){
                e.preventDefault();
                currentIndex = $('.slide.active').index() - 1,
                firstItem = _this.firstItemIndex - 1;

                if(currentIndex != firstItem) {
                    _this.moveCarousel(currentIndex);
                }
            });

            // Prev Button
            _this.fwdBtn.on('click', function(e){
                e.preventDefault();
                currentIndex = $('.slide.active').index() + 1,
                lastItem = _this.lastItemIndex + 1;

                if(currentIndex != lastItem) {
                    _this.moveCarousel(currentIndex);
                }
            });

            //Thumbnail Click
            this.thumbItems.on('click', function(e) {
                e.preventDefault();
                currentIndex = $('.slide.active').index();
                _this.moveCarousel(currentIndex);
            });

        },

        moveCarousel : function(currIndex) {
            var _this = this;
            _this.carouselItem.removeClass('active').fadeOut('fast').eq(currIndex).addClass('active').fadeIn('fast');
            _this.activateThumbnail(currIndex);
        },

        activateThumbnail : function(currIndex) {
            var _this = this;
            _this.thumbItems.removeClass('current').eq(currIndex).addClass('current');
            _this.setCurrentItemIndex(currIndex + 1);

            if(currIndex == _this.defaultThumbnailSet) {
                _this.slideThumbnails();
            }

        },

        slideThumbnails : function() {
            var _this = this;
            var remainingThumbnails = _this.thumbItems.length - _this.defaultThumbnailSet;
            var itemWidth = _this.thumbItems.width() + parseInt(_this.thumbItems.css('padding').split('px')[0]*2);
            if(remainingThumbnails > 5) {
                $('.slidetabcontent').animate({
                    'left' : -(itemWidth*5)
                }, 'slow');
            } else {
                $('.slidetabcontent').animate({
                    'left' : -(itemWidth*remainingThumbnails)
                },'slow');
            }
        },

        setCurrentItemIndex : function(currIndex) {
            var _this = this;
            _this.currentItemIndex.html(currIndex);
        },

        init : function() {
            var _this = this;
            _this.generateCarousel();
        }
    };
    $(document).ready(function(){
        var extensionCarousel = extCarousel.init();
    })
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50

0 Answers0