2015-12-10 109 views
2

我研究了不同的解决方案,例如我用一个按钮改进的这个解决方案:http://jsfiddle.net/3mhJJ/用Angularjs创建一个条形音箱

$(".button").click(function(){ 
    $(".bar").each(function(i) {fluctuate($(this));}); 
}); 

我希望能够暂停录制,同时也停止动画时声音过(我不包括,但试想一下,声音持续10秒)

我从来没有使用jQuery,但我熟悉AngularJS,你认为有什么方法可以让我只用JQuery或者将JQuery和AngularJS混合在一起?

否则,我想重新创建类似Soundcloud的内容,但我不确定它有多难。

感谢您的建议。

回答

1

不能完全确定,我明白了你之后,但在这里是如何开始/停止动画:

http://jsfiddle.net/kyysdo4n/2/(更新,包括自动停止功能)

var stopAnimation = false; 
var closeTimeoutHandle = null; 

function fluctuate(bar) { 
    if(stopAnimation){ 
     return; 
    } 

    var amplitude = Math.random() * 10; 
    console.log(amplitude); 
    var height = amplitude * 4; 
    //Animate the equalizer bar repeatedly 
    bar.animate({ 
     height: height 
    }, function() { 
     fluctuate($(this)); 
    }); 
} 

$("#btnStart").click(function(i) { 
    stopAnimation = false; 
    fluctuate($('.bar')); 

    closeTimeoutHandle = setTimeout(function(){ 
     stopAnimation = true; 
    }, 10 * 1000); //10 seconds 
}); 


$("#btnStop").click(function(i) { 
    stopAnimation = true; 
    fluctuate($('.bar')); 
    // clear the timeout so that if the user starts the animation again 
    // it doesn't get stopped when the initial timeout gets called 
    if(closeTimeoutHandle){ 
     clearTimeout(closeTimeoutHandle); 
    } 
}); 

我不知道这是否有帮助..如果它不,那么请添加更多的细节,也许更多的代码?

+0

谢谢你的回答:)这正是我一直在寻找的。但是,如果用户忘记点击停止,是否可以在一段时间后停止动画? –

+1

是的,我更新了plunkr:http://jsfiddle.net/kyysdo4n/2/ – sirrocco

+0

非常感谢,这正是我一直在寻找的! –