栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > Web开发 > React.JS

小程序小记2:抖音最火弹幕小程序只要100行代码?!

React.JS 更新时间:发布时间: 百科书网 趣学号

主要使用的是微信小程序的动画API实现的。代码里都有注释,如何实现的直接看代码吧:

代码不明白的可以留言、或微信我:qicong88
dm.wxml




  
    {{item}}
  




  
  

dm.js



Page({

  data: {
    flyText: "我是抖音最火弹幕!",
    flyDuration: 10000,
    textColor: "rgb(255,0,155)"
  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    wx.getSystemInfo({
      success: res => {
 this.setData({
   screenHeight: res.screenHeight
 });
      }
    });
    //分享
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  onReady: function () {
    this.initBarrage(this.data.flyText);
  },

  //初始化弹幕
  initBarrage: function (flyText){
    var textArr = flyText.split("");
    var screenHeight = this.data.screenHeight; //rpx计算调整位置
    var transYHeight = screenHeight * textArr.length / 5; //px 计算动画移动Y值
    // console.log(screenHeight);

    this.setData({
      screenHeight: screenHeight,
      transYHeight: transYHeight,
      textArr: textArr,
    });

    //开始循环执行
    this.barrageAnimation();

    //定时执行
    this.inter_id = setInterval(function () {
      this.barrageAnimation();
    }.bind(this), this.data.flyDuration);
  },

  //定时器 让弹幕飞起来 
  barrageAnimation: function () {
    //开始弹幕滚动
    if (!this.animation){
      this.animation = wx.createAnimation({
 duration: 0,
 timingFunction: 'linear'
      });
    }

    //动画恢复到原位
    this.animation.translateY(this.data.screenHeight).step();
    this.setData({
      textArr: [], //文字清空的动画效果
      animationData: this.animation.export()
    });

    //延迟0.1s执行
    setTimeout(function(){
      this.animation.translateY(-this.data.transYHeight).step({ duration: this.data.flyDuration });
      this.setData({
 textArr: this.data.flyText.split(""),
 animationData: this.animation.export()
      });  
    }.bind(this), 100);

  },

  //设置弹幕
  formSubmit: function (e){
    var flyText = e.detail.value["flyText"];
    if(flyText){
      var textArr = flyText.split("");
      var screenHeight = this.data.screenHeight; //rpx计算调整位置
      var transYHeight = screenHeight * textArr.length / 5; //px 计算动画移动Y值

      this.setData({
 flyText: flyText,
 screenHeight: screenHeight,
 transYHeight: transYHeight,
 textArr: textArr,
      });

    }
  }

})

dm.wxss



.barrage-view {
    height: 100%;
    width: 100%;
    position: absolute;
  background-color: #000;
}
.barrage-viewfly {
    position: absolute; 
  font-size: 120px;
  left: 35%;
  height: 300rpx;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}
.text-item{
  margin-bottom: -60rpx;
  transform: rotate(90deg);
}
.barrage-input-block{
  position: fixed;
  bottom: 10px;
}
.barrage-input input{
  background-color: #FFF;
}
.barrage-shoot{
  margin-top: 10rpx;
}
.barrage-shoot button{
  margin-right: 10rpx;
}

图片参考下图,具体效果微信搜索 "程序盒子Store",即可看到;

https://img1.sycdn.imooc.com/5b86bb7d0001999012422208.jpg

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1042038.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号