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

组件代码

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>

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

相关推荐
Emma歌小白2 天前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子2 天前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张3 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差3 天前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬3 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106323 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI3 天前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220693 天前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