简体   繁体   English

使用 CSS 和 jQuery 将数字动画化到单独的跨度中

[英]Animate the digits into the separate span using CSS and jQuery

I am using this code to animate the digits.我正在使用此代码为数字设置动画。 Is there any way to animate each digit into separate span.有没有办法将每个数字动画成单独的跨度。 I need to give padding with background for each digit and need space between each digit.我需要为每个数字提供背景填充,并且每个数字之间需要空格。

Number is starting from 0 which is expected functionality.数字从 0 开始,这是预期的功能。

实现这一点:

 $('.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://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"> <span class="count">200</span> </div>

Please try this.请试试这个。

 var total_digit = $('#test').attr('data-count').toString().length; var countobj=$('.count').get(0); for (var i=1;i<total_digit;i++){ $("#test").append(' <span class="count"> 0 </span>') } $('.count').each(function () { $(this).prop('Counter',0).animate({ Counter: $('#test').attr('data-count') }, { duration: 5000, easing: 'swing', step: function (now) { var num =Math.ceil(now).toString() num = num.padStart(total_digit, '0') for (var i=1;i<=total_digit;i++){ $('#test span:nth-child('+i+')').text(num.charAt(i-1)) } } }); });
 span{ margin:15px; background:red; padding:15px; color:white; direction: rtl; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test" data-count="200"> <span class="count"> 0 </span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM