微信小程序全屏滾動字幕的實現(xiàn)方法詳解
實現(xiàn)效果

一、實現(xiàn)背景
無意中在某音上看到用手機橫屏作為廣告屏的視頻,大部分都是用第三方軟件實現(xiàn)的;

以及在汽車后擋風玻璃放置提醒字樣的視頻,這種基本是要花錢買屏幕,通過手機控制屏幕內(nèi)容;

遂想實現(xiàn)這種效果
二、實現(xiàn)代碼
1,滾動字幕
zimu.wxml,界面布局,很簡單,沒啥特別的,頂部一個返回按鈕,為了不影響整體效果,可以把這個按鈕做成透明的圖片放上去;除了那個按鈕剩下的就是滾動的字幕組件了
<!--pages/zimu/zimu.wxml-->
<view class="parent">
<view class="topview">
<image class="topback" src="/image/clock_back.png" mode="widthFix" bindtap="onBack"/>
</view>
<view class="marqueeView1">
<text class="marqueeText1" style="--during--:{{during}}s;" decode> {{mark}}</text>
</view>
</view>zimu.wxss
/* pages/zimu/zimu.wxss */
/* xm.wxss是一個字體樣式文件,可不要 */
/*@import '../../style/xm.wxss';*/
page {
background: black;
width: 100%;
height: 100%;
}
.parent {
height: 100%;
width: 100%;
position: relative;
z-index: 1;
}
.marqueeView1 {
position: absolute;
z-index: 2;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
margin: 10rpx auto;
overflow: hidden;
/* background: #fff; */
border-radius: 5px;
padding: 5px;
box-sizing: border-box;
}
.marqueeText1 {
color: white;
font-size: 250rpx;
font-family: "DS-Digital";
/* font-family: "Courier New", Courier, monospace; */
white-space: nowrap;
/* infinite無限循環(huán) 10s*/
animation: 10s loop linear infinite normal;
display: inline-block;
vertical-align: top;
}
@keyframes loop {
0% {
transform: translateX(350px);
-webkit-transform: translateX(350px);
}
100% {
transform: translateX(-100%);
-webkit-transform: translateX(-100%);
}
}
@-webkit-keyframes loop {
0% {
transform: translateX(1000px);
-webkit-transform: translateX(1000px);
}
100% {
transform: translateX(-75%);
-webkit-transform: translateX(-75%);
}
}
.topview {
position: absolute;
z-index: 4;
margin-top: 10rpx;
}
.topback {
margin-left: 20rpx;
padding: 10px;
width: 30px;
height: 30px;
/* background: red; */
}zimu.json,配置這個頁面橫屏展示,landscape,背景色為黑色
{
"usingComponents": {},
"pageOrientation": "landscape",
"navigationBarBackgroundColor": "#000000",
"navigationStyle": "custom",
"navigationBarTextStyle": "white"
}zimu.js,主要是onload函數(shù),接收了上一個界面的傳參,把內(nèi)容和滾動速度參數(shù)傳過來,當然也可以加其他參數(shù),比如說字體顏色等
data: {
mark:'測試滾動字幕',
marqueeWidth:0
},
onBack: function(){
wx.navigateBack({
delta:1
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad(options) {
this.setData({
mark: options.mark,
})
},
三、滾動速度

(1)新增一個時間變量,在wxss中引用,這個during來自于wxml中定義
animation: var(--during--) loop linear infinite normal;
<text class="marqueeText1" style="--during--:{{during}}s;" decode> {{mark}}</text>
(2)控制滾動速度的是一個radioGroup組件,內(nèi)含三個radio單選按鈕,通過綁定bindChange事件獲取單選按鈕的值傳到下一個界面使用
(3)根據(jù)文字的長度和選擇的滾動速度計算出動畫所需要的事件,這里默認正常速度一個字一秒。
data: {
mark:'測試滾動字幕',
speed: 2,
during:10,
marqueeWidth:0
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad(options) {
console.log(options.mark+options.speed)
var consumeTime = 10
if(options.speed == 1){
consumeTime = options.mark.length * 2
}else if(options.speed == 2){
consumeTime = options.mark.length
}else if(options.speed == 3){
consumeTime = options.mark.length / 2
}
this.setData({
mark: ' '+options.mark,
during: consumeTime
})
},
(4)給輸入框添加清空按鈕,添加一個icon跟在文字的后面
<view class='clear-clear'>
<icon type="clear" size="30" catchtap='clearInput'/>
</view>
clearInput: function (e) {
this.setData({
mark:''
})
},
四、后續(xù)優(yōu)化
1,可以添加動態(tài)表情圖片
2,可以添加修改文字顏色
3,可以添加語音播報
到此這篇關(guān)于微信小程序全屏滾動字幕的實現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)小程序全屏滾動字幕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Gulp實現(xiàn)靜態(tài)網(wǎng)頁模塊化的方法詳解
眾所周知Gulp.js 是一個自動化構(gòu)建工具,開發(fā)者可以使用它在項目開發(fā)過程中自動執(zhí)行常見任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Gulp實現(xiàn)靜態(tài)網(wǎng)頁模塊化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2018-01-01
JS從非數(shù)組對象轉(zhuǎn)數(shù)組的方法小結(jié)
這篇文章主要給大家介紹了關(guān)于JS從非數(shù)組對象轉(zhuǎn)數(shù)組的一些方法,分別是Array.prototype.slice.call(obj)、Array.from(obj)、[…obj]和Object.values(obj)等方法的詳細實現(xiàn)方法,需要的朋友可以參考下。2018-03-03
js報錯 Object doesn''t support this property or method的原因分析
運行js是出現(xiàn)Object doesn't support this property or method 錯誤的可能原因分析。2011-03-03
使用bootstrap實現(xiàn)下拉框搜索功能的實例講解
今天小編就為大家分享一篇使用bootstrap實現(xiàn)下拉框搜索功能的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

