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

效果图

.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,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
李纲明16 小时前
微信小程序次卡跨店对不上账或前台随手改余次:先核流水只增不改与统一日切,再查冲正权限与总部只读
微信小程序·devdiy·核销流水·跨店次卡·日切对账·冲正权限·总部只读
李纲明19 小时前
微信小程序维修拆机后口头加价或未确认就开工:先核验机快照与报价确认,再查补充报价与取机核验
微信小程序·devdiy·维修验机·报价确认·报价快照·补充报价·取机核验
wx1qq1com20 小时前
一桩带几台车:共享租赁桩位配比、取电与结算闭环设计
物联网·微信小程序·小程序
李纲明21 小时前
微信小程序场地长租月底对不上或口头占场撞场:先核合约预约与核销流水出账,再查附加费与转租归属
微信小程序·devdiy·场地长租·月结账单·核销流水·附加费·转租归属
2401_879692511 天前
微信小程序照相与摄像功能实现——chooseImage 与 chooseVideo 详解
微信小程序·小程序·notepad++
律宏阔2 天前
微信小程序 + Taro + TDesign 去掉 [&&&],让普通 Tailwind 类接管局部样式
前端·微信小程序·taro
律宏阔2 天前
微信小程序 + Taro + 多语言实现与国际化检查实录
前端·微信小程序·taro
李纲明3 天前
微信小程序 getSystemInfo 提示已废弃、屏幕高度算错胶囊还对不齐?getWindowInfo 和 getDeviceInfo 怎么接才稳
javascript·微信小程序·小程序·小程序开发·devdiy
dreams_dream3 天前
微信小程序-使用npm包
微信小程序·小程序·npm
L1mbo3 天前
Lite模式 缓解微信开发者工具卡顿,附 uni-app 实践方案
前端·微信小程序