Carousel添加autoscroll(jQuery)

时间:2014-04-07 12:35:57

标签: jquery html

我正在使用本教程创建jQuery Carousel(添加少量内容)http://jqueryfordesigners.com/jquery-infinite-carousel/

这是我的代码

(function ($) {
$.fn.infiniteCarousel = function () {
    function repeat(str, num) {
        return new Array(num + 1).join(str);
    }

    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(true) + 70,

            visible = Math.ceil($wrapper.innerWidth() / singleWidth),
            currentPage = 1,
            pages = Math.ceil($items.length / visible);

        if (($items.length % visible) !== 0) {
            for(var i = 0; i <= visible - ($items.length % visible); i++)
            {
                 var $clone = $items.filter(':nth-child('+ i +')').clone().addClass('empty');
                $slider.append($clone);
            }

            $items = $slider.find('> li');
        }

        $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));

        $wrapper.scrollLeft(singleWidth * visible);

        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

            $wrapper.filter(':not(:animated)').animate({
                scrollLeft: '+=' + left
            }, 2000, function () {
                if (page === 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    page = 1;
                }
                currentPage = page;
            });
            return false;
        }

        $('a.back').click(function (e) {
            e.preventDefault();
            return gotoPage(currentPage - 1);
        });

        $('a.forward').click(function (e) {
            e.preventDefault();
            return gotoPage(currentPage + 1);
        });

        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });
    };
})(jQuery);

$(document).ready(function () {
    $('.infiniteCarousel').infiniteCarousel();
});

http://jsfiddle.net/eVw2B/60/
请告诉我,如何在我的Carousel中添加autoscroll?

1 个答案:

答案 0 :(得分:3)

我已经在一周前回答了你的类似问题 如果您希望Gallery自动启动,您必须确保通过计时器调用您的功能 在这个fiddle我只添加一行代码:

var autoScroll = setInterval(function(){gotoPage(currentPage + 1)},10000);