// static/nativeComponents/Notice/index.js let app = getApp() Component({ /** * 组件的属性列表 */ properties: { datas : { type : Object } }, /** * 组件的初始数据 */ data: { timer:null, newLeft : 0 }, ready(){ let marqueeWidth = 0; let screenWidth = 0; let _this = this this.createSelectorQuery().select('.marquee').boundingClientRect(function(res){ if (res) { marqueeWidth = res.width _this.createSelectorQuery().select('.notice').boundingClientRect(function (res1) { if (res1) { screenWidth = res1.width console.log(marqueeWidth, screenWidth, 'screenWidth') if (marqueeWidth > screenWidth) { _this.marqueeMove(marqueeWidth,screenWidth); }else{ clearInterval(_this.data.timer); _this.setData({ newLeft : 0 }) } } }).exec() } }).exec() }, /** * 组件的方法列表 */ methods: { marqueeMove(marqueeWidth,screenWidth){ clearInterval(this.data.timer); this.data.timer = setInterval(() => { this.setData({ newLeft : this.data.newLeft - 1 }) if(Math.abs(this.data.newLeft) >= marqueeWidth + 200){ this.setData({ newLeft: screenWidth }) } },10) }, onclickHandle(){ app.$themeToLink(this.data.datas.componentData.link) } } })