当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效

基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效

2019年07月21日  | 移动技术网IT编程  | 我要评论

通钢吧,陈际瓦,淘宝学

实现目标

浏览各大云平台,发现一个页面特效使用较为频繁,以“百度云”为例(),进入百度云后,当滚动条滚动至“更可靠的数据支持”模块时,页面数据将会开始滚动式增长特效。下面将会介绍我的解决方案,希望有同行更好的解决方案大家一起交流。

解决思路

主要的解决要点如下:

如何实现数字动画的效果
如何监听滚动条到指定的位置
分解要点寻找解决思路:

一、如何实现数字动画的效果

在vue的官方文档(... 中,实现了数字动画特效。于是参照此示例基于tween来完成。

二、如何监听滚动条到指定的位置

使用 window.addeventlistener('scroll',this.handlescroll)监听窗口的滚动,进行位置判断。

实现代码

1.下载tween.js

cnpm install tween.js --save-dev

2.引入tween.js

import tween from 'tween.js'
// ***.vue,注意这里千万别在main.js中引入,否则运行会报:tween is undefined,
// 这边存疑,不知道为什么在main.js中不执行

3.实现数字动画效果和监听滚动条

<div class="sectionright">
 <span class="numberinit" style="display:none">{{num1}}</span>
 <p class="numbergrow numbergrow1">{{formatnum1}}</p>
 <p class="sectiontxt">抵御攻击</p>
</div>
export default {
 computed:{
 formatnum1(){
  let num = this.animatednum1
  return (num || 0).tostring().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
 }
 },
 data () {
 return {
  num1: 0,
  animatednum1: 0
 }
 },
 watch: {
 num1: function(newvalue, oldvalue) {
  var vm = this
  function animate (time) {
  requestanimationframe(animate)
  tween.update(time)
  }
  new tween.tween({ tweeningnumber: oldvalue })
  .easing(tween.easing.quadratic.out)
  .to({ tweeningnumber: newvalue }, 5000)
  .onupdate(function () {
   vm.animatednum1 = this.tweeningnumber.tofixed(0) 
   //tofixed(num):num代表小数点后几位
  })
  .start()
  animate()
 }
 },
 methods:{
 setanimatednum(){
  this.num1 = 3567892881
 },
 handlescroll(){  
  let windowh = document.body.clientheight
  let docsrolltop = $(document).scrolltop() //文档卷动值
  let clienth = $(window).height() //视窗大小
  let sectiontop = $(".sectionbody").offset().top //动态文字模块距离文档头部的距离
  let sectionh = $(".sectionbody").height()
  if((docsrolltop + clienth - sectiontop) >= 0 
  && (docsrolltop - sectiontop - sectionh) <= 0){
  this.setanimatednum()
  }
 }
 },
 mounted(){
 this.handlescroll()
 window.addeventlistener('scroll',this.handlescroll)
 }
}

github源码:https://github.com/mirror1988...
演示地址 :...

总结

以上所述是小编给大家介绍的基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网