vue实现数字“滚动式增加”效果 【页面加载后,获取API返回的data后,即执行渲染动画(如下图gif动画)】
- 源码来自于转载
- demo演示地址 https://www.17sucai.com/pins/demo-show?id=29746,
(即:素材网址 https://www.17sucai.com/preview/1268063/2018-06-28/ai-dome1/index.html )
免责声明:
本人改造了功能部分,仅供学习参考。
- 为了更好的对接仓木,特别针对后台API返回的data部分对接前端逻辑,特对部分js代码进行了改造。
- 除
css
样式部分,其余全部相关代码如下。【下载地址 (含css、API接口)】
一、效果图:
二、<templete>
html
部分代码,如下:
<!-- 数据效果 动画 -->
<div class="product-num-box mgb20">
<div class="product-num-content">
<div class="product-num-title">全线产品免费开放,等你加入</div>
<div class="product-num-text">我们致力于构建最全面、最开放、最前沿的AI开放平台</div>
<div class="product-num-text">提供最易用的API、SDK等开发组件,助力您快速高效地实现产品升级</div>
<div class="product-num-head">
<div class="product-num-item">
<span class="timer product-num-nub count-title" data-to="102" data-speed="102">
<span id="count-number1">0</span><span class="product-num-add">项</span>
</span>
<span class="product-num-inf">技术能力</span>
</div>
<div class="product-num-item">
<span class="timer product-num-nub count-title" data-to="24" data-speed="24">
<span id="count-number2">0</span><span class="product-num-add">小时</span>
</span>
<span class="product-num-inf">快速集成</span>
</div>
<div class="product-num-item">
<span class="timer product-num-nub count-title" data-to="54" data-speed="54">
<span id="count-number3">0</span><span class="product-num-wan">W<span class="product-num-add">+</span></span>
</span>
<span class="product-num-inf">开发者正在使用</span>
</div>
</div>
<a href="#" class="product-num-jus">立即体验</a>
</div>
</div>
三、<script>
的部分代码
,如下:
mounted () {
// 数字效果 动画
this.handleScroll();
},
method:{
/* 数字效果 动画
* 数字自增到某一值动画参数(目标元素,自定义配置)
*/
NumAutoPlusAnimation: function (targetEle, options) {
/*可以自己改造下传入的参数,按照自己的需求和喜好封装该函数*/
//不传配置就把它绑定在相应html元素的data-xxxx属性上吧
options = options || {};
var $this = document.getElementById(targetEle),
time = options.time || $this.data('time'), //总时间--毫秒为单位
finalNum = options.num || $this.data('value'), //要显示的真实数值
regulator = options.regulator || 100, //调速器,改变regulator的数值可以调节数字改变的速度
step = finalNum / (time / regulator),/*每30ms增加的数值--*/
count = 0, //计数器
initial = 0;
var timer = setInterval(function() {
count = count + step;
if(count >= finalNum) {
clearInterval(timer);
count = finalNum;
}
//t未发生改变的话就直接返回
//避免调用text函数,提高DOM性能
var t = Math.floor(count);
if(t == initial) return;
initial = t;
$this.innerHTML = initial;
}, 50);
},
// 滚动监听 · 动画播放
handleScroll: function(){
let scrollHeight = $(".pos-rightbar").scrollTop(); // 滚动条的滚动行程
//滚动条滚动到对应的板块显示
console.log("重要参考:"+scrollHeight);
console.log("numAnimate前"+this.numAnimate);
this.numAnimate = this.numAnimate + 1;
console.log("numAnimate后"+this.numAnimate);
let count1 = 100, count2 = 50, count3 = 120;
//动画播放设置
if( this.numAnimate ==1){
var localPath = this.GLOBAL.localSrc; // 本地 接口地址
var serverPath = this.GLOBAL.serverSrc; // 线上 接口地址
axios.get( serverPath + '/jobpost/countdata',
{
// params:{
// },
headers: {
'Content-Type':'application/json',
// 'Authorization': key
}
}
)
.then(function (response) {
console.log(response);
count1 = response.data.data.enterpriseTotal;
count2 = response.data.data.hrTotal;
count3 = response.data.data.recommendTotal;
// alert(count2);
// 成功之后调用
this.NumAutoPlusAnimation("count-number1", {
time: 1500,
num: count1,
regulator: 50
})
this.NumAutoPlusAnimation("count-number2", {
time: 1500,
num: count2,
regulator: 50
})
this.NumAutoPlusAnimation("count-number3", {
time: 1500,
num: count3,
regulator: 50
})
}.bind(this))
.catch(function (error) {
console.log("请求统计数据失败"+error);
});
}
},
}
上述代码,可独立运行效果。
Tips:【后期文章更新,会附链demo源码地址(含css)】
- 博主自定义 留言区
- 具体代码实现效果,参考某某项目,对应目录为
/company/main.vue
文件
相关附件 · 下载地址
- 参考文件下载:点击下载 · (含css、API接口)
以上就是关于“ vue实现数字‘滚动式增加’效果 【插件化封装】 ” 的全部内容。