微信小程序滚动字幕
先演示
上代碼
index.wxml
index.json
{"navigationStyle": "custom" }index.js
// canvas.js var helloData = {name: 'Weixin',systemInfo:{},windowInfo:{},widthP: 100,heightP: 100,leftFlag: false,speed: 60,marginLeft:-100,color: "#ff6f10", //按鈕顏色disabled: false, //是否可以點(diǎn)擊getCode: "開(kāi)始滾動(dòng)", //顯示文字hidden:'',text:'', }Page({data: helloData,changeName: function(e) {// sent data change to viewthis.setData({name: 'MINA'})},onReady() {wx.getSystemInfo({success: (result) => {console.log(result)this.setData({systemInfo:result,marginLeft:result.windowWidth})},}),wx.setNavigationBarColor({frontColor: '#000000',backgroundColor: '#000000'})// wx.setBackgroundColor({// backgroundColor: '#000000',// })// const query = wx.createSelectorQuery()// query.select('#myCanvas')// .fields({ node: true, size: true })// .exec((res) => {// const canvas = res[0].node// const ctx = canvas.getContext('2d')// const dpr = wx.getSystemInfoSync().pixelRatio// canvas.width = res[0].width * dpr// canvas.height = res[0].height * dpr// ctx.scale(dpr, dpr)// ctx.fillRect(0, 0, 100, 100)// ctx.fillStyle = "red"// ctx.fontSize = 48// ctx.fillText('hello world',100,100)// })// }},sendCode: function(e) {var that = this;var times = 0var i = setInterval(function() {times++if (times >= 99999999) {that.setData({color: "#ff6f10",disabled: false,getCode: "開(kāi)始滾動(dòng)",})clearInterval(i)} else {console.log(times)if(times>80){times=-20}that.setData({getCode: "重新獲取" + times + "s",marginLeft:times*5,disabled: true,})}}, 100) } , formSubmit(e) {console.log('form發(fā)生了submit事件,攜帶數(shù)據(jù)為:', e.detail.value);const settingInfo = e.detail.value;if(settingInfo.content===""){wx.showToast({icon:'error',title: '內(nèi)容必填',})return;}if(settingInfo.speed===""){wx.showToast({icon:'error',title: '速度必填',})return; }settingInfo.fontSize=Number(settingInfo.fontSize);if(settingInfo.fontSize===""){wx.showToast({icon:'error',title: '字體大小必填',})return;}if(settingInfo.color===""){wx.showToast({icon:'error',title: '字體顏色必填',})return;}this.setData({color: settingInfo.color,speed: Number(settingInfo.speed),content:settingInfo.content,fontSize:Number(settingInfo.fontSize),hidden:'hidden'})console.log(this.data)var that = this;var times = 0const speed = this.data.speedconst windowWidth = this.data.systemInfo.windowWidthvar marginLeft = windowWidthconst textLength = this.data.content.lengthconst fontSize = this.data.fontSizevar i = setInterval(function() {times++marginLeft-=speedif (times >= 99999999) {that.setData({color: "#ff6f10",disabled: false,getCode: "開(kāi)始滾動(dòng)",})clearInterval(i)} else {console.log('times:',times,'marginLeft',marginLeft)console.log(fontSize*textLength)if(marginLeft<-textLength*fontSize){marginLeft=windowWidth}that.setData({getCode: "重新獲取" + times + "s",marginLeft:marginLeft,})}}, 50) },})index.wxss
page{height:100% }.background-view{position: relative;background-color: #000000;height: 100%; }.text{position: relative;margin-top: 200px;white-space: nowrap; /*規(guī)定文本不進(jìn)行換行*/ }總結(jié)
- 上一篇: 《朗读者》的读后感优秀范文4000字
- 下一篇: 20多个可以提高你安卓开发技能的开源ap