制作一个没有递归的滑块

Making a slider without recursion

本文关键字:递归 一个      更新时间:2023-09-26

给定以下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()功能,定期更新滑块:

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

注意,我将原来的_cycle()函数重命名为_cycle2(),并删除了delay_duration参数。您可以在这里看到一个工作演示。

你不会想要任何类似while(true)的东西。

你的问题源于这样一个事实,你正在创建一个静态函数,然后试图找出如何动画的孩子与保持它所有在你的静态函数的范围内的约束。

你应该为每个元素创建一个对象的实例,让对象维护滑块的状态,并通过滑块操作所需的实现来更新该状态。

http://jsfiddle.net/qjVJF/3/

    (function ($) {
    var $b = $.behaviors || {};
    $b.slider = function(element, options) {
        this.element = $(element);
        this.panels = this.element.find('.slide');
        this.options = $.extend({}, $b.slider.defaults, options);
        this.currentPanel = 0;
        var horizontal = (this.options.direction == 'left' || this.options.direction == 'right');
        var anti = (this.options.direction == 'left' || this.options.direction == 'up');
        var distance  = horizontal ? '600' : '150';
        this.action = anti ? '-='+distance : '+='+distance;
        this.origin = anti ? distance : 0-distance;
        this.edge = horizontal ? 'left' : 'top';
        this.animation = horizontal ? { "left": this.action } : { "top" : this.action };
        this.panels.css(this.edge, this.origin+'px').show().first().css(this.edge, '0px');
        this.delayNext();
        return this;
    }
    $b.slider.defaults = {
        delay:      500,
        direction:  'left',
        easing:     'swing',
        transition: 3000
    };
    $b.slider.prototype = {
        delayNext: function() {
            setTimeout($.proxy(this.slideNext, this), this.options.delay);
        },
        slideNext: function() { 
            var current = this.panels[this.currentPanel % this.panels.length]; 
            var next = $(this.panels[++this.currentPanel % this.panels.length])
                .css(this.edge, this.origin+'px'); 
            var plugin = this;
            next.add(current).animate(
                this.animation,
                this.options.transition, 
                this.options.easing, 
                function() { 
                    if (this == current) plugin.delayNext();
                }
            );
        }  
    };
    $.fn.cycle = function (options) {
            return this.each(function() { 
                    $(this).data('bCycle', new $b.slider(this, options));
                });
    };
    }(jQuery));
    jQuery(document).ready(function () {
        jQuery('.slider').cycle();
    });

也许这个插件http://docs.jquery.com/Plugins/livequery可以帮助你?

Live Query利用jQuery选择器的强大功能,为匹配的元素自动绑定事件或触发回调,甚至在加载页面并更新DOM之后也是如此。

例如,您可以使用以下代码将单击事件绑定到所有a标记,甚至是您可能通过AJAX添加的任何a标记。
    $('a') 
      .livequery('click', function(event) { 
        alert('clicked'); 
        return false; 
    });