小程序?qū)崿F(xiàn)左右來(lái)回滾動(dòng)字幕效果
本文實(shí)例為大家分享了小程序左右來(lái)回滾動(dòng)字幕的具體代碼,供大家參考,具體內(nèi)容如下

wxml:
<!--pages/market/market.wxml-->
<swiper indicator-dots="{{indicatorDots}}"
autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
<block wx:for="{{imgUrls}}" wx:key="unique">
<swiper-item>
<image src="{{item}}" class="slide-image"/>
</swiper-item>
</block>
</swiper>
<!--彈幕開(kāi)關(guān)-->
<view class="barrage-Switch" style="color:{{barrageTextColor}};">
<switch id="switch_" bindchange="barrageSwitch"/>
<text>彈幕</text>
</view>
<!--彈幕輸入框-->
<view class="barrage-inputText" style="display:{{barrage_inputText}}">
<view class="barrage-input">
<input bindblur="bind_shoot" value="{{bind_shootValue}}"/>
</view>
<view class="barrage-shoot">
<button class="shoot" size="mini" bindtap="shoot">發(fā)射</button>
</view>
</view>
<!--彈幕上單文字-->
<view class="barrage-fly" style="display:{{barragefly_display}}">
<block wx:for="{{barrage_style}}" wx:key="unique">
<text class="barrage-textFly" style="color:{{item.barrage_shoottextColor}};left:{{item.barrage_phoneWidth}}px;top:{{item.barrageText_height}}px;">{{item.barrage_shootText}}</text>
</block>
</view>
wxss:
/* pages/market/market.wxss */
.slide-image{
width: 100%;
}
/* 彈幕選擇按鈕的操作*/
.barrage-Switch{
position: absolute;
bottom: 10px;
right: 10px;
z-index: 2;
}
/* 彈幕輸入框的操作*/
.barrage-inputText{
position: absolute;
display: flex;
background-color: #BFBFBF;
width: 100%;
height: 40px;
flex-direction: row;
nav-index: 2;
justify-content: center;
align-items: center;
bottom: 10%;
}
.barrage-input{
background-color: greenyellow;
width: 60%;
height: 30px;
}
.barrage-shoot{
margin-left: 10px;
width: 25%;
height: 30px;
}
.shoot{
width: 100%;
color: black;
}
/*彈幕飛飛飛*/
.barrage-fly{
z-index: 3;
height: 80%;
width: 100%;
position: absolute;
top: 0;
}
.barrage-textFly{
position: absolute;
}
market.js
// pages/market/market.js
var barrage_style_arr = [];
var barrage_style_obj = {};
var phoneWidth = 0;
var timers = [];
var timer;
Page({
data: {
imgUrls: [
'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
],
indicatorDots: true,
autoplay: true,
interval: 3000,
duration: 500,
barrageTextColor: "#D3D3D3",
barrage_inputText: "none",
barrage_shoottextColor: "black",
bind_shootValue: "",
barrage_style: [],
barragefly_display: "none",
},
// 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
onLoad: function (options) {
var that = this;
//獲取屏幕的寬度
wx.getSystemInfo({
success: function (res) {
that.setData({
barrage_phoneWidth: res.windowWidth - 100,
})
}
})
phoneWidth = that.data.barrage_phoneWidth;
console.log(phoneWidth);
},
//是否打開(kāi)彈幕...
barrageSwitch: function (e) {
console.log(e);
//先判斷沒(méi)有打開(kāi)
if (!e.detail.value) {
//清空彈幕
barrage_style_arr = [];
//設(shè)置data的值
this.setData({
barrageTextColor: "#D3D3D3",
barrage_inputText: "none",
barragefly_display: "none",
barrage_style: barrage_style_arr,
});
//清除定時(shí)器
clearInterval(timer);
} else {
this.setData({
barrageTextColor: "#04BE02",
barrage_inputText: "flex",
barragefly_display: "block",
});
//打開(kāi)定時(shí)器
timer = setInterval(this.barrageText_move, 800)
}
},
//發(fā)射按鈕
shoot: function (e) {
//字體顏色隨機(jī)
var textColor = "rgb(" + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + ")";
// //設(shè)置彈幕字體的水平位置樣式
// var textWidth = -(this.data.bind_shootValue.length*0);
//設(shè)置彈幕字體的垂直位置樣式
var barrageText_height = (Math.random()) * 266;
barrage_style_obj = {
// textWidth:textWidth,
barrageText_height: barrageText_height,
barrage_shootText: this.data.bind_shootValue,
barrage_shoottextColor: textColor,
barrage_phoneWidth: phoneWidth
};
barrage_style_arr.push(barrage_style_obj);
this.setData({
barrage_style: barrage_style_arr, //發(fā)送彈幕
bind_shootValue: "" //清空輸入框
})
//定時(shí)器 讓彈幕動(dòng)起來(lái)
// this.timer= setInterval(this.barrageText_move,800);
},
//定時(shí)器 讓彈幕動(dòng)起來(lái)
barrageText_move: function () {
var timerNum = barrage_style_arr.length;
var textMove;
for (var i = 0; i < timerNum; i++) {
textMove = barrage_style_arr[i].barrage_phoneWidth;
console.log("barrage_style_arr[" + i + "].barrage_phoneWidth----------:" + barrage_style_arr[i].barrage_phoneWidth);
textMove = textMove - 20;
barrage_style_arr[i].barrage_phoneWidth = textMove;
//走完的移除掉
if (textMove <= -100) {
// clearTimeout(this.timer);
barrage_style_arr.splice(0, 1);
i--;
//全部彈幕運(yùn)行完
if (barrage_style_arr.length == 0) {
this.setData({
barrage_style: barrage_style_arr,
})
// clearInterval(this.timer);
return;
}
}
console.log("第" + i + "個(gè)定時(shí)器:", textMove);
this.setData({
barrage_style: barrage_style_arr,
})
}
},
//綁定發(fā)射輸入框,將值傳遞給data里的bind_shootValue,發(fā)射的時(shí)候調(diào)用
bind_shoot: function (e) {
this.setData({
bind_shootValue: e.detail.value
})
},
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析script標(biāo)簽中的defer與async屬性
最近在網(wǎng)上看到一個(gè)前輩在寫(xiě)script標(biāo)簽的時(shí)候,居然同時(shí)寫(xiě)了async和defer屬性,想著這是什么意思呢?所以決定深入的了解下這其中的學(xué)問(wèn),以下這篇文章就是個(gè)人在學(xué)習(xí)了之后的一些總結(jié)分析,有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
layui表格checkbox選擇全選樣式及功能的實(shí)例
下面小編就為大家分享一篇layui表格checkbox選擇全選樣式及功能的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
小程序開(kāi)發(fā)調(diào)用微信支付以及微信回調(diào)地址配置
本文主要介紹了小程序開(kāi)發(fā)調(diào)用微信支付以及微信回調(diào)地址配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
js屏蔽鼠標(biāo)鍵盤(pán)(右鍵/Ctrl+N/Shift+F10/F11/F5刷新/退格鍵)
屏蔽鼠標(biāo)右鍵、Ctrl+N、Shift+F10、F11、F5刷新、退格鍵/Alt+ 方向鍵 →等等,太多了就不一一寫(xiě)來(lái)了感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
js獲取修改title與jQuery獲取修改title的方法
這篇文章主要介紹了js獲取修改title與jQuery獲取修改title的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
各種頁(yè)面定時(shí)跳轉(zhuǎn)(倒計(jì)時(shí)跳轉(zhuǎn))代碼總結(jié)
下面對(duì)實(shí)現(xiàn)頁(yè)面定時(shí)跳轉(zhuǎn)(也稱倒計(jì)時(shí)跳轉(zhuǎn))做一下總結(jié),以備不時(shí)之需,經(jīng)常使用的朋友可以參考下2013-10-10
詳解webpack3如何正確引用并使用jQuery庫(kù)
本篇文章主要介紹了詳解webpack3如何正確引用并使用jQuery庫(kù),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

