简体   繁体   English

如何停止和重置对象中的几个动画元素? (几个单独的动画)

[英]How to stop and reset few animated elements in object? (several separate animations)

I have created a few animations and each of them should be a separate slide on the main site slider. 我已经创建了一些动画,每个动画都应该是主站点滑块上的单独幻灯片。
Thats why, I've got from my boss an object structure (code below) and he said I should use it to those animations. 那就是为什么,我从老板那里得到了一个对象结构(下面的代码),他说我应该在那些动画中使用它。

var RDAnimation = function(o){
var f = $.extend({
    start: function() {
        return true;
    },
    pause: function() {
        return true;
    },
    reset: function() {
        return true;
    },
    stop: function() {
        if (this.pause()) {
            return this.reset();
        }
    },
    vars: {}
},o);
this.start = f.start;
this.pause = f.pause;
this.reset = f.reset;
this.stop = f.stop;
this.vars=f.vars;
};

But because I don't have a much of experience with working on objects, I just paste all of the animating function into 'start', just to see what will happen (code below). 但是因为我没有太多处理对象的经验,所以我只是将所有动画功能粘贴到“开始”中,只是想知道会发生什么(下面的代码)。

var anim3=new RDAnimation({

    start: function() {
        var $this = this;

function bars() {
var barHeight = 0;

$('.chart-bar').each(function () {

    barHeight = Math.floor(Math.random() * 100);

    $(this).delay(1000).animate({
        'height': barHeight + '%',
        'min-height': '20px'},
        700, function(){

            bars();   
     });

});

}

var count = 0;

function badgeClone() {

var badge1 = $('.badge');
var badge2 = $('.badge').clone();

var badgeWidth = badge1.width();
var badgeHeight = badge1.height();

    //badge1.text(count);

    badge1.after(badge2);
    badge2.css({
        'line-height': '180px',
        'text-align': 'center',
        'font-size': '70px',
        'font-weight': 'bold',
        'color': '#fff',
        'opacity':'0'
    });

    badge2.animate({
        'width': badgeWidth * 2 + 'px',
        'height': badgeHeight *2 + 'px',
        'line-height': '360px',
        'font-size': '140px',
        'top': '-150px',
        'right': '-100px'
        },
        100, "linear", function() {


            if(count >= 9) {
                count = 1
            } else {
                count++
            }

            badge2.text(count);


            badge2.delay(300).animate({
                'width': badgeWidth + 'px',
                'height': badgeHeight + 'px',
                'line-height': '180px',
                'font-size': '70px',
                'top': '-60px',
                'right': '-40px',
                'opacity': '1'},
                100, "linear", function(){
                    badge1.fadeOut(600, function(){
                        $(this).remove();
                    });
                });
        });  

}

bars();

var chartbars = $('.chart-bar');

$(chartbars).each(function(i) {

    chartbar = chartbars[i];

    var leftPos = i * 24 + 4;

    $(chartbar).css({'left': leftPos + 'px'});

});

// ppl animations

var height = $('.person').height();    

$('.person').css({'top': -height + 'px'});

var female = function(){
    $('.female').fadeIn(300).animate({'top': '0px'}, 2500, function(){

        male();

        badgeClone();

        $(this).delay(1000).fadeOut(500, function() {
            $(this).css({'top': -height + 'px'});
        });

    });
};

var male = function(){

    $('.male').fadeIn(300).animate({'top': '0px'},2500,function(){
        badgeClone();

        female();

            $(this).delay(1000).fadeOut(500, function() {
            $(this).css({'top': -height + 'px'});
        });
    });
};

male();

   },
   pause: function() {
       var $this = this; 

       $('.chart-bar').stop();


   },
   reset: function() {
        var $this = this;

    $this.count = 0;

   },
    vars: {


   }
});

And it's working! 它正在工作! Animation will start when I run anim3.start . 当我运行anim3.start时,动画将开始。 But now.. How can I stop it? 但是现在..我怎样才能阻止它? Because I have to stop it when user chose another animated slide, and start it from beginning when user again chose this slide. 因为我必须在用户选择另一个动画幻灯片时停止它,并在用户再次选择此幻灯片时从头开始。

I've wrote a simple code, just for test.. But it doesnt work. 我已经编写了一个简单的代码,仅用于测试..但它不起作用。 In code above I've wrote $('.chart-bar').stop(); 在上面的代码中,我写了$('.chart-bar').stop(); in pause, to check if this will stop at least one chart-bar animation but it doesnt. 在暂停中,检查这是否将停止至少一个图表栏动画,但不会停止。 In reset I've wrote $this.count = 0; 在复位中,我写了$this.count = 0; to reset number on the .badge element - but it doesnt work as well.. 重置.badge元素上的数字-但它也不起作用。

Obviously, to run those lines of code I've put a simple code on the bottom of script (code below). 显然,要运行这些代码行,我已在脚本底部放置了一个简单代码(下面的代码)。

$('#slider-2, #slider-3').hide();
$('.tour-slider').on('click', 'li a', function(e){
    e.preventDefault();
    $(this).closest('.slider').hide();
    var sliderHref = $(this).attr('href');
    $(sliderHref).fadeIn();
    if(sliderHref == '#slider-1'){
        anim1.start();
        anim3.pause();

    }else if(sliderHref == '#slider-2'){
        anim2.start();  
        anim3.pause();
    }else if(sliderHref == '#slider-3'){
        anim3.reset();
        anim3.start();   
        anim3.pause();
    }
});

As you can see, I even run a anim3.pause(); 如您所见,我什至运行anim3.pause(); at once, after I start it - just to check, if it will stop immediately - but even one chart-bar didn't stop.. 在我启动它之后立即-仅检查它是否将立即停止-但即使是一个图表栏也没有停止。

So now, can somebody tell my please, how can I stop (and reset) these animated elements? 所以现在,有人可以告诉我,我如何停止(重置)这些动画元素? I just need to get one working example and rest I can do by my own. 我只需要得到一个可行的例子,剩下的我就可以自己做。

I will be grateful for any help 我将不胜感激

Cheers! 干杯!

Ok, I found a solution. 好的,我找到了解决方案。

1st of I modify a bars() function (add if statement) 我的第一项修改了bars()函数(添加if语句)

        function bars() {

        var barHeight = 0;
        var loop = true;

        $('.chart-bar').each(function () {

            barHeight = Math.floor(Math.random() * 100);

            $(this).delay(1000).animate({
                'height': barHeight + '%',
                'min-height': '20px'},
                700, function(){

            if (loop) {
                bars();   
            }

             });
        });

    }

And then all I need was those two line of code added into my pause method: 然后,我所需要的只是将两行代码添加到我的pause方法中:

   pause: function() {
       var $this = this;
       $('#animation-3').find('*').stop(true, false);
       $this.loop = false;
   }

Thanks everyone for your time :) 谢谢大家的时间:)

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

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