我被困住了。我试图让jquery在页面加载时使用数字生成动画。因此,数字5将在0到9之间切换,直到分配的时间结束,或者如果我有50,则5将经历0-9,而0也将经历0-9。
谢谢大家的答案和建议。
答案 0 :(得分:12)
我很无聊。的 Here 强>:
(function($){
$.fn.extend({
numAnim: function(options) {
if ( ! this.length)
return false;
this.defaults = {
endAt: 2560,
numClass: 'autogen-num',
duration: 5, // seconds
interval: 90 // ms
};
var settings = $.extend({}, this.defaults, options);
var $num = $('<span/>', {
'class': settings.numClass
});
return this.each(function() {
var $this = $(this);
// Wrap each number in a tag.
var frag = document.createDocumentFragment(),
numLen = settings.endAt.toString().length;
for (x = 0; x < numLen; x++) {
var rand_num = Math.floor( Math.random() * 10 );
frag.appendChild( $num.clone().text(rand_num)[0] )
}
$this.empty().append(frag);
var get_next_num = function(num) {
++num;
if (num > 9) return 0;
return num;
};
// Iterate each number.
$this.find('.' + settings.numClass).each(function() {
var $num = $(this),
num = parseInt( $num.text() );
var interval = setInterval( function() {
num = get_next_num(num);
$num.text(num);
}, settings.interval);
setTimeout( function() {
clearInterval(interval);
}, settings.duration * 1000 - settings.interval);
});
setTimeout( function() {
$this.text( settings.endAt.toString() );
}, settings.duration * 1000);
});
}
});
})(jQuery);
测试HTML:
<span id="number"></span>
用法:
$("#number").numAnim({
endAt: 97855,
duration: 3
});
答案 1 :(得分:2)
这是一个简单的解决方案,评论并点亮了jQuery。
基本上,您可以使用setInterval
每X毫秒运行一些函数。如果持续时间已过期,或者您有所需的值,则clearInterval
可以完全停止循环。
工作示例:http://jsfiddle.net/ZDsMa/1
var output, started, duration, desired;
// Constants
duration = 5000;
desired = '50';
// Initial setup
output = $('#output');
started = new Date().getTime();
// Animate!
animationTimer = setInterval(function() {
// If the value is what we want, stop animating
// or if the duration has been exceeded, stop animating
if (output.text().trim() === desired || new Date().getTime() - started > duration) {
clearInterval(animationTimer);
} else {
// Generate a random string to use for the next animation step
output.text(
''+
Math.floor(Math.random() * 10)+
Math.floor(Math.random() * 10)
);
}
}, 100);
答案 2 :(得分:2)
这里是使用动画效果的jquery随机数字转换器。 http://jsfiddle.net/ZDsMa/94/
var output, started, duration, desired;
// Constants
duration = 5000;
desired = '50';
// Initial setup
output = $('#output');
started = new Date().getTime();
// Animate!
animationTimer = setInterval(function() {
// If the value is what we want, stop animating
// or if the duration has been exceeded, stop animating
if (output.text().trim() === desired || new Date().getTime() - started > duration) {
console.log('animating');
// Generate a random string to use for the next animation step
output.text('' + Math.floor(Math.random() * 990)
);
} else {
console.log('animating');
// Generate a random string to use for the next animation step
output.text('' + Math.floor(Math.random() * 990)
);
}
}, 1000);
您可以更改其中的值
答案 3 :(得分:0)
如果你想在jquery中随机化一个数字,试试
Math.floor(Math.random()*100)
Math.random()
创建一个介于0.0和1.0之间的数字