实现NoticeBar 通知栏。走马灯公告栏

前言

微信小程序封装公共组件——实现NoticeBar 通知栏。走马灯公告栏


一、使用步骤

1.引入库

代码如下(示例):

"usingComponents": {
  "van-notice-bar": "@vant/weapp/notice-bar/index"
}

2.页面代码

index.wxml代码如下(示例):

<view class="notice-bar">

  <view class="notice-title">
    {{title}}
  </view>
  <view>
    <van-notice-bar custom-class="bar" background="#fff" color="#3D3D3D" scrollable text="{{text}}" />
  </view>
</view>

index.ts

// components/navigation/index.ts
let timers = 0
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    notice: {
      type: Array,
      value: [],
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '',
    text: '',
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleInterval(list) {
      let index = 0
      const len = list.length

      this.setData({
        ...list[index],
      })
      if (len < 2) return
      timers ==
        setInterval(() => {
          index = (index + 1) % len
          this.setData({
            ...list[index],
          })
        }, 15000)
    },
  },
  observers: {
    notice(value) {
      this.handleInterval(value)
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      timers && clearInterval(timers)
    },
  },
})

总结

公告通知栏父组件传notice数组,组件设置了timer可以多条进行轮播

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值