2016-11-30 76 views
2

我想用動畫數出一個數字。我有一個HTML代碼,我設置了數字和一個計算這個數字的函數。問題是我有一個數字95%後面有一個百分號字符。但是這個函數刪除了這個字符。你有什麼想法爲什麼?用動畫計算數字

var fired = 0; 
 
$(document).ready(function() { 
 
\t if(fired === 0) { 
 
\t \t $('.count').each(function() { 
 
\t \t \t $(this).prop('Counter',0).animate({ 
 
\t \t \t \t Counter: $(this).text() 
 
\t \t \t  \t }, { 
 
\t \t \t  \t duration: 3000, 
 
\t \t \t  \t easing: 'swing', 
 
\t \t \t  \t step: function (now) { 
 
\t \t \t  \t $(this).text(Math.ceil(now)); 
 
\t \t \t  \t } 
 
\t \t \t }); 
 
\t \t }); 
 
\t \t fired = 1; //Only do scroll function once 
 
\t } 
 
});
.count { 
 
    font-size: 50px; 
 
    margin-bottom: 10px; 
 
    font-weight: 900; 
 
    text-transform: uppercase; 
 
    display: block; 
 
    padding-top: 15px; 
 
} 
 
#counter { 
 
    float: left; 
 
    display: block; 
 
    width: 28.71111111%; 
 
    text-align: center; 
 
    padding: 0 30px; 
 
} 
 
#counter h3 { 
 
    font-size: 1.2em; 
 
    font-weight: 100; 
 
    text-transform: uppercase; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="counter"> 
 
\t <h3> 
 
\t \t <span class="count customer-satisfaction">95%</span> 
 
\t \t Customer Satisfaction 
 
\t </h3> 
 
</div>

+0

僅爲%符號添加一個範圍。 – Aslam

+0

@hunzaboy我嘗試過,但問題是該函數刪除這個百分比字符;),當我把它放在一個新的跨度跨度以外的CSS形成丟失 – ItsOdi

回答

4

這是因爲$(this).text(Math.ceil(now))插入的數目,如果你告訴函數的%添加到數字像這樣前清除<span class="count">$(this).text(Math.ceil(now) + '%');它會像你想要它。從評論

更新通過OP

原來的情況是,有多個<span class="count">其停止我原來的答覆無法正常工作。通過添加

if ($(this).hasClass('customer-satisfaction')) { 
    $(this).text(Math.ceil(now) + '%'); 
} else { 
    $(this).text(Math.ceil(now)); 
} 

%只會被添加到正確的數字,而不是全部。

var fired = 0; 
 
$(document).ready(function() { 
 
\t if(fired === 0) { 
 
\t \t $('.count').each(function() { 
 
\t \t \t $(this).prop('Counter',0).animate({ 
 
\t \t \t \t Counter: $(this).text() 
 
\t \t \t  \t }, { 
 
\t \t \t  \t  duration: 3000, 
 
\t \t \t  \t easing: 'swing', 
 
\t \t \t  \t step: function (now) { 
 
\t \t \t  \t if ($(this).hasClass('customer-satisfaction')) { 
 
         $(this).text(Math.ceil(now) + '%'); 
 
        } else { 
 
         $(this).text(Math.ceil(now)); 
 
        } 
 
\t \t \t  \t } 
 
\t \t \t }); 
 
\t \t }); 
 
\t \t fired = 1; //Only do scroll function once 
 
\t } 
 
});
.count { 
 
    font-size: 50px; 
 
    margin-bottom: 10px; 
 
    font-weight: 900; 
 
    text-transform: uppercase; 
 
    display: block; 
 
    padding-top: 15px; 
 
} 
 
#counter { 
 
    float: left; 
 
    display: block; 
 
    width: 28.71111111%; 
 
    text-align: center; 
 
    padding: 0 30px; 
 
} 
 
#counter h3 { 
 
    font-size: 1.2em; 
 
    font-weight: 100; 
 
    text-transform: uppercase; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="counter"> 
 
\t <h3> 
 
\t \t <span class="count customer-satisfaction">95%</span> 
 
\t \t Customer Satisfaction 
 
\t </h3> 
 
</div>

+0

但我有3個字段被計數,但只有一個應該包含百分比? – ItsOdi

+0

啊得到它;)'如果($(本).hasClass( '顧客滿意')){ \t \t \t \t \t \t $(本)的.text(Math.ceil(現在)+ '%'); \t \t \t \t \t}其他{ \t \t \t \t \t \t $(本)的.text(Math.ceil(現)); \t \t \t \t \t}' – ItsOdi

+1

你有沒有去,希望我幫忙! :) @ItsOdi – Simplicity

1

CSS解決方案:

如果你想通過CSS將它們添加到所有的罪名,你可以做這樣的:

.count:after{content: '%'} 

如果你想要做到客戶滿意跨度,你可以這樣做:

.customer-satisfaction:after{content: '%'} 
+0

這會將'%'添加到所有''這不是OP所尋找的。 – Simplicity

+0

@Simplicity,認真?我確信OP瞭解它,並會根據他/她的要求使用它。 – Aslam

+0

如果你看看我對答案的評論,你會看到他這麼說。 – Simplicity