微信小程序跑马灯效果--基于CSS3 animation

视图模板wxml中:

<view class="marquee_container" style="--marqueeWidth--:-12em">
    <view class="marquee_text">测试跑马灯效果</view>
</view>

样式wxss中:

@keyframes around {
  from {
   margin-left: 100%;
  }
  to {
   /* var接受传入的变量 */
   margin-left: var(--marqueeWidth--);
  }
 }
 
.marquee_container{
  background-color: #000;
  height: 40rpx;
  line-height: 40rpx;
  position: relative;
  width: 100%;
}
.marquee_container:hover{
  /* 不起作用 */
  animation-play-state: paused;
}
.marquee_text{
  color:#b4b4b4;
  font-size: 28rpx;
  display: inline-block;
  white-space: nowrap;
  animation-name: around;
  animation-duration: 10s;  /*过渡时间*/
  animation-iteration-count: infinite;
  animation-timing-function:linear;
}

如果上面看懂了下面再深入一下,因为上面的只是应付一些简单的滚动(内容少的情况下),下面这是应对比较多的内容时的解决方案代码如下(结合js)

事件js

//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    content_t:'测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测sss',//内容
    size:12,//宽度即文字大小
    marqueeW:0,
    moveTimes:8,//一屏内容滚动时间为8s
    allT:"0s"
  },
  onLoad: function () {
    var screenW=wx.getSystemInfoSync().windowWidth;//获取屏幕宽度
    var contentW=this.data.content_t.length*this.data.size;//获取文本宽度(大概宽度)
    var allT=(contentW/screenW)*this.data.moveTimes;//文字很长时计算有几屏
    allT=allT<8?8:allT;//不够一平-----最小滚动一平时间
   this.setData({marqueeW:-contentW+"px",allT:allT+"s"});
  }
})

视图模板wxml

<view class="marquee_container" style="--marqueeWidth--:{{marqueeW}};--allTs--:{{allT}};">
    <view class="marquee_text" style="font-size:{{size}}px">{{content_t}}</view>
</view>

样式wxss

@keyframes around {
  from {
   margin-left: 100%;
  }
  to {
   /* var接受传入的变量 */
   margin-left: var(--marqueeWidth--);
  }
 }
 
.marquee_container{
  background-color: #000;
  height: 40rpx;
  line-height: 40rpx;
  position: relative;
  width: 100%;
}

.marquee_text{
  color:#b4b4b4;
  font-size: 28rpx;
  display: inline-block;
  white-space: nowrap;
  animation: around var(--allTs--)  infinite linear;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容