好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

微信小程序实现走马灯效果实例

前言

日常开发中,我们经常会遇到文字横向循环滚动的效果,俗称走马灯,也是项目中经常会使用的一个功能。在网页web前端很常见,今天就介绍下小程序的实现方式,一种是用的css样式实现,另一种是运用小程序的动画功能实现。

@keyframes 实现

利用@keyframes的规则来实现,非常方便,只需要css样式就可以满足,使用方法跟web上一样。

?

< view class = "marquee" >

  < text >这是一段滚动的文字</ text >

</ view >

样式类,from to 来定义动画的开始结束,这里是从屏幕最右端向左滑,触及到最左侧后重新开始新一轮动画。

?

@keyframes translation {

  from {

  margin-left : 750 rpx; //从屏幕最右端开始

  }

 

  to {

  margin-left : 0px ;

  }

}

 

.marquee{

  white-space : nowrap ;

  animation-name: translation; //定义动画的名称

  animation-duration: 3 s;

  animation-iteration-count: infinite;

  animation-timing-function: linear;

}

如果想要的效果是滑动到左侧之后,文字继续左滑,直到全部消失,再从最右开始动画的,那么就需要再加上文字的长度,这里需要去计算文字的长度,使用SelectorQuery 对象实例来拿到文字节点的宽度。在页面首次渲染完毕onReady时执行,查询到对应文字节点信息的对象,得到文字的长度。这里定义的是上面的marquee类名。

?

onReady: function () {

  let query = wx.createSelectorQuery();

  query.select( '.marquee' ).boundingClientRect();

  query.exec((res) => {

   if (res[0]) {

   this .setData({

    marqueeWidth: res[0].width

   })

   }

  })

}

然后使用css var() 函数插入定义的属性值,到结束的位置处,让它走完整个屏幕以及自身文字的长度。定义一个名为 "--marqueeWidth" 的属性,然后在wxss样式文件里使用var()函数调用该属性:

?

<view class= "marquee" style= "--marqueeWidth:-{{marqueeWidth}}px" >

  <text>这是一段滚动的文字</text>

</view>

 

wxss样式里:

@keyframes translation {

  from {

  margin-left: 750rpx;

  }

 

  to {

  margin-left: var (--marqueeWidth);

  }

}

这是通过css的方式实现的,非常方便,但是会出现在部分机型上适配的问题,还有一种是通过Animation动画实现。

animation实现

通过animation动画实例来完成动画,一开始让视图位于屏幕右侧超出屏幕的。

?

<view class= "marquee2" bindtransitionend= "animationend" animation= "{{animationData}}" >

     <text>这是一段滚动的文字</text>

</view>

 

.marquee 2 {

  display : inline- block ;

  white-space : nowrap ;

  margin-left : 750 rpx;

}

同样这里计算了文字的长度,通过Animation.translate(number tx, number ty)平移属性进行移动操作,直至移出整个屏幕。在一组动画完成之后,调用bindtransitionend回调,再一次去执行动画,

?

this .animation = wx.createAnimation({

  duration: 3000,

  timingFunction: 'linear'

});

var query = wx.createSelectorQuery();

query.select( '.marquee2' ).boundingClientRect();

query.exec((res) => {

  if (res[0]) {

  this .setData({

   marqueeWidth: res[0].width //文字长度

  }, () => {

   this .doAnim()

  })

  }

})

 

 

doAnim: function () {

  //向左滚动到超出屏幕,这里临时写死的屏幕宽度375px

  this .animation.translate(- this .data.marqueeWidth - 375, 0).step();

  setTimeout(() => {

   this .setData({

   animationData: this .animation.export(),

   });

  }, 10)

}

在第一次动画结束之后,重新开始,通过animationend监听动画结束,然后再次执行。

?

animationend() {

  //复位

  this .animation.translate(0, 0).step({ duration: 0 });

  this .setData({

   animationData: this .animation.export()

  }, () => {

   //重新开始动画

   this .doAnim()

  });

}

这个动画在小程序开发工具上运行会有动画突然暂停的现象,可以用手机试下的。

相对而言是比较容易实现的,而且走马灯的效果也是我们在项目中经常会用到的,正好也可以熟悉下小程序的动画的。

总结

到此这篇关于微信小程序实现走马灯效果实例的文章就介绍到这了,更多相关小程序走马灯效果内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6916884147348701192

dy("nrwz");

查看更多关于微信小程序实现走马灯效果实例的详细内容...

  阅读:68次