缩略图自定义滑块的滚动值

时间:2013-04-16 16:37:56

标签: javascript jquery module-pattern

在自定义图像轮播上弄脏了我的手。写了一些jquery让它工作,但坚持一个问题。

问题出在“prev”&点击“下一步”链接:

  • 单击“下一步”时,如果到达最后一个可见项目,则缩略图容器DIV将滚动到负左侧。
  • 总滚动值取决于两个因素
  • 首先,如果剩余超过5个缩略图,则DIV应滚动到(itemWidth x 5)
  • 其次,如果剩余项目小于5则滚动值将为(itemWidth x remainingItems)

如果“prev”&amp ;;如何计算点击滚动值“下一步”按钮点击?

供参考:http://jsfiddle.net/ylokesh/8bHZq/2/

JavaScript代码:

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();
    })

0 个答案:

没有答案
相关问题