js实现数字变化

这次给大家带来js实现数字变化,js实现数字变化的注意事项有哪些,下面就是实战案例,一起来看一下。

 $.fn.countTo = function (options) { options = options || {};//当options未被初始化,即typeof options = 'undefined'时,执行后面部分即var options = {}来初始化一个对象 return $(this).each(function () {  // set options for current element  var settings = $.extend({}, $.fn.countTo.defaults, {  from:  $(this).data('from'),  to:  $(this).data('to'),  speed:  $(this).data('speed'),  refreshInterval: $(this).data('refresh-interval'),  decimals: $(this).data('decimals')  }, options);  // how many times to update the value, and how much to increment the value on each update  //更新值多少次,每次更新值多快  var loops = Math.ceil(settings.speed / settings.refreshInterval),  increment = (settings.to - settings.from) / loops;  // references & variables that will change with each update  //引用和变量每次更新将改变  var self = this,//返回html对象  $self = $(this),//返回返回一个jquery对象  loopCount = 0,  value = settings.from,  data = $self.data('countTo') || {};//获取jauery方法对象  $self.data('countTo', data);//赋值  // if an existing interval can be found, clear it first  //如果存在间隔,则清除它  if (data.interval) {  clearInterval(data.interval);  }  data.interval = setInterval(updateTimer, settings.refreshInterval);  // initialize the element with the starting value  //用开始的值初始化  render(value);  function updateTimer() {  value += increment;  loopCount++;  render(value);  if (typeof(settings.onUpdate) == 'function') {   settings.onUpdate.call(self, value);  }  if (loopCount >= loops) {   // remove the interval   $self.removeData('countTo');   clearInterval(data.interval);   value = settings.to;   if (typeof(settings.onComplete) == 'function') {   settings.onComplete.call(self, value);   }  }  }  function render(value) {  var formattedValue = settings.formatter.call(self, value, settings);  $self.html(formattedValue);  } }); }; $.fn.countTo.defaults = { from: 200,  // the number the element should start at to: 0,   // the number the element should end at speed: 1000,  // how long it should take to count between the target numbers refreshInterval: 1, // how often the element should be updated decimals: 0,  // the number of decimal places to show formatter: formatter, // handler for formatting the value before rendering onUpdate: null, // callback method for every time the element is updated onComplete: null // callback method for when the element finishes updating }; function formatter(value, settings) { return value.toFixed(settings.decimals); } // custom formatting example $('#count-number').data('countToOptions', { formatter: function (value, options) { return value.toFixed(options.decimals).replace(/B(?=(?:d{3})+(?!d))/g, ','); } }); // start all the timers $('.timer').each(count); function count(options) { var $this = $(this); options = $.extend({}, options || {}, $this.data('countToOptions') || {}); $this.countTo(options); }

登录后复制

apply与call的简单用法,学习链接。

相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!

推荐阅读:

以上就是js实现数字变化的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2772064.html

(0)
上一篇 2025年3月8日 13:09:14
下一篇 2025年3月1日 02:23:51

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • js封装操作class步奏详解

    这次给大家带来js封装操作class步奏详解,js封装操作class的注意事项有哪些,下面就是实战案例,一起来看一下。 一、添加class function addClass(ele,cName) { var arr = ele.class…

    编程技术 2025年3月8日
    200
  • 怎么操作Node.js发送邮件

    这次给大家带来怎么操作Node.js发送邮件,操作Node.js发送邮件的注意事项有哪些,下面就是实战案例,一起来看一下。 var nodemailer = require(“nodemailer”);var mailTitle=’http…

    编程技术 2025年3月8日
    200
  • Two.js做出物体环绕动画

    这次给大家带来Two.js做出物体环绕动画,Two.js做出物体环绕动画的注意事项有哪些,下面就是实战案例,一起来看一下。 Two.js 是面向现代 Web 浏览器的一个二维绘图 API。Two.js 可以用于多个场合:SVG,Canvas…

    编程技术 2025年3月8日
    200
  • two.js怎样实现动画功能

    这次给大家带来two.js怎样实现动画功能,two.js实现动画功能的注意事项有哪些,下面就是实战案例,一起来看一下。 什么是two.js? Two.js 是面向现代 Web 浏览器的一个二维绘图 API。Two.js 可以用于多个场合:S…

    编程技术 2025年3月8日
    200
  • JS实现图片放大镜效果使用案例

    这次给大家带来JS实现图片放大镜效果使用案例,JS实现图片放大镜效果的注意事项有哪些,下面就是实战案例,一起来看一下。 前  言   我们大家经常逛各种电商类的网站,商品的细节就需要用到放大镜,这个大家一定不陌生,今天我们就做一个图片放大镜…

    编程技术 2025年3月8日
    200
  • js如何使用定时器实现倒计时功能

    这次给大家带来js如何使用定时器实现倒计时功能,js使用定时器实现倒计时功能的注意事项有哪些,下面就是实战案例,一起来看一下。 日期函数 倒计时 =  用 将来的时间  –   现在的时间  问题:将来时间 距离 1970 毫秒…

    编程技术 2025年3月8日
    200
  • Vuejs怎样实现购物车功能

    这次给大家带来Vuejs怎样实现购物车功能,Vuejs实现购物车功能的注意事项有哪些,下面就是实战案例,一起来看一下。 开始更新前端框架Vue.JS的相关博客。 功能概述 学习了Vue.JS的一些基础知识,现在利用指令、数据绑定这些基础知识…

    编程技术 2025年3月8日
    200
  • JS实现音乐切换播放以及轮流播放

    这次给大家带来JS实现音乐切换播放以及轮流播放,JS实现音乐切换播放以及轮流播放的注意事项有哪些,下面就是实战案例,一起来看一下。 welcome .content { width: 600px; margin:0 auto; border…

    编程技术 2025年3月8日
    200
  • JS实现三级级联

    这次给大家带来JS实现三级级联,JS实现三级级联的注意事项有哪些,下面就是实战案例,一起来看一下。 本文实例为大家分享了js实现三级级联特效的具体代码,供大家参考,具体内容如下     //二维数组存放市的信息 var shi =[[“丽水…

    编程技术 2025年3月8日
    200
  • JS+jQuery显示开机时间(精确到秒)

    这次给大家带来JS+jQuery显示开机时间(精确到秒),JS+jQuery显示开机时间的注意事项有哪些,下面就是实战案例,一起来看一下。 实现原理:   给关闭按钮绑定点击事件,点击以后触发动画效果。利用jQuery的animate方法,…

    编程技术 2025年3月8日
    200

发表回复

登录后才能评论