微信小程序一款不错的文字动画

效果图

.js

javascript 复制代码
Page({
  data: {
    list:[],
    animation:['text-left','text-right','text-top','text-bottom'],
    text:[
      [
        '春眠不觉晓,处处闻啼鸟。',
        '夜来风雨声,花落知多少。'  
      ],
      [
        '床前明月光,疑是地上霜。',
        '举头望明月,低头思故乡。'
      ],
      [
        '千山鸟飞绝,万径人踪灭。',
        '孤舟蓑笠翁,独钓寒江雪。'
      ]
    ],
    number:-1,
  },
  randomNum(){
    return Math.floor(Math.random() * 4);	
  },
  createText(){
    this.setData({
      list:[]
    })
    const text = this.data.text
    this.data.number++
    if(this.data.number >= text.length){
      this.data.number = 0
    }
    const o = text[this.data.number]
    const arr = []
    for(let i = 0;i < o.length;i++){
      arr[i] = []
      for(let k in o[i]){
        arr[i].push({
          random:this.randomNum(),
          text:o[i][k]
        })
      }
    }
    this.setData({
      list:arr
    })
  },
  onLoad(){
    this.createText()
  }
})

.wxml

html 复制代码
<view class="container">
  <view wx:for="{{list}}" wx:key="index" class="box">
    {{item.text}}
    <view wx:for="{{item}}" wx:key="*this" class="text-animated-word list"
    style="--name:{{animation[item.random]}};" >
      {{item.text}}
    </view>
  </view>
</view>
<button bind:tap="createText">下一首</button>

.wxss

css 复制代码
.container{
  width: 70%;
  margin: 200rpx 15%;
  font-size: 32rpx;
  color: red;
}
.box{
  display: flex;
  flex-wrap:wrap;
  width: max-content;
  margin: 0 auto;
}
.list{
  margin: 4rpx;
}
.text-animated-word{
  animation-name: var(--name);
  animation-duration: 0.8s;
}
@keyframes text-top{
  from{
    opacity:0;
    transform:translate3d(0,-100vh,0);
  }
  to{
    opacity:1;
    transform:translateZ(0);
  }
}
@keyframes text-bottom{
  from{
    opacity:0;
    transform:translate3d(0,100vh,0);
  }
  to{
    opacity:1;
    transform:translateZ(0);
  }
}
@keyframes text-left{
  from{
    opacity:0;
    transform:translate3d(-100vw,0,0);
  }
  to{
    opacity:1;
    transform:translateZ(0);
  }
}
@keyframes text-right{
  from{
    opacity:0;
    transform:translate3d(100vw,0,0);
  }
  to{
    opacity:1;
    transform:translateZ(0);
  }
}

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
铲子Zzz3 小时前
Java使用接口AES进行加密+微信小程序接收解密
java·开发语言·微信小程序
春哥的研究所10 小时前
AI人工智能名片小程序源码系统,名片小程序+分销商城+AI客服,包含完整搭建教程
人工智能·微信小程序·小程序
Bruce_Json12 小时前
微信小程序ts+sassjlin-ui
微信小程序·小程序·sass
来碗盐焗星球14 小时前
记一次微信小程序AI开发的血泪史
前端·微信小程序
The_era_achievs_hero1 天前
微信小程序71~80
微信小程序·小程序
dssxyz1 天前
uniapp打包微信小程序主包过大问题_uniapp 微信小程序时主包太大和vendor.js过大
javascript·微信小程序·uni-app
澄江静如练_1 天前
微信小程序发体验版
微信小程序·小程序
流口水的兔子1 天前
作为一个新手,如果让你去用【微信小程序通过BLE实现与设备通讯】,你会怎么做,
前端·物联网·微信小程序
一念杂记1 天前
免费开源!微信小程序商城源码,快速搭建你的线上商城系统!
微信小程序·uni-app
The_era_achievs_hero2 天前
微信小程序61~70
微信小程序·小程序