小程序实现左右来回滚动字幕效果


Posted in Javascript onDecember 28, 2018

本文实例为大家分享了小程序左右来回滚动字幕的具体代码,供大家参考,具体内容如下

小程序实现左右来回滚动字幕效果

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>
 
<!--弹幕开关-->
<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">发射</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",
 },
 
 
 // 生命周期函数--监听页面加载
 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);
 },
 
 //是否打开弹幕...
 barrageSwitch: function (e) {
 console.log(e);
 //先判断没有打开
 if (!e.detail.value) {
  //清空弹幕
  barrage_style_arr = [];
  //设置data的值
  this.setData({
  barrageTextColor: "#D3D3D3",
  barrage_inputText: "none",
  barragefly_display: "none",
  barrage_style: barrage_style_arr,
  });
  //清除定时器
  clearInterval(timer);
 } else {
  this.setData({
  barrageTextColor: "#04BE02",
  barrage_inputText: "flex",
  barragefly_display: "block",
  });
  //打开定时器
  timer = setInterval(this.barrageText_move, 800)
 }
 },
 
 //发射按钮
 shoot: function (e) {
 
 //字体颜色随机
 var textColor = "rgb(" + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + ")";
 // //设置弹幕字体的水平位置样式
 // var textWidth = -(this.data.bind_shootValue.length*0);
 //设置弹幕字体的垂直位置样式
 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,  //发送弹幕
  bind_shootValue: ""     //清空输入框
 })
 
 //定时器 让弹幕动起来
 // this.timer= setInterval(this.barrageText_move,800);
 
 },
 
 //定时器 让弹幕动起来
 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--;
  //全部弹幕运行完
  if (barrage_style_arr.length == 0) {
   this.setData({
   barrage_style: barrage_style_arr,
   })
   // clearInterval(this.timer);
   return;
  }
  }
  console.log("第" + i + "个定时器:", textMove);
  this.setData({
  barrage_style: barrage_style_arr,
  })
 }
 
 
 },
 
 //绑定发射输入框,将值传递给data里的bind_shootValue,发射的时候调用
 bind_shoot: function (e) {
 this.setData({
  bind_shootValue: e.detail.value
 })
 },
 
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript 操作键盘的Enter事件(键盘任何事件),兼容多浏览器
Oct 11 Javascript
JS操作select下拉框动态变动(创建/删除/获取)
Jun 02 Javascript
javascript预加载图片、css、js的方法示例介绍
Oct 14 Javascript
浅谈Jquery为元素绑定事件
Apr 27 Javascript
理解Javascript的call、apply
Dec 16 Javascript
学习JavaScript设计模式之代理模式
Jan 12 Javascript
jquery配合.NET实现点击指定绑定数据并且能够一键下载
Oct 28 Javascript
js实现tab切换效果
Feb 16 Javascript
js实现鼠标拖拽多选功能示例
Aug 01 Javascript
微信小程序实践之动态控制组件的显示/隐藏功能
Jul 18 Javascript
微信小程序使用Vant Weapp组件库的方法步骤
Aug 01 Javascript
mustache.js实现首页元件动态渲染的示例代码
Dec 28 Javascript
原生JS实现的自动轮播图功能详解
Dec 28 #Javascript
jQuery实现的自定义轮播图功能详解
Dec 28 #jQuery
微信小程序实现简单跑马灯效果
May 26 #Javascript
详解如何快速配置webpack多入口脚手架
Dec 28 #Javascript
详解puppeteer使用代理
Dec 27 #Javascript
Angular(5.2-&gt;6.1)升级小结
Dec 27 #Javascript
详解angular2 控制视图的封装模式
Dec 27 #Javascript
You might like
PHPMyAdmin 快速配置方法
2009/05/11 PHP
php ignore_user_abort与register_shutdown_function 使用方法
2009/06/14 PHP
php 常用类汇总 推荐收藏
2010/05/13 PHP
php  PATH_SEPARATOR判断当前服务器系统类型实例
2016/10/28 PHP
PHP中strtr与str_replace函数运行性能简单测试示例
2019/06/22 PHP
常用参考资料(手册)下载或者链接
2006/07/22 Javascript
零基础学JavaScript最新动画教程+iso光盘下载
2008/01/22 Javascript
浏览器脚本兼容 文本框中,回车键触发事件的兼容
2010/06/21 Javascript
jquery实现带单选按钮的表格行选中时高亮显示
2013/08/01 Javascript
jQuery的one()方法用法实例
2015/01/19 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
js实现类似jquery里animate动画效果的方法
2015/04/10 Javascript
微信小程序动态添加分享数据
2017/06/14 Javascript
详解tween.js的使用教程
2017/09/14 Javascript
使用store来优化React组件的方法
2017/10/23 Javascript
使用Angular CLI从蓝本生成代码详解
2018/03/24 Javascript
vue实现验证码按钮倒计时功能
2018/04/10 Javascript
vuejs实现ready函数加载完之后执行某个函数的方法
2018/08/31 Javascript
对angularJs中$sce服务安全显示html文本的实例
2018/09/30 Javascript
npm scripts 使用指南详解
2018/10/08 Javascript
vue下的@change事件的实现
2019/10/25 Javascript
如何正确理解vue中的key详解
2019/11/02 Javascript
vue+canvas实现拼图小游戏
2020/09/18 Javascript
javascript实现移动端轮播图
2020/12/09 Javascript
python实现倒计时的示例
2014/02/14 Python
python调用Matplotlib绘制分布点并且添加标签
2018/05/31 Python
Python文件读写保存操作的示例代码
2018/09/14 Python
Python3.6简单的操作Mysql数据库的三个实例
2018/10/17 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
2018/10/28 Python
Python 3.3实现计算两个日期间隔秒数/天数的方法示例
2019/01/07 Python
python3实现飞机大战
2020/11/29 Python
中国一家专注拼团的社交购物网站:拼多多
2018/06/13 全球购物
家居装修公司创业计划书范文
2014/03/20 职场文书
计算机网络专业求职信
2014/06/05 职场文书
会计岗位说明书
2014/07/29 职场文书
2016年助残日旅游活动总结
2016/04/01 职场文书