简体   繁体   English

jQuery渐变动画

[英]jQuery gradual animation

I have this issue with my jquery plugin atm which is used alongside jquery mobile. 我与jquery mobile一起使用的jquery插件atm有此问题。 I have an animate function which looks like this: 我有一个动画函数,如下所示:

    animateWheel: function (self, speed, count) {
        var $item = $(self.settings.itemClass).first();
        var width = $item.outerWidth(true);

        $item.animate({ "left": "-" + width + "px" }, {
            duration: speed,
            easing: "linear",
            step: function (now, fx) {
                $(self.settings.itemClass + ":gt(0)").css("left", now);
            },
            complete: function () {
                self.moveItem(self, speed, count);
            }
        });
    },
    moveItem: function (self, speed, count) {
        if (count > 0) {
            var $item = $(self.settings.itemClass).first();
            $item.hide(speed, "linear", function () {
                $(this).appendTo(self.settings.itemContainerClass).show(speed, "linear");
                self.moveItem(self, speed, count - 1); // Repeat
            });
        }
    }

As you can see when animateWheel is called, it gets the first item and then animates it. 如您所见,当调用animateWheel时,它将获取第一项,然后对其进行动画处理。 The step function animates the rest of the items (there could be hundreds). 步进功能会为其余项目设置动画(可能有数百个)。 This animation only happens once. 该动画仅发生一次。 When the animation is complete it calls moveItem which you can see moves the first item to the end of the array of elements and calls moveItem again until the counter is at 0. These all using the linear easing to make it looks smooth. 动画完成后,它将调用moveItem ,您可以看到将第一个项目移动到元素数组的末尾,并再次调用moveItem直到计数器为0。所有这些都使用线性缓动使其看起来很平滑。

This all works fine and can be viewed at [url=http://www.r3plica.co.uk]Winner Select[/url] but my issue is that I want the animation to start slowly and then speed up and finally slow down and stop. 一切正常,可以在[url = http://www.r3plica.co.uk]优胜者选择[/ url]上查看,但我的问题是我希望动画先缓慢开始然后加速然后最终慢下来停下来 To get a decent Idea, imagine wheel of fortune and that should give you an idea of what I am after. 要获得一个体面的想法,想象一下命运之轮,那应该让您对我的追求有所了解。 The problem is, I am unsure how to do this. 问题是,我不确定如何执行此操作。

What I think needs to be done, is to alter the speed as the function ( moveItem ) is called starting from a low speed, then speeding up and finally slowing down to a stop. 我认为需要做的是更改速度,因为从低速开始调用该函数( moveItem ),然后加快速度,最后减速至停止。

My html looks like this: 我的html看起来像这样:

<div data-role="page" id="carouselPage">

    <div data-role="header">
        <img src="assets/css/images/logo.jpg" />
    </div>

    <div data-role="content">
        <div id="carousel" class="carousel">
            <div class="carousel-container">

                <div class="carousel-item red">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item orange">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item green">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item navy">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item blue">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item teal">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item red">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item orange">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item green">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item navy">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item blue">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

                <div class="carousel-item teal">
                    <div class="carousel-photo">
                        <img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
                    </div>
                    <div class="carousel-content ">
                        <p>Jaymie<br />Jeffrey</p>
                        <span class="twitter">
                            <p>@JaymieJeffrey</p>
                        </span>
                    </div>
                </div>

            </div>
        </div>


    </div><!-- /content -->

</div><!-- /page -->

If anyone can help, I would appreciate it :) 如果有人可以帮助,我将不胜感激:)

/r3plica / r3plica

Update 1 更新1

I have now managed to get some sort of easing involved by doing this: 通过执行此操作,我现在已经设法实现了某种放松:

animateWheel: function (self, speed) {
    var $item = $(self.settings.itemClass).first();
    var width = $item.outerWidth(true);

    $item.animate({ "left": "-" + width + "px" }, {
        duration: speed,
        easing: "linear",
        step: function (now, fx) {
            $(self.settings.itemClass + ":gt(0)").css("left", now);
        },
        complete: function () {
            var random = Math.random() * 5;
            var duration = 1000 + Math.floor(1000 * random); // min: 1000, max: 6000

            $({ speed: 0 }).animate({ speed: 100 }, {
                duration: duration,
                easing: 'easeInBack', // can be anything
                step: function () { // called on every step
                    var $item = $(self.settings.itemClass).first();

                    $item.hide(Math.ceil(this.speed), "linear", function () {
                        $(this).appendTo(self.settings.itemContainerClass).show(this.speed, "linear");
                    });
                }
            });
        }
    });
}

You can see the results at http://www.r3plica.co.uk 您可以在http://www.r3plica.co.uk上查看结果

Maybe try messing with different easing types here is a really useful cheat sheet I use. 也许尝试将不同的宽松类型弄混,这是我使用的非常有用的备忘单。

http://easings.net/ http://easings.net/

I was thinking you would probably want something like easeInOutBack maybe? 我当时在想,您可能想要诸如easeInOutBack之类的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM