JS 插件文档库邀你一起协同创作

项目概览JS 插件文档库地址:JS 插件文档库 · 语雀在线演示代码仓库:JS 插件文档库示例代码 · GitHub在线预览:JS 插件在线演示项目介绍如今,随着大前端的发展,曾经的霸主:jQuery 也渐渐的陨落,随之一起没落的,也包含基于 jQuery 的一些优秀的插件。这些优秀的插件,让我们在前端的开发中,节省了大量的时间。自己的项目中也使用了大部分基于 jQuery 的前端插件,帮助我实现了很多的功能,节省了大量的开发时间。所以,我计划发起一个开源项目,整理并收集这些优秀的前端插件的使用方法,并提供示例代码,可供调试和学习。一来为了留个存档,二来也是为了分享出去,帮助到需要的人。该文档起于 jQuery 框架,但不止于 jQuery,希望能整理所有的 Web 前端框架。期待你的加入,一起维护文档,语雀文档协作邀请链接,如果链接过期,请加个人微信号:P_lu0503,并备注:文档协作。为了文档的可用性,我希望每一个插件都有一篇独立文档,以及独立在线演示。文档介绍文档在语雀平台上,期待你的加入进行协作编写,不要求所有的配置项都介绍到,列举一些最常见的即可。文档主要包括以下几个方面的描述:简单介绍:插件的用途,使用场景,以及官方文档链接;开始使用:如何正确的引入库文件到页面中,这一点对于初学者很重要;使用示例(需要代码演示):前台 HTML 如何编写前台 JS 如何调用后台数据返回的格式(不限后台技术,重点关注返回数据的格式)在线演示每一个插件都有一个独立的页面可供访问,在线演示原代码在 GitHub 上,采用 Node 语言编写,可以自行部署在本地,便于调试,尝试插件不同参数的用法。熟悉 Node + Express 框架的可以直接 fork 项目,并提交 pull requests,如果不熟悉 Node 开发,也没关系,我会定期将文档中新增的插件,做成演示示例,push 到 GitHub 仓库。另外,也提供了在线预览。本地部署步骤安装 Node 环境安装 supervisor,安装方法:npm i -g supervisor安装 bower 包管理器,安装方法:npm i -g bower克隆代码到本地,顺便点个 Star进入项目目录,安装依赖,npm install 以及 bower install运行项目,npm start浏览器打开项目:http://localhost:3030/page

April 13, 2019 · 1 min · jiezi

jQuery插件--web端轮播图

