微信小程序从右向左无限滚动组件封装(类似公告)

组件代码

wxml代码

js 复制代码
<view class="container">
    <view class="text-wrapper">
        <!-- 这两个都不能删除,用来放无限滚动的,第一个滚完第二个接上,第二个滚完第一个又接上了 -->
        <text class="scroll-text text1" style="left: {{-moveleft}}px;">{{ scrollText }}</text>
        <text class="scroll-text text2" style="left: {{ moveleft2}}px;">{{ scrollText }}</text>
    </view>
</view>

wxss代码

css 复制代码
.container {
    display: flex;
    width: 100vw;
}

.text-wrapper {
    display: flex;
    width: 100vw;
    height: 50rpx;
    overflow: hidden;
    position: relative;
    margin-right: 20rpx;
    margin-left: 20rpx;
}

.scroll-text {
    white-space: nowrap;
}

.text1 {
    position: absolute;
    white-space: pre;
    font-size: 26.37rpx;
    line-height: 28rpx;
    color: #FAEECC;
}

.text2 {
    position: absolute;
    white-space: pre;
    font-size: 26.37rpx;
    line-height: 28rpx;
    color: #FAEECC;
}

js代码

js 复制代码
Component({
    /**
     * 组件的属性列表
     */
    properties: {
      // 两文本的内容间隔
      interval_widgth: {
        type: Number,
        value: 10
      },
      scrollText:{
        type:String,
        value:'这里是需要无缝滚动的文本,用于检测滚动效果是否正常!'
      }
    },
   
    /**
     * 组件的初始数据
     */
    data: {
   
      moveleft:0,
      moveleft2:0,
      text_width:0,
      timer:null
    },
   
    /**
     * 组件的方法列表
     */
    methods: {
   
    },
    lifetimes: {
      attached() {
      },
      ready() {
        // 获取内容的宽度
        const query = wx.createSelectorQuery().in(this);
        query.select('.scroll-text').boundingClientRect().exec((res) => {
          this.setData({
            'text_width':res[0].width,
            moveleft2: res[0].width+this.data['interval_widgth']
          })
        });
      },
      detached(){
        clearInterval(this.data.timer);
      }
    },
    pageLifetimes: {
      show() {
        this.data.timer=setInterval(()=>{
          if(this.data.moveleft>=this.data.text_width+this.data.interval_widgth){
            this.setData({
              moveleft:-(this.data.interval_widgth+this.data.text_width)
            })
          };
          if(-(this.data.moveleft2)>=this.data.text_width+this.data.interval_widgth){
            this.setData({
              moveleft2:(this.data.interval_widgth+this.data.text_width)
            })
          };
          this.setData({
            moveleft:this.data.moveleft+2,
            moveleft2:this.data.moveleft2-2
          })
        },20)
       
      },
      hide(){
        clearInterval(this.data.timer);
      }
    }
  })

页面使用

在页面的json文件引入组件

json 复制代码
{
    "usingComponents": {
        "gundong": "../../components/gundong/gundong"
    }
}

使用

wxml 复制代码
<gundong></gundong>

这里也支持传值,如果封装的值或者样式不满足需求可以修改组件传入更多数据,不影响滚动功能即可

相关推荐
软件聚导航7 小时前
「聚小软 AI 助手」技术升级:从数据库检索到 RAG 知识库问答
前端·数据库·mysql·微信小程序·小程序·ai编程·rag
一叶星殇10 小时前
微信小程序 Vant Popup 滚动穿透:页面级与组件级解决方案详解
微信小程序·小程序
2601_9638699516 小时前
【计算机毕业设计】基于微信小程序的点餐传菜系统设计与实现
微信小程序·小程序·课程设计
show43319 小时前
AI证件照生成工具推荐:小程序vsAPPvs网页全对比
人工智能·小程序
北漂燕郊杨哥19 小时前
VS Code 安装微信小程序 MCP 介绍
vscode·微信小程序·小程序·mcp
show43320 小时前
2026小程序端AI配音技术实现:TTS多音色引擎集成与MP3生成优化
人工智能·小程序
CRMEB1 天前
不懂设计也能有精美商城?CRMEB 主题广场与可视化装修的“千店千面“实操
人工智能·ai·小程序·开源·php
维双云1 天前
2026微信小程序开店用哪个平台?长期稳定运营的系统选择方法
微信小程序·小程序
Ai-_Man1 天前
我是想导出豆包智能体的聊天记录,能分开用户和智能体对话吗?记录有点太多了
人工智能·ai·小程序
辛迪聊物业数字化1 天前
【智能楼宇系统有什么用?赋能园区高效 节能 安全】
经验分享·安全·微信小程序·php