【小程序】滚动的公告栏

公告栏的展示方式一般是有两种:

  1. 垂直方向滚动式
  2. 水平方向滚动式

【垂直式公告栏】
原理:运用了小程序自带的swiper来实现
适用范围:信息量短或可跳转详情的多条公告消息。

// 数据:
		noticeList: [{
            content: '公告1',
        }, {
            content: '公告2',
        }, {
            content: '公告3',
        }, {
            content: '公告4~~~~~~~~~~~~~~~~~~~',
        }],
        
// 布局:
 <view class="verticality_box">
        <swiper class="notice_verticality" autoplay="true" vertical="true" circular="true" interval="2000" display-multiple-items='1'>
            <block wx:for="{{noticeList}}">
                <swiper-item>
                    <view class="noticecont">{{item.content}}</view>
                </swiper-item>
            </block>
        </swiper>
    </view>
    
// 样式:
.verticality_box {
    width: 100%;
    margin: 10px auto;
}

.notice_verticality {
    width: 100%;
    height: 50rpx;
    line-height: 50rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.noticecont {
    width: calc(100vw - 20px);
    line-height: 50rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

【水平式公告栏】
原理:跑马灯
适用范围:单条数据为最佳

数据:
		noticeList: [{
            content: '公告1',
        }, {
            content: '公告2',
        }, {
            content: '公告3',
        }, {
            content: '公告4~~~~~~~~~~~~~~~~~~~',
        }],
 		// 水平滚动数据
        scrollData: {
            speed: 5, //滚动速度,每秒5个字
            font_size: "16", //字体大小(px)
            text_color: "#de8c17", //字体颜色
            back_color: "#fffbe8", //背景色
        }

布局:
<view class="horizontal_box">
        <view class='marquee_text' style='--marqueeWidth--:{{-scrollData.width}}px;--speed--:{{scrollData.time}}s;width:{{scrollData.width_mal}}px;'>
            <block wx:for="{{noticeList}}" wx:key='index'>
                <view class="notice_cont" style='color:{{scrollData.text_color}};'>{{item.content}}</view>
            </block>
        </view>
    </view>

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

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

.horizontal_box {
    position: relative;
    width: 100%;
    margin: 10px auto;
    height: 50rpx;
    line-height: 50rpx;
    overflow: hidden;
}

.marquee_text {
    width: 100%;
    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;
}
.notice_cont{
    margin-right: 20px;
}


//  方法(重点):
 // 水平方向 公告栏
    noticeEvent() {
        let noticeList = this.data.noticeList,
        scrollData = this.data.scrollData,
        this_width = 0,
        spacing = 0,
        speed = (this.data.scrollData.speed * this.data.scrollData.font_size); //m每秒行走的距离
      for (let i in noticeList) {
        if (noticeList[i].content) {
            this_width += noticeList[i].content.length * this.data.scrollData.font_size + 20;
        } else {
            noticeList[i].content = ''
        }
      }
      let total_length = this_width;//总长
      scrollData.time = total_length / (speed / 2.5 ); /**滚动时间*/
      scrollData.width = total_length - wx.getSystemInfoSync().windowWidth;
      // 如果公告内容少,则不滚动
       if(total_length<= wx.getSystemInfoSync().windowWidth){
        scrollData.time = 0
       }

      this.setData({
        scrollData: scrollData,
        noticeList: noticeList
      })
    },
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个微信小程序文字公告栏的代码示例: ``` <view class="notice"> <text class="notice-text">{{notice}}</text> <text class="notice-more">更多</text> </view> ``` 其中,`.notice` 和 `.notice-text` 是样式类名,可以根据需要自行修改;`{{notice}}` 是公告文字的数据绑定,需要在对应的 Page 或 Component 中定义;`notice-more` 是一个“更多”按钮,可以根据需要添加或删除。 在对应的样式文件中,可以添加如下样式: ``` .notice { background-color: #f5f5f5; height: 40rpx; line-height: 40rpx; text-align: center; overflow: hidden; position: relative; } .notice-text { font-size: 24rpx; color: #333; margin: 0 30rpx; white-space: nowrap; animation: noticeMove 10s linear infinite; } .notice-more { font-size: 24rpx; color: #999; position: absolute; right: 20rpx; top: 50%; transform: translateY(-50%); } @keyframes noticeMove { 0% { transform: translateX(0); -webkit-transform: translateX(0); } 100% { transform: translateX(-100%); -webkit-transform: translateX(-100%); } } ``` 其中,`animation` 属性定义了公告文字滚动的动画效果,可以自行修改动画时长和动画函数;`@keyframes` 定义了动画的具体实现,可以根据需要自行修改。 在对应的 Page 或 Component 中,需要定义公告文字的数据绑定,例如: ``` Page({ data: { notice: '欢迎使用微信小程序!' } }) ``` 以上就是一个简单的微信小程序文字公告栏的代码示例,可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值