是否可以实现json数据的动画计数js。我试了但是回来了。这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="number"><span class="count">Count</span></div>
<div id="id">Id: </div>
<div id="pos">POS: </div>
<div> Static Number</div>
<div class="count">345678912</div>
<script type="text/javascript">
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
</script>
JS:
var info = {"city":[
{"id":"4","number":"376478","pos":"null"},
{"id":"5","number":"4565878","pos":"3"},]};
var res = info.city.filter(function(item){
return item.id=='4';
function CheckNullReturnBlank(item) {
return item = (item == null) ? 'No Data' : item;
}
});
console.log(res);
var HTML = '<span>'+res[0].number+'</span>';
$('.count').append(HTML);
var HTML ='<span>'+res[0].id+'</span>';
$('#id').append(HTML);
var HTML ='<span>'+res[0].pos+'</span>';
$('#pos').append(HTML);
但如果我使用静态数字测试,动画效果很好。另一件事是,我试图将空值从json替换为No Data Available但没有运气。感谢任何帮助我可以尝试。
这是JSfiddle链接:http://jsfiddle.net/zeef/vLw5wo5p/3/
答案 0 :(得分:1)
有一些问题:
item == null
,而“null”是一个字符串。 item.pos == 'null'
)NaN
,因为在您的第一个.count
范围内,您有“Count”字符串,这不是数字:)CheckNullReturnBlank
功能;
var info = {"city":[
{"id":"4","number":"376478","pos":"null"},
{"id":"5","number":"4565878","pos":"3"},]};
var res = info.city.filter(function(item){
return item.id=='4';
});
res.forEach(function(item) {
item.pos = (item.pos == 'null') ? 'No Data' : item.pos;
});
console.log(res);
var HTML = '<span>'+res[0].number+'</span>';
$('.count').append(HTML);
var HTML ='<span>'+res[0].id+'</span>';
$('#id').append(HTML);
var HTML ='<span>'+res[0].pos+'</span>';
$('#pos').append(HTML);
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="number">Count <span class="count"></span></div>
<div id="id">Id: </div>
<div id="pos">POS: </div>
<div> Static Number</div>
<div class="count">345678912</div>