jQuery 动画未执行

发布于 2024-12-03 15:25:18 字数 4719 浏览 0 评论 0原文

我一直在写一个 jQuery 循环插件。

首先,动画永远不会发生。默认情况下,每张“幻灯片”都会在那里停留八秒钟,然后不会在没有过渡的情况下“弹出”到下一张图像。下面是我的代码。我通过 JSLint 运行它,唯一出现的问题是第 103 行从未使用过 options。我猜测该代码块与我的插件无法按预期工作的原因有关。可以在 http://corolla.tylercrompton.com/ 中查看实例。

其次,这是制作没有递归的滑块的后续问题,其中我的脚本是不是插件。不过,它确实有效,我仍在寻找答案。谢谢。

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

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

                    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 = 'right';
                        value = 0;
                        break;
                    case 'up':
                        animation = {top: '-=' + $(this).height()};
                        property = 'top';
                        value = $(this).height();
                        break;
                    case 'down':
                        animation = {top: '+=' + $(this).height()};
                        property = 'top';
                        value = 0;
                        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();

});

I have been writing a jQuery cycle plugin.

Firstly, the animation never happens. Each "slide" sits there for eight seconds by default and never then "pops" to the next image with no transition. Below is my code. I ran it through JSLint and the only thing that came up was that options is never used on line 103. I'm guessing that block of code has something to do with why my plugin is not working as expected. A live example can be seen at http://corolla.tylercrompton.com/.

Secondly, this is a follow question to Making a slider without recursion in which my script is not a plugin. It did work, though, I am still seeking an answer toward that. Thanks.

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

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

                    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 = 'right';
                        value = 0;
                        break;
                    case 'up':
                        animation = {top: '-=' + $(this).height()};
                        property = 'top';
                        value = $(this).height();
                        break;
                    case 'down':
                        animation = {top: '+=' + $(this).height()};
                        property = 'top';
                        value = 0;
                        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();

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-12-10 15:25:18

这实际上是一个 CSS 错误,而不是 JavaScript/JQuery 错误。我从来没有将位置设置为绝对。我更新了我链接的问题中的 JavaScript,以反映感兴趣的人的最新版本。

It was actually a CSS error rather than a JavaScript/JQuery error. I was never setting the positions to absolute. I updated my JavaScript in the question that I linked to reflect the most up to date version for those that are interested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文