vue实现数字“滚动式增加”效果 【插件化封装】

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文件
相关附件 · 下载地址

以上就是关于“ vue实现数字‘滚动式增加’效果 【插件化封装】 ” 的全部内容。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的Vue数字滚动抽奖的实现示例: 1. 在template中定义一个数字滚动的容器: ```html <template> <div class="number-roller"> <div class="number" v-bind:style="numberStyle">{{ number }}</div> </div> </template> ``` 2. 在data中定义数字滚动的数值和样式: ```javascript export default { data() { return { number: 0, numberStyle: { transform: 'translateY(0px)', transition: 'transform 0.3s ease-out' } } } } ``` 3. 在computed中计算数字滚动的样式和值: ```javascript computed: { numberStyle() { return { transform: `translateY(${-this.number * 30}px)`, transition: 'transform 0.3s ease-out' } } } ``` 这里假设数字是在0到9之间滚动,每个数字之间的间距为30像素。当数字改变时,计算属性会重新计算数字滚动的样式。 4. 在methods中添加一个"startRolling"方法,以触发数字滚动动画: ```javascript methods: { startRolling() { let count = 0; let interval = setInterval(() => { this.number = Math.floor(Math.random() * 10); // 随机生成一个数字 count++; if (count > 10) { // 滚动10次后停止滚动 clearInterval(interval); this.$emit('rolling-complete', this.number); // 触发抽奖完成事件,传递中奖号码 } }, 300); } } ``` 这里使用setInterval方法每隔0.3秒随机生成一个数字滚动10次后停止滚动,并通过$emit方法触发抽奖完成事件,将中奖号码传递给父组件。 5. 在父组件中使用"NumberRoller"组件,并监听抽奖完成事件: ```html <template> <div> <button @click="startLottery">开始抽奖</button> <number-roller v-if="rolling" @rolling-complete="handleRollingComplete"></number-roller> </div> </template> ``` 6. 在父组件中定义"startLottery"方法,以开始抽奖: ```javascript methods: { startLottery() { this.rolling = true; }, handleRollingComplete(number) { this.rolling = false; alert(`恭喜您中奖了,中奖号码为${number}`); } } ``` 这里使用"rolling"变量控制数字滚动组件的显示和隐藏,当抽奖完成时,显示中奖号码的提示框。 这样就完成了一个简单的Vue数字滚动抽奖的实现。你可以根据实际需求进行更加丰富的自定义和优

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值