javascript - 不用递归制作 slider

标签 javascript jquery slider

给定以下 jsFiddle,如何在不构建堆栈的情况下实现与我所做的相同的效果?

http://jsfiddle.net/YWMcy/1/

我试过这样做:

jQuery(document).ready(function () {
    'use strict';
    (function ($) {

        function validateOptions(options) {
            if (typeof(options.delay) == typeof(0)) {
                $.error('Delay value must an integer.');
                return false;
            } else if (options.delay < 0) {
                $.error('Delay value must be greater than zero.');
                return false;
            }

            if (typeof(options.direction) == typeof('')) {
                $.error('Direction value must be a string.');
                return false;
            } else if (!(options.direction in ['left', 'right', 'up', 'down'])) {
                $.error('Direction value must be "left", "right", "up", or "down".');
                return false;
            }

            if (typeof(options.easing) == typeof('')) {
                $.error('Easing value must be a string.');
                return false;
            }

            if (typeof(options.selector) == typeof('')) {
                $.error('Selector value must be a string.');
                return false;
            }

            if (options.transition < 0) {
                $.error('Transition value must be greater than zero.');
                return false;
            }
            return true;
        }

        var methods = {
            init:   function (options) {

                return this.each(function () {

                    var settings = {
                        delay:      5000,
                        direction:  'left',
                        easing:     'swing',
                        selector:   '*',
                        transition: 3000
                    };

                    if (options) {
                        $.extend(settings, options);
                    }

                    $(this).css({
                        overflow:   'hidden',
                        position:   'relative'
                    });

                    var styles = {
                        left:       0,
                        position:   'absolute',
                        top:        0
                    };

                    switch (settings.direction) {
                    case 'left':
                        styles.left = $(this).width() + 'px';
                        break;
                    case 'right':
                        styles.left = -$(this).width() + 'px';
                        break;
                    case 'up':
                        styles.top = $(this).height() + 'px';
                        break;
                    case 'down':
                        styles.top = -$(this).height() + 'px';
                        break;
                    default:
                        jQuery.error('Direction ' + settings.direction + ' is not valid for jQuery.fn.cycle');
                        break;
                    }

                    $(this).children(settings.selector).css(styles).first().css({
                        left:   0,
                        top:    0
                    });

                    if ($(this).children(settings.selector).length > 1) {
                        $(this).cycle('slide', settings);
                    }
                });
            },

            slide:  function (options) {
                return this.each(function () {

                    var settings = {
                        delay:      5000,
                        direction:  'left',
                        easing:     'swing',
                        selector:   '*',
                        transition: 3000
                    }, animation, property, value;

                    if (options) {
                        $.extend(settings, options);
                    }

                    switch (settings.direction) {
                    case 'left':
                        animation = {left: '-=' + $(this).width()};
                        property = 'left';
                        value = $(this).width();
                        break;
                    case 'right':
                        animation = {left: '+=' + $(this).width()};
                        property = 'left';
                        value = -$(this).width();
                        break;
                    case 'up':
                        animation = {top: '-=' + $(this).height()};
                        property = 'top';
                        value = $(this).height();
                        break;
                    case 'down':
                        animation = {top: '+=' + $(this).height()};
                        property = 'top';
                        value = -$(this).height();
                        break;
                    default:
                        jQuery.error('Direction ' + settings.direction + ' is not valid for jQuery.fn.cycle');
                        break;
                    }

                    $(this).children(settings.selector + ':first-child').each(function () {
                        $(this).delay(settings.delay);
                        $(this).animate(
                            animation,
                            settings.transition,
                            settings.easing,
                            function () {
                                $(this).css(property, value);
                            }
                        );
                    });

                    $(this).append($(this).children(settings.selector + ':first-child').detach());

                    $(this).children(settings.selector + ':first-child').each(function () {
                        $(this).delay(settings.delay);
                        $(this).animate(
                            animation,
                            settings.transition,
                            settings.easing,
                            function () {
                            $(this).parent().cycle('slide', settings);
                            }
                        );
                    });
                });
            }
        };

        jQuery.fn.cycle = function (method, options) {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method ' + method + ' does not exist on jQuery.fn.cycle');
            }
        };
    }(jQuery));

    jQuery('.slider').cycle();

});

但是 each() 方法不考虑在循环期间添加的节点。

最佳答案

您可以通过 setInterval() 启动您的 _cycle() 函数,以定期更新 slider :

setInterval(function() {
  _cycle2(slider, transition_duration, easing);
}, delay_duration);

请注意,我将您原来的 _cycle() 函数重命名为 _cycle2(),并删除了 delay_duration 参数。你可以see a working demo here .

关于javascript - 不用递归制作 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7211815/

相关文章:

javascript - 在 acrobat PDF 中分配 mouseUp 事件

php - 使用 jquery 导出 PDF、CSV、Excel 表单数据表?

JavaScript 初始化 + html5 标签

javascript - 按类名获取元素并更改类名

javascript - 移动一个与鼠标移动相反的大 div

javascript - JS 中的混​​色器不起作用

javascript - jQuery 1.8.1 data() 检索 HTML5 数据属性时出错

javascript - JQuery:创建一个由我选择的选项的数据属性组成的数组

jquery - 是否有与此 Flash 效果等效的 jQuery? (滑动效果)

css - Wordpress WooCommerce 更改 "Featured Products" slider 中元素的间距