效果地址HTML<div id=“slider” class=“slider”></div><script> $(function () { $(’#slider’).sliders({ imgArr: [’./resource/banner1.jpg’, ‘./resource/banner2.jpg’, ‘./resource/banner3.jpg’], autoLoop: true, current: 1, time: 4000, vWidth: 824 }) });</script>cssJS代码;(function ($, window, document, undefined) { ‘use strict’; function Sliders(element, options) { this.element = element; this.options = { vWidth: options.vWidth || element.width(), current: options.current || 1, imgArr: options.imgArr, len: options.imgArr.length, autoLoop: options.autoLoop, time: options.time || 4000 }; this.init(); } Sliders.prototype = { constructor: Sliders, init: function () { this.createHtml(); this.bindEvent(); this.hackLoop(); }, createHtml: function () { var me = this; var content = []; var imgArr = me.options.imgArr, len = me.options.len, current = me.options.current; content.push("<button type=‘button’ class=‘preBtn’ id=‘preBtn’>上</button>"); content.push("<button type=‘button’ class=‘nextBtn’ id=‘nextBtn’>下</button>"); content.push("<ul class=‘sliderUl’>"); for (var i = 0; i < len; i++) { content.push("<li class=‘sliderLi’><img class=‘block’ src=" + imgArr[i] + “>” + i + “</li>”) } content.push("<li class=‘sliderLi’><img class=‘block’ src=" + imgArr[0] + “>” + i + “</li></ul>”); content.push("<ul class=‘pointer’>"); for (var i = 1, len1 = len + 1; i < len1; i++) { if (current !== i) { content.push("<li data-index=" + i + “></li>”); } else { content.push("<li class=‘current’ data-index=" + i + “></li>”); } } content.push("</ul>"); me.element.html(content.join(’’)); }, bindEvent: function () { var me = this; me.element.on(‘mouseenter’, ‘.sliderUl’, function () { clearInterval(me.timer); }); me.element.on(‘click’, ‘.pointer li’, function () { clearInterval(me.timer); var index = parseInt($(this).data(‘index’)); me.goPage(index); }); me.element.on(‘mouseleave’, ‘.sliderUl’, function () { me.hackLoop(); }); me.element.on(‘click’, ‘#preBtn’, function () { clearInterval(me.timer); var i = me.options.current, len = me.options.len; me.options.current = –i === 0 ? len-1 : i-1; me.loop(); }); me.element.on(‘click’, ‘#nextBtn’, function () { clearInterval(me.timer); var i = me.options.current, len = me.options.len; me.options.current = i === len ? 0 : i; me.loop(); }); }, loop: function () { var me = this; var i = me.options.current, vWidth = me.options.vWidth, len = me.options.len; console.log(i); me.element.children(’.sliderUl’).css({ “-webkit-transform”: “translateX(” + (-i * vWidth) + “px)”, “transform”: “translateX(” + (-i * vWidth) + “px)”, “transition-duration”: “0.4s”, “transition-timing-function”: “ease-in”, “transition-property”: “transform” }); me.element.children(’.pointer’).children(“li”).removeClass(“current”); me.element.children(’.pointer’).children(“li”).eq(i === len ? 0 : i).addClass(“current”); if (me.options.current === len) { setTimeout(function () { me.element.children(’.sliderUl’).css({ “-webkit-transform”: “translateX(0px)”, “transform”: “translateX(0px)”, “transition-duration”: “none”, “transition-timing-function”: “none”, “transition-property”: “none” }); //时间必须不小于动画所需时间 }, 400); } me.options.current = (len !== i) ? ++i : 1; }, hackLoop: function () { var me = this; if (me.options.autoLoop) { me.timer = setInterval(function () { me.loop(); }, me.options.time); } }, goPage: function (index) { var me = this; var vWidth = me.options.vWidth; me.options.current = index; me.element.children(’.sliderUl’).css({ “-webkit-transform”: “translateX(” + (-(index - 1) * vWidth) + “px)”, “transform”: “translateX(” + (-(index - 1) * vWidth) + “px)”, “transition-duration”: “0.4s”, “transition-timing-function”: “ease-in”, “transition-property”: “transform” }); me.element.children(’.pointer’).children(“li”).removeClass(“current”); me.element.children(’.pointer’).children(“li”).eq(index - 1).addClass(“current”); } }; $.fn.sliders = function (options) { return new Sliders($(this), options); }})(jQuery, window, document); ...

April 3, 2019 · 3 min · jiezi

jQuery插件--分页

HTML<!DOCTYPE html><html><head> <meta charset=“utf-8”/> <title></title> <style> /* 外面盒子样式—自己定义 / .page_div{margin:20px 10px 20px 0;color:#666} / 页数按钮样式 / .page_div button{display:inline-block;min-width:30px;height:28px;cursor:pointer;color:#666;font-size:13px;line-height:28px;background-color:#f9f9f9;border:1px solid #dce0e0;text-align:center;margin:0 4px;-webkit-appearance: none;-moz-appearance: none;appearance: none;} #firstPage,#lastPage,#nextPage,#prePage{width:50px;color:#0073A9;border:1px solid #0073A9} #nextPage,#prePage{width:70px} .page_div .current{background-color:#0073A9;border-color:#0073A9;color:#FFF} / 页面数量 */ .totalPages{margin:0 10px} .totalPages span,.totalSize span{color:#0073A9;margin:0 5px} /button禁用/ .page_div button:disabled{opacity:.5;cursor:no-drop} </style></head><body ontouchstart="" onmousemove=""><div value=“1 0”></div><div id=“page” class=“page_div”></div></body><script src=“js/jquery.min.js”></script><script type=“text/javascript” src=“js/page1Me.js”></script><script> $("#page").paging({ // pageNo: 18, // totalPage: 20, // totalSize: 300, pageNum: 5, totalNum: 14, totalList: 300, callback: function (num) { console.log(num); } }); // 模拟ajax数据用以下方法,方便用户更好的掌握用法 // 参数为当前页 // ajaxTest(1); // function ajaxTest(num) { // $.ajax({ // url: “table.json”, // type: “get”, // data: {}, // dataType: “json”, // success: function(data) { // console.log(data); // //分页 // $("#page").paging({ // pageNo: num, // totalPage: data.totalPage, // totalSize: data.totalSize, // callback: function(num) { // ajaxTest(num) // } // }) // } // }) // }</script></html>JS;(function ($, window, document, undefined) { ‘use strict’; function Paging(element, options) { this.element = element; this.options = { pageNum: options.pageNum || 1, // 当前页码 totalNum: options.totalNum, // 总页码 totalList: options.totalList, // 数据总记录 callback: options.callback // 回调函数 }; this.init(); } Paging.prototype = { constructor: Paging, init: function () { this.createHtml(); this.bindEvent(); }, createHtml: function () { var me = this; var content = []; var pageNum = me.options.pageNum; var totalNum = me.options.totalNum; var totalList = me.options.totalList; content.push("<button type=‘button’ id=‘firstPage’>首页</button><button type=‘button’ id=‘prePage’>上一页</button>"); // 总页数大于6必显示省略号 if (totalNum > 6) { // 1、当前页码小于5且总页码大于6 省略号显示后面+总页码 if (pageNum < 5) { // 1与6主要看要显示多少个按钮 目前都显示5个 for (var i = 1; i < 6; i++) { if (pageNum !== i) { content.push("<button type=‘button’>" + i + “</button>”); } else { content.push("<button type=‘button’ class=‘current’>" + i + “</button>”); } } content.push(". . ."); content.push("<button type=‘button’>" + totalNum + “</button>”); } else { // 2、当前页码接近后面 中间隔3个 省略号显示后面+总页面 if (pageNum < totalNum - 3) { for (var i = pageNum - 2; i < pageNum + 3; i++) { if (pageNum !== i) { content.push("<button type=‘button’>" + i + “</button>”); } else { content.push("<button type=‘button’ class=‘current’>" + i + “</button>”); } } content.push(". . ."); content.push("<button type=‘button’>" + totalNum + “</button>”); } else { // 3、页码至少在5,最多在【totalNum - 3】的中间位置 第一页+省略号显示前面 content.push("<button type=‘button’>" + 1 + “</button>”); content.push(". . ."); for (var i = totalNum - 4; i < totalNum + 1; i++) { if (pageNum !== i) { content.push("<button type=‘button’>" + i + “</button>”); } else { content.push("<button type=‘button’ class=‘current’>" + i + “</button>”); } } } } } else { // 总页数小于6 for (var i = 1; i < totalNum + 1; i++) { if (pageNum !== i) { content.push("<button type=‘button’>" + i + “</button>”); } else { content.push("<button type=‘button’ class=‘current’>" + i + “</button>”); } } } content.push("<button type=‘button’ id=‘lastPage’>尾页</button><button type=‘button’ id=‘nextPage’>下一页</button>"); content.push("<span class=‘totalNum’> 共 " + totalNum + " 页 </span>"); content.push("<span class=‘totalList’> 共 " + totalList + " 条记录 </span>"); me.element.html(content.join(’’)); // DOM重新生成后每次调用是否禁用button setTimeout(function () { me.dis(); }, 20); }, bindEvent: function () { var me = this; me.element.off(‘click’, ‘button’); // 委托新生成的dom监听事件 me.element.on(‘click’, ‘button’, function () { var id = $(this).attr(‘id’); var num = parseInt($(this).html()); var pageNum = me.options.pageNum; if (id === ‘prePage’) { if (pageNum !== 1) { me.options.pageNum -= 1; } } else if (id === ’nextPage’) { if (pageNum !== me.options.totalNum) { me.options.pageNum += 1; } } else if (id === ‘firstPage’) { if (pageNum !== 1) { me.options.pageNum = 1; } } else if (id === ’lastPage’) { if (pageNum !== me.options.totalNum) { me.options.pageNum = me.options.totalNum; } } else { me.options.pageNum = num; } me.createHtml(); if (me.options.callback) { me.options.callback(me.options.pageNum); } }); }, dis: function () { var me = this; var pageNum = me.options.pageNum; var totalNum = me.options.totalNum; if (pageNum === 1) { me.element.children(’#firstPage, #prePage’).prop(‘disabled’, true); } else if (pageNum === totalNum) { me.element.children(’#lastPage, #nextPage’).prop(‘disabled’, true); } } }; $.fn.paging = function (options) { return new Paging($(this), options); }})(jQuery, window, document);jQuery插件友情链接// 1、代码最前面的分号,可以防止多个文件压缩合并以为其他文件最后一行语句没加分号,而引起合并后的语法错误。// 2、匿名函数(function(){})();:由于Javascript执行表达式是从圆括号里面到外面,所以可以用圆括号强制执行声明的函数。避免函数体内和外部的变量冲突。// 3、$实参:$是jquery的简写,很多方法和类库也使用$,这里$接受jQuery对象,也是为了避免$变量冲突,保证插件可以正常运行。// 4、window, document实参分别接受window, document对象,window, document对象都是全局环境下的,而在函数体内的window, document其实是局部变量,不是全局的window, document对象。这样做有个好处就是可以提高性能,减少作用域链的查询时间,如果你在函数体内需要多次调用window 或 document对象,这样把window 或 document对象当作参数传进去,这样做是非常有必要的。当然如果你的插件用不到这两个对象,那么就不用传递这两个参数了。// 5、undefined形参了,看起来是有点多余。undefined在老一辈的浏览器是不被支持的,直接使用会报错,js框架要考虑到兼容性,因此增加一个形参undefined ...

April 3, 2019 · 3 min · jiezi

JQuery插件之$.fn

jQuery对象方法$(“a”).css(“color”, “red”);当使用$选择元素的时候,它会返回一个jQuery对象,这个对象中包含了你一直使用的方法(比如:.css(),.click()等)。这个jQuery对象是从$.fn对象中得到的这些方法。$.fn对象包含了jQuery对象的所有方法,如果想编写我们自己的方法,也要将方法定义在$.fn对象里面。使用这种方式编写插件,即jQuery类的实例将会拥有此功能。为什么会有这种效果?简单看下源码jQuery.fn = jQuery.prototype ={ init: function( selector, context ){ //do something…};从源码可知,当使用$.fn的方式编写插件时,就是在jQuery的原型中绑定新的方法,斯所以根据原型的特性,新创建的jQuery对象自然会拥有新绑定的方法。简单插件目的:改变a标签的字体颜色做法:创建名为greenify的方法,将其加在$.fn里面。如此一来,greenify方法适用于所有的jQuery对象了<html><head> <title>Title</title> <script type=“text/javascript” src="../../js/jQuery-1.8.0.min.js"></script> <script type=“text/javascript”> //这里使用$与jQuery都可以 $.fn.greenify = function () { /这里使用this和$(this)都可以/ this.css(“color”, “red”); }; $(function () { //$(“a”)即jQuery实例化对象 $(“a”).greenify(); }); </script></head><body> <a href="#">Click Me!!!</a> <a href="#">Cover Me!!!</a></body></html>$.fn还有另外一种写法$.fn.extend({})jQuery.fn.extend({ greenify: function () { $(this).css(“color”, “red”); }});$(function () { $(“a”).greenify();});链式写法jQuery.fn.extend({ greenify: function () { $(this).css(“color”, “red”); //在结尾处返回this return this; }});$(function () { $(“a”).greenify().addClass(“greenified”);});链式编程的好处就是当调用完一个方法后,可以在后面继续调用该对象的其他方法。$的别名保护$在JavaScript库中使用是非常广泛的,如果你正在使用的其他JavaScript类库中也使用到了$,并且你也使用了jQuery,那么你不得不使用jQuery.noConflict()来解除冲突。显然这会破坏我们的插件,因为它是在假设$是jQuery函数的别名(实际上$本来就是jQuery的别名)如果既想使用其他插件,也要使用jQuery的$别名,我们需要将代码放进立即执行函数(文章结尾会对其简单解释)中,然后将jQuery作为实参传递进去,$作为形参接收。(function ( $ ) { $.fn.greenify = function() { this.css( “color”, “green” ); return this; };}( jQuery ));由于window,document都是全局作用域的,而将其传入函数内部后,其在内部是作为局部变量存在的,这样做可以提高性能,减少作用域的查找时间,如果在函数内部多次调用window、document,这是很高效的做法。(function ( _window, _document ) { $.fn.greenify = function() { this.css( “color”, “green” ); return this; }; }( window, document));另外,存储私有变量也是使用立即执行函数的主要目的,假如想要存储一个默认的颜色,就可以使用一个私有变量进行存储。 (function ($) { var defaultColor = “pink”; $.fn.extend({ setColor:function () { $(this).css(“color”, defaultColor); return $(this); } }) })(jQuery); $(function () { //所有div的text都会变成粉色 $(“div”).setColor(); });;分号的使用通常在编写插件时,会在在立即执行函数前加入一个分号;(function($){ //do something…})(jQuery);作用:防止多个文件压缩合并后,前一个文件最后一行语句没加分号,而引起合并后的语法错误。如果前面已经加了分号,此时就会多出来一个分号,但这样是不会报错的。即不怕多,就怕少。undefined解决兼容性问题;(function($, undefined){ //do something…})(jQuery)上面代码的undefined参数略显多余,此参数是为了解决undefined在老一辈的浏览器(IE5)可以被赋值的问题,全局的undefined有可能会被其他函数覆盖。 var undefined = 99; alert(undefined);以上代码如果在IE5运行,可以弹出99, 而不是undefined,如果是这样的话,全局的undefined就有被其他函数覆盖的危险;但以上代码在chrome运行,会弹出undefined。既然是插件就要考虑兼容性,所以通过在匿名函数中多定义一个undefined的形参,由于只传入了一个实参jQuery,从而可以保证undefined形参未被赋值,从而最终是我们想要的undefined的值,什么是我们想要的undefined?即,undefined没有被赋值,undefined就是undefined。最大限度的减少插件的空间当使用$.fn创建插件时,尽量减少空间的占用,能使用参数进行区分的,就不要多定义一个方法,这样可以降低插件被覆盖的可能性。反例(function( $ ) { $.fn.TurnOnLight= function() { // Turn on }; $.fn.TurnOffLight = function() { // Turn off };}( jQuery ));正例(function( $ ) { $.fn.light = function( action ) { if ( action === “turnOn”) { // Turn on light code. } if ( action === “turnOff” ) { // Turn off light code. } };}( jQuery ));使用参数选项当插件越来越复杂时,通过接收参数选项的方式来自定义插件是一种很好的做法。<html><head> <title>Title</title> <script type=“text/javascript” src="../../js/jquery-1.8.0.min.js"></script><script type=“text/javascript”> (function ($) { $.fn.extend({ changeColor:function (options) { var defaultColor = $.extend({ color: “red”, backgroundColor:“skyblue” }, options); return $(this).css({ color:defaultColor.color, backgroundColor:defaultColor.backgroundColor }); } }); })(jQuery); $(function () { $(“div”).changeColor(); /$(“div”).changeColor({ color:“pink”, backgroundColor:“yellow” });/ });</script></head><body><div>JavaScript</div><div>Jquery</div></body></html>调用changeColor不传递任何参数,文本颜色默认红色,背景默认天蓝色;反之按照传入参数渲染颜色。默认的颜色被$.extned()覆盖为其他颜色。$.extend({ color: “red”, backgroundColor:“skyblue”}, options)以上代码执行结果:options如果有值,那么它将覆盖第一个参数并返回options;如果为undefined则直接返回第一个参数。简单解释立即执行函数(IIFE)立即执行函数((Immediately-Invoked Function Expression)也称为自调用函数,函数定义好后会自动执行。(function(){})表示一个匿名函数,后面紧跟一个(),表示立即执行函数。(function(){ console.log(“我会立即执行”);})()(function(param){ console.log(param); //我会立刻执行})(“我会立刻执行”);优点:不会产生产生全局变量,不会造成变量污染。缺点:只能执行一次,没法重复执行。在编写插件时,使用IIFE是最合适不过的了,插件只需要引用一次,即执行一次。 ...

March 9, 2019 · 2 min · jiezi

VUE中使用 zTree -- jQuery 树插件 及 使用时报错 jQuery is not defined 的问题解决

使用需求时隔两年多,再次使用到了 ztree ,这次是在vue中引入使用,并遇到了一些小问题,在这里把经验和解决办法给大家分享下,共勉。ztree: zTree – jQuery 树插件 官网简介 zTree v3.5 Demo 演示有关ztree的使用,之前整理过两篇在jquery中使用的方法,大家参考下:zTree – jQuery 树插件 使用方法与例子zTree – jQuery 树插件 构造treeNode JSON 数据对象zTree v3.5 Demo 演示 OutLook 样式的左侧菜单在vue中使用zTree首先,需要安装插件:npm install ztree然后,使用ztree具体相关的代码如下:<ul id=“ztree” class=“ztree”></ul>提示:zTree 的容器 className 别忘了设置为 “ztree”!!!import ‘jquery’import ‘ztree’import ‘ztree/css/metroStyle/metroStyle.css’data () { return { treeNodes: ‘’, selectNode: ‘’, selectTreeSetting: { view: { showIcon: true, showLine: true, dblClickExpand: false }, async: { enable: true, type: ‘get’, dataType: ‘json’, url: ‘/xxxx/list.json’, autoParam: [‘id=parentId’], dataFilter: this.filter }, data: { simpleData: { enable: true, idKey: ‘id’, pIdKey: ‘parentId’ } }, callback: { onClick: this.zTreeOnClick } } } }, mounted () { this.initStationSelectTree($(’#station’)) }, methods: { initStationSelectTree (anchor) { this.$get(’/xxxxx/list.json’, {}, response => { //此处是使用的封装过的 this.treeNodes = $.fn.zTree.init(anchor, this.selectTreeSetting, response.data) }) }, filter (treeId, parentNode, childNodes) { if (!childNodes) return null for (var i = 0, l = childNodes.length; i < l; i++) { if (childNodes[i].parentId === ‘super’) childNodes[i].open = true } curChildNodes = childNodes return childNodes }, zTreeOnClick: function (event, treeId, treeNode) { // console.log(treeNode);点击树节点获取当前节点 treeNode.name 等,编写需要的逻辑即可 } }效果如图:报错jQuery is not defined的解决注意,在ztree中会使用到jQuery ,可能会报错jQuery is not defined,一开始参考vue ztree 结合使用中的办法:项目build的时候自动加载jquery,并且输出到jQuery中 new webpack.ProvidePlugin({ jQuery:‘jquery’, $:‘jquery’, })在 npm run dev 之前,先build一下,解决了报错 jQuery is not defined 的问题。但是,重点来了,本地没问题,打包发布到正式环境依然报错jQuery is not defined,这就奇怪了,于是搜了很多解决报错jQuery is not defined的文章,各种方法试了很多,依然是报错,最后,其中一篇Webpack引入jquery及其插件的几种方法中的第四种方案,终于能成功解决了,本地运行和发布到正式环境都没再报错了。这里贴一下解决方案:此方法只依赖于自己,完全不需要任何其他插件与加载器,创建jquery的包装对象jquery-vendor.jsimport $ from ‘jquery’window.$ = $window.jQuery = $export default $以后引用jquery时指向jquery-vendor.jsimport $ from ‘../assets/jquery-vendor.js’import ‘jquery-ui’// 此时UI的方法全部可用,如果需要引用bootstrap,可参照此方法为了调用方便,可在webpack配置文件中创建jquery-vendor.js的别名alias: { jquery : ‘src/assets/jquery-vendor.js’ // 将其指向jquery-vendor.js所在位置} ——————— 作者:蚁方阵 来源:CSDN 原文:https://blog.csdn.net/yiifaa/article/details/51916560 版权声明:本文为博主原创文章,转载请附上博文链接!相关参考vue ztree 结合使用Webpack引入jquery及其插件的几种方法“jQuery/$ is not defined” 解决方法 ...

December 26, 2018 · 2 min · jiezi

数字滚动插件numberAnimate.js的使用及效果修改

有个实现数字滚动的需求,想着肯定有很多这种效果的插件,就不自己造轮子了,于是,找了个numberAnimate js数字滚动插件,还挺好用,很简单,刚好符合需求。代码如下:<!DOCTYPE html><html lang=“en”> <head> <meta charset=“utf-8”> <title>数字滚动插件</title> <link rel=“stylesheet” type=“text/css” href="../dist/style/numberAnimate.css" /> </head> <body> 无分隔符,无小数点:<div class=“numberRun”></div><br><br> </body> <script src=“http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script> <script type=“text/javascript” src=”../dist/script/numberAnimate.js"></script> <script type=“text/javascript”> $(function(){ //初始化 var numRun = $(".numberRun").numberAnimate({num:‘1553093’, speed:1000}); //想要的是过一段时间加1的效果,可以实现 var nums = 1553093; setInterval(function(){ nums+= 1; numRun.resetData(nums); },2000); })</script></html>想要的效果是过一段时间加1的效果,可以实现,但是有个小小的bug,那就是,比如说,个位数字加到9时进一位然后该位上为0,问题来了,这里0的数字却滚动不出来,直接到了下一个时间间隔时滚动到了11,调试了很久发现,是numberAnimate.js中,第85行的判断条件有问题,注释掉该if判断就好了,如下图:上图中,第85行的$(this).css(“top”)一直都是0px,而第84行计算获取到的thisTop 在数字滚到到0 时值为0px,所以导致直接跳过了判断,没有执行到transform动画那里,所以少了0的效果,导致看起来的效果就是9直接跳到了11,没有10。另外,提示一句,改变数字的字号大小,需要修改的numberAnimate.css中的height、width、字号的比例要把握好,一不小心就坏了,呵呵,亲身体会 -_-||

September 7, 2018 · 1 min · jiezi