栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

原生小程序封装跑马灯效果

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

原生小程序封装跑马灯效果

本文实例为大家分享了小程序封装跑马灯效果的具体代码,供大家参考,具体内容如下

Marquee.wxml


 
 
 
 {{item.img}}
 
 
 

Marquee.wxss

@keyframes around {
 from {
 margin-left: 100%;
 }

 to {
 margin-left: var(--marqueeWidth--);
 }
}

.marquee_container {
 
 padding: 12rpx 0;
 position: relative;
 width: 100%;
 

}

.marquee_text {
 display: flex;
 white-space: nowrap;
 animation-name: around;
 animation-duration: var(--speed--);
 animation-iteration-count: infinite;
 animation-timing-function: linear;
 line-height: 50rpx;
}

.marquee_tit {
 height: 50rpx;
 line-height: 50rpx;
 position: absolute;
 padding-left: 22rpx;
}

Marquee.js

Component({
 options: {
 multipleSlots: true // 在组件定义时的选项中启用多slot支持
 },
 properties: {
 title: {   // 属性名
  type: String,  // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
  value: '标题' , // 属性初始值(可选),如果未指定则会根据类型选择一个
  observer: function (newVal) {
  this.setData({
   'data[0].img': newVal
  })
  this.run()
  }
 }
 },
 data: {
 data: [
  {
  img: "",
  }
 ],
 broadcast_arr: {
  speed: 5, //滚动速度,每秒5个字
  font_size: "16", //字体大小(px)
  text_color: "#de8c17", //字体颜色
  back_color: "#fffbe8", //背景色
 }
 },

 
 methods: {
 run() {
  let ititdata = this.data.data,
  assist = this.data.broadcast_arr,
  this_width = 0,
  spacing = 0,
  speed = (this.data.broadcast_arr.speed * this.data.broadcast_arr.font_size); //m每秒行走的距离

  for (let i in ititdata) {
  ititdata[i].starspos = wx.getSystemInfoSync().windowWidth; //以取屏幕宽度为间距
  this_width += (ititdata[i].img.length-12) * this.data.broadcast_arr.font_size;
  if (i != ititdata.length - 1) {
   spacing += ititdata[i].starspos
  }
  }
  let total_length = this_width + spacing;//总长
  assist.time = total_length / speed; 
  assist.width_mal = total_length;
  this.setData({
  broadcast_arr: assist,
  data: ititdata
  })
 }
 }
})

index引入—index.json

{
 "usingComponents": {
 "marquee":"/components/Marquee/Marquee"
 },

index.wxml


 

index.js

onReady:function(){
 this.marquee=this.selectComponent('#marquee')
 this.marquee.run()
 },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/68245.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号