随机缓动Jquery

时间:2015-04-05 17:44:08

标签: javascript jquery

我只是想知道一种使用jQuery animate()函数执行自定义或randon缓动的方法。

我知道有一个包含大量缓动的插件,但我想知道是否有添加自定义缓动的方法

所以不是线性的:

111111111111111111111111111111111111 (velocity)

或摇摆:

00011111222222444422222211111000 (velocity)

我得到类似的东西:

0011128833747733322885666111221100 (velocity)

上面的数字代表速度。我想让它随机变得更加逼真,而不是那么安排。

1 个答案:

答案 0 :(得分:0)

定义自定义功能。

jQuery.easing.custom = function( p ){
    var pow2,
		bounce = 4;
    while ( p < ( ( pow2 = Math.pow( 2, --bounce ) ) - 1 ) / 11 ) {}
		return 1 / Math.pow( 4, 3 - bounce ) - 7.5625 * Math.pow( ( pow2 * 3 - 2 ) / 22 - p, 2 );
};
console.log(jQuery.easing.custom(5756836523456436));
$("button").animate({
    width: "70%",
    opacity: 0.6
}, 2000, "custom", function(){
    $( this ).after( "<div>Animation complete.</div>" );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
    animate
</button>