這裏已經有幾個很好的答案。我會建議驗證你的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