制作没有递归的滑块

Tyl*_*ton 4 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();

});
Run Code Online (Sandbox Code Playgroud)

但是该each()方法没有考虑在循环期间添加的节点.

Iva*_*van 5

您可以_cycle()通过启动函数setInterval()来定期更新滑块:

setInterval(function() {
  _cycle2(slider, transition_duration, easing);
}, delay_duration);
Run Code Online (Sandbox Code Playgroud)

请注意,我将原始_cycle()函数重命名为_cycle2(),并删除了delay_duration参数.你可以在这里看到一个有效的演示.

  • setInterval返回一个你可以捕获的值.例如:my_interval = setInterval(function(){...如果保存此值(即在全局范围内),您可以通过执行clearInterval(my_interval)轻松地仅停止此间隔.这将仅停止(清除)此间隔,没有任何其他你或其他模块已经启动.例如:http://www.w3schools.com/jsref/met_win_clearinterval.asp (2认同)