这里已经有几个很好的答案。我会建议验证你的HTML。还有一些CSS在涉及滚动时引起奇怪(固定背景图像不滚动)。
我采取了与其他人稍有不同的方法。而不是使用setInterval,我去了$ .animate和一个步骤函数。和其他人一样,我选择了一个班级来瞄准每一个项目:“填满我”。
小提琴:http://jsfiddle.net/LFbKs/6/
注意:检查小提琴,因为我修改HTML(非常轻微)和CSS到更大的程度。
// for each item we need to "fill up"
$('.fill-me-up').each(function(){
// cache DOM references
var this$ = $(this)
, bar$ = this$.find('.bar')
, show$ = this$.find('.show')
, span$ = bar$.find('div span')
// the target percentage height for this item
, p = span$.text()
// combine '.bar' and '.show' so we can apply the animation to both
, toAnimate = $().add(bar$).add(show$);
// add class causing fade-in
bar$.find('div').addClass('is-visible');
// animate height to target height over 2 seconds and
// at each step, update the span with a truncated value
toAnimate.animate(
{ height: p+'%' },
{
duration: 2000,
step: function(currentStep) {
span$.html('%'+currentStep.toFixed(0));
}
}
);
});
干杯
将不得不更加具体。你试过的东西太含糊。 –
那么你的问题是什么?如果使用多个元素,则必须定位特定元素 –
很显然,因为您在执行'percent = $('#bar div span')时使用了第一个'span'中存储的元素。“html()' – Colandus