微信小程序公告滚动

子组件JS文件

export default Component({
  properties: {
    // 是否显示组件
    isShow: {
      type: Boolean,
      value: true
    },
    // 是否滚动显示
    scroll: {
      type: Boolean,
      value: false
    },
    // 公告文案
    notice: {
      type: String,
      value: ''
    },
    // 背景颜色
    bgColor: {
      type: String,
      value: 'none'
    },
    // 文字颜色
    color: {
      type: String,
      value: '#fff'
    },
    // 是否显示公告Icon
    showIcon: {
      type: Boolean,
      value: false
    },
    // 是否显示关闭按钮
    close: {
      type: Boolean,
      value: false
    },
   	// 公告文字大小
    fontSize: {
      type: String,
      value: '28rpx'
    },
    // 公告滚动速度
    speed: {
      type: Number,
      value: 8
    },
  },
  methods: {
    onDismissNotice(event) {
      this.setData({
        isShow: false
      });
      let detail = event.detail;
      let option = {};
      this.triggerEvent('close', detail, option);
    }
  }
});

子组件wxml文件

<view wx:if="{{isShow && notice}}" class="notice" style="background-color: {{bgColor}};">
	<!-- 公告图标`在这里插入代码片`(可以替换为自己的UI) -->
    <image wx:if="{{showIcon}}" class="notice-tips-img" src="/images/meeting/notice_img.png"></image>
    <view class="notice__text--wrap" style="color: {{color}};">
        <view wx:if="{{scroll}}" class="notice__text notice__text--animate" style="font-size: {{fontSize}};animation: notice {{speed}}s linear infinite;">
            <text class="notice__text--span">{{notice}}</text>
            <text class="notice__text--span">{{notice}}</text>
        </view>
        <view wx:else class="no_scroll_notice_text" style="font-size: {{fontSize}}">
            <text class="">{{notice}}</text>
        </view>
    </view>
    <!-- 关闭按钮(可以替换为自己的UI) -->
    <block wx:if="{{close}}">
		<image wx:if="{{showIcon}}" class="notice-tips-img" src="/images/meeting/notice_close.png"></image>
    </block>
</view>

子组件wxss文件


@keyframes notice {
  0% {
    transform: translateX(0);
  }

  100% {
    transform: translateX(-50%);
  }
}
.notice {
  display: flex;
  position: relative;
  align-items: center;
}
.notice__icon {
  display: block;
  width: 36rpx;
  height: 30rpx;
  line-height: 30rpx;
  margin-right: 12rpx;
}
.notice__text {
  display: inline-block;
  margin-right: 30rpx;
  white-space: nowrap;
  font-size: 28rpx;
}
.no_scroll_notice_text{
  font-size: 30rpx;
  white-space: nowrap;
}
.notice__text--wrap {
  flex: 1;
  overflow: hidden;
}
.notice__text--animate {
  animation: notice 8s linear infinite;
}
.notice__text--overlay {
  position: absolute;
  width: 44rpx;
  height: 100%;
  top: 0;
  right: 60rpx;
  z-index: 1;
}
.notice__text--span {
  display: inline-block;
  margin-right: 36rpx;
}
.notice__close {
  margin-left: 20rpx;
  color: #fff;
  font-size: 24rpx;
}
.notice-tips-img{
  width: 41rpx;
  height: 25rpx;
  margin-right: 15rpx;
  margin-top: 4rpx;
}

父组件调用

<notice 
	<!-- 根据文本长度动态设置滚动开关 例如: 文本长度 > 38 -->
  	scroll="{{true}}" 
  	<!-- 根据文本长度动态设置滚动速度 例如: 文本长度 > 80 ? 12 : 8 -->
	speed="{{10}}" 
	<!-- 公告文本 -->
	notice="{{noticeText}}" 
	<!-- 文本字体大小 -->
	fontSize="26rpx" 
	<!-- 是否显示左侧公告图标 -->
	showIcon="{{true}}">
</notice>

帮到你的话,点个赞吧(⊙_⊙)

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值