小程序兩種滾動(dòng)公告欄的實(shí)現(xiàn)方法
先上效果圖:
橫向滾動(dòng)欄實(shí)現(xiàn):
網(wǎng)上的幾種方案或多或少都有一些問題:
1.setData定時(shí)器更新text view的margin-left方法,由于setData的毫秒延時(shí),動(dòng)畫播放起來一卡一卡;
2.純CSS實(shí)現(xiàn),keyframe等,無法實(shí)現(xiàn)循環(huán)播放的設(shè)置;
3.使用string.length*font-size的方法獲取字符串像素長度,不夠精確,多次循環(huán)播放后誤差會累積變大。
我采用的animate動(dòng)畫方法,實(shí)測動(dòng)畫流暢,循環(huán)播放無誤差。
橫向滾動(dòng)代碼如下所示
// wxml <view class='notice'> <view class="left"> <text class='iconfont icon-labagonggao voice'></text> <view class="left-box"> <view class="left-text"></view> <view class='content-box'> <view class='content-text' animation="{{animationData}}"><text id="text">{{text}}</text></view> </view> <view class="right-text"></view> </view> </view> <view class="right" bindtap="goApp"> <!-- <image class="app" mode="aspectFit" src="/assets/images/app.png" style="width:{{widthrpx}}rpx" bindload="imageLoad"></image> --> <text class="more">更多應(yīng)用></text> </view> </view> // wxss .notice { display: flex; align-content: center; justify-content: space-between; padding: 10rpx 25rpx; background: #f1f1f1; } .left { display: flex; align-items: center; } .voice { margin-right: 5rpx; margin-top: 2rpx; color: #fa6016; } .left-box { position: relative; display: flex; align-items: center; } .left-text { position: absolute; left: 0; width: 40rpx; height: 100%; background: linear-gradient(to left,rgba(241,241,241,0),rgba(241,241,241,1)); z-index: 99; } .right-text { position: absolute; right: -1rpx; width: 40rpx; height: 100%; background: linear-gradient(to left,rgba(241,241,241,1),rgba(241,241,241,0)); z-index: 99; } .content-box { overflow: hidden; width: 350rpx; } .content-text { color: #5e5e5e; white-space: nowrap; font-size: 28rpx; } .right { display: flex; align-items: center; } .app { height: 48rpx; } .more { margin-left: 5rpx; color: #aaa; font-size: 32rpx; } // js data: { text: "1.【評分標(biāo)準(zhǔn)】頁可以查看不同年齡段的評分標(biāo)準(zhǔn),通過首頁選擇對應(yīng)的性別、類別和年齡。2.【單項(xiàng)成績】頁包含了詳細(xì)的單項(xiàng)打分情況及成績雷達(dá)圖,直觀地看出自己的弱項(xiàng)和強(qiáng)項(xiàng)。", animation: null, timer: null, duration: 0, textWidth: 0, wrapWidth: 0 }, onShow() { this.initAnimation(this.data.text) }, onHide() { this.destroyTimer() this.setData({ timer: null }) }, onUnload() { this.destroyTimer() this.setData({ timer: null }) }, destroyTimer() { if (this.data.timer) { clearTimeout(this.data.timer); } }, /** * 開啟公告字幕滾動(dòng)動(dòng)畫 * @param {String} text 公告內(nèi)容 * @return {[type]} */ initAnimation(text) { let that = this this.data.duration = 15000 this.data.animation = wx.createAnimation({ duration: this.data.duration, timingFunction: 'linear' }) let query = wx.createSelectorQuery() query.select('.content-box').boundingClientRect() query.select('#text').boundingClientRect() query.exec((rect) => { that.setData({ wrapWidth: rect[0].width, textWidth: rect[1].width }, () => { this.startAnimation() }) }) }, // 定時(shí)器動(dòng)畫 startAnimation() { //reset // this.data.animation.option.transition.duration = 0 const resetAnimation = this.data.animation.translateX(this.data.wrapWidth).step({ duration: 0 }) this.setData({ animationData: resetAnimation.export() }) // this.data.animation.option.transition.duration = this.data.duration const animationData = this.data.animation.translateX(-this.data.textWidth).step({ duration: this.data.duration }) setTimeout(() => { this.setData({ animationData: animationData.export() }) }, 100) const timer = setTimeout(() => { this.startAnimation() }, this.data.duration) this.setData({ timer }) },
sipwer滾動(dòng)方式代碼如下所示
swiper默認(rèn)為橫向滾動(dòng),vertical為true,則縱向滾動(dòng)
// wxml <!-- 公告 --> <text class='swiper-notice'>公告:</text> <swiper class='swiper-container' autoplay='true' vertical='true' circular='true' interval='2000'> <block wx:for='{{msgList}}'> <navigator url='/pages/notice/notice?title={{item.url}}' open-type='navigate'> <swiper-item> <view class='swiper-item'>{{item.title}}</view> </swiper-item> </navigator> </block> </swiper> <!-- 公告 end --> // wxss /* 公告start */ .swiper-notice { font-size: 28rpx; color: #fa6016; } .swiper-container { margin-left: 10rpx; width: 400rpx; height: 100%; } .swiper-item { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; font-size: 28rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: 2rpx; } /* 公告end */
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 函數(shù)用法詳解【函數(shù)定義、參數(shù)、綁定、作用域、閉包等】
這篇文章主要介紹了JavaScript 函數(shù)用法,結(jié)合實(shí)例形式分析了JavaScript函數(shù)定義、參數(shù)、綁定、作用域、閉包、回調(diào)函數(shù)、柯理化函數(shù)等相關(guān)概念、原理與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05微信小程序?qū)崿F(xiàn)可拖動(dòng)懸浮圖標(biāo)的示例代碼
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)可拖動(dòng)懸浮圖標(biāo)的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12Electron中實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳功能
Electron是開源的框架,可以使用h5來開發(fā)跨平臺pc桌面應(yīng)用,這樣前端開發(fā)這可以開發(fā)桌面應(yīng)用了。這篇文章主要介紹了Electron中實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳功能,需要的朋友可以參考下2018-10-10JQuery 表格操作(交替顯示、拖動(dòng)表格行、選擇行等)
JQuery 表格操作包括交替顯示、拖動(dòng)表格行、選擇行等功能,大家可以參考下。2009-07-07JavaScript實(shí)現(xiàn)俄羅斯方塊游戲過程分析及源碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)俄羅斯方塊游戲過程分析及源碼分享,本文分解了游戲規(guī)則、實(shí)現(xiàn)過程、難點(diǎn)分析及實(shí)現(xiàn)源碼,需要的朋友可以參考下2015-03-03JavaScript中使用ActiveXObject操作本地文件夾的方法
以前一直用vbscript來操作文件夾,才發(fā)現(xiàn)原來使用JavaScript也是可以的,肯定不如vbs用的簡單,不過學(xué)習(xí)一下還是不錯(cuò)的2014-03-03