jquery.countup.min.js 中
1 2 3 | var f = function () { e.text(e.data( "counterup-nums" ).shift()), e.data( "counterup-nums" ).length ? setTimeout(e.data( "counterup-func" ), a) : (e.data( "counterup-nums" ), e.data( "counterup-nums" , null ), e.data( "counterup-func" , null )) } |
改为
1 2 3 4 5 | var f = function () { if (e.data( 'counterup-nums' )) { e.text(e.data( "counterup-nums" ).shift()), e.data( "counterup-nums" ).length ? setTimeout(e.data( "counterup-func" ), a) : (e.data( "counterup-nums" ), e.data( "counterup-nums" , null ), e.data( "counterup-func" , null )) } } |
1 2 3 4 5 6 7 8 9 10 11 12 | var f = function () { if ($ this .data( 'counterup-nums' )) { //加个这个判断 $ this .text($ this .data( 'counterup-nums' ).shift()); if ($ this .data( 'counterup-nums' ).length) { setTimeout($ this .data( 'counterup-func' ), delay); } else { $ this .data( 'counterup-nums' ); $ this .data( 'counterup-nums' , null ); $ this .data( 'counterup-func' , null ); } } }; |
waypoint,也就是第一个引入的文件是用于滚动监听的
倒数几行的位置有一行代码
1 | $ this .waypoint(counterUpper, { offset: '100%' , triggerOnce: true }); |
这行代码的就是用了第一个引入的文件,滚动监听,你滚动到绑定countup数字滚动的标签位置,就会开始滚动数字,
你如果希望只滚动一次数字,那你就把这句注释掉,自己写滚动监听
1 2 3 4 5 6 7 8 9 | // Perform counts when the element gets into view // $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true }); var scrollBool = true ; $(window).on( 'scroll' , function () { if ($(window).height() + $(document).scrollTop() >= $ this .offset().top && scrollBool) { scrollBool = false counterUpper(); } }); |
关于 数据每此都是第一次的数为问题 ;处理下这段代码
1 2 3 | // if(!$this.data('counterupTo')) { $ this .data( 'counterupTo' ,$ this .text()); // } |
注释掉 if(!$this.data('counterupTo'))
1 2 3 4 5 6 7 8 9 10 11 12 | var f = function () { if ($ this .data( 'counterup-nums' )) { $ this .text($ this .data( 'counterup-nums' ).shift()); if ($ this .data( 'counterup-nums' ).length) { setTimeout($ this .data( 'counterup-func' ), delay); } else { $ this .data( 'counterup-nums' ); $ this .data( 'counterup-nums' , null ); $ this .data( 'counterup-func' , null ); } } }; |
这个是加在哪里呢,现在报错呢
Cannot read property 'shift' of null
at f
1 2 3 4 5 6 7 | var f = function () { if (e.data( "counterup-nums" )) { //报错另外添加的判断 e.text(e.data( "counterup-nums" ).shift()), e.data( "counterup-nums" ).length ? setTimeout(e.data( "counterup-func" ), a) : (e.data( "counterup-nums" ), e.data( "counterup-nums" , null ), e.data( "counterup-func" , null )) } }; |