【小程序】微信小程序重复循环平移动画

2023-05-16

1.说明

需求是让一张图片不断重复地从下往上移动,实现方法由多种,wx.createAnimation、关键帧动画、swiper等都能实现

2.wx.createAnimation

最先想到的是使用wx.createAnimation()API,它的好处是兼容性好,只要小程序基础库版本不低于1.9.6就可以正常工作

wxml

<view style="width: 100%; height: 100%;">
  <image src="/pages/images/img1.png" animation="{{animationData}}" style="width: 100%; height: 300rpx;" />
</view>

js

Page({

  data: {
    animationData: null
  },

  onLoad(options) {
    let animation = wx.createAnimation({
      delay: 0,
      duration: 1000,
      timingFunction: 'linear',
    })
    this.animation = animation
    this.moveDistance = 200

    animation.translateY(-this.moveDistance).step()
    this.setData({
      animationData: animation.export()
    })
    this.reAnimation()
  },
  
  reAnimation: function () {
    setInterval(() => {
      console.log("000")
      this.animation.translateY(this.moveDistance).step({
        duration: 0
      })
      this.setData({
        animationData: this.animation.export()
      })
      setTimeout(() => {
        this.animation.translateY(-this.moveDistance).step()
        this.setData({
          animationData: this.animation.export()
        })
      }, 60)
    }, 2000)
  },

})

参考效果
在这里插入图片描述

2.animate关键帧动画

从小程序基础库 2.9.0 开始支持一种更友好的动画创建方式,用于代替旧的 wx.createAnimation。我们可以在js文件里直接调用this.animate(selector, keyframes, duration, callback)接口

wxml

<view style="width: 100%; height: 100%;">
  <image src="/pages/images/img1.png" id="bannerImg" style="width: 100%; height: 300rpx; margin-top: 300rpx;" />
</view>

js

Page({
  data: {},

  onLoad(options) {
    this.startAnimation()
  },

  startAnimation: function () {
    this.moveDistance = -300
    setInterval(() => {
      console.log("111")
      this.animate('#bannerImg', [{
          translateY: 0,
        },
        {
          translateY: this.moveDistance,
        },
      ], 2000, function () {
        this.clearAnimation('#bannerImg', {}, function () {})
      }.bind(this))

      this.animate('#bannerImg', [{
          translateY: this.moveDistance,
        },
        {
          translateY: 0,
        },
      ], 0, function () {
        this.clearAnimation('#bannerImg', {}, function () {})
      }.bind(this))
    }, 2000)

  }

})

参考效果
在这里插入图片描述

3.swiper

虽然wx.createAnimation()能做的动画很多,但如果只是简单的垂直或水平方向上的平移动画,其实使用swiper组件也可以完成的,swiper从基础库1.0.0 开始支持

wxml

<swiper style="height: 200rpx;" autoplay='true' circular='true' interval='1000' duration='1000' vertical='true' easing-function='linear'>
  <swiper-item wx:for="{{imgList}}" wx:key="key" wx:for-item="item">
    <image style="height: 200rpx; width: 100%;" src="{{item}}"></image>
  </swiper-item>
</swiper>

js

Page({

  data: {
    imgList: [
      "/pages/images/img1.png",
      "/pages/images/img1.png"
    ],
  },
  onLoad: function (options) {

  },
})

参考效果
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【小程序】微信小程序重复循环平移动画 的相关文章

随机推荐

  • ESP32/ESP8266 MQTT协议接入阿里云(二)

    ESP32 ESP8266 MQTT协议接入阿里云 xff08 二 xff09 1 在连接阿里云之前 xff0c 需要先了解MQTT的连接协议 CONNECT 协议格式 xff1a 固定包头 43 可变包头 43 有效载体 xff08 1
  • https是如何验证证书的有效性的

    证书验证的过程是使用非对称加密的 xff0c 客户端对服务器端发起请求 xff0c 服务器返回一个证书 xff0c 客户端验证这个证书的合法性 xff0c 如果这个证书是合法的 xff0c 那么就生成一个随机值 xff0c 利用这个随机值作
  • Kali Linux 更新源

    vi etc apt source list 添加下列更新源 中科大 deb http mirrors ustc edu cn kali kali rolling main non free contrib deb src http mir
  • 安装所有Kali 工具包

    apt get kali linux all
  • 路由选路三原则

    路由选路的三原则 最长掩码匹配原则AD值 Administrative Distance 通告距离 路由类型AD值Connect0Static1EIGRP Summary5EBGP20EIGRP 内部90OSPF110RIP120EIGRP
  • OSPF7种状态

  • CentOS 7 由原来的root@localhost~# 变成了-bash-4.2#

    发生这种原因可能是 root 目录下缺少了几个配置 bashrc 和 bash profile 进入 etc skel 目录下 将 bashrc 和 bash profile复制到 root 目录下 1 cp etc skel bashrc
  • Kali 中 dnsdict6 安装过程

    更新下载源 文件目录 etc apt source list 增加源deb http mirrors ustc edu cn kali kali rolling main non free contrib deb src http mirr
  • 在CentOS 7上搭建代理服务器(Socks 5)

    安装环境配置 1 yum install gcc 2 yum install openldap devel 3 yum install pam devel 4 yum install openssl devel 安装Socks 5 wget
  • Archlinux 安装教程 - 附详细图文(一)

    博主声明 xff1a 转载请在开头附加本文链接及作者信息 xff0c 并标记为转载 本文由博主 威威喵 原创 xff0c 请多支持与指教 本文首发于此 博主 xff1a 威威喵 博客主页 xff1a https blog csdn net
  • C语言实战——生产者消费者问题

    C语言实战 生产者消费者问题 方法摘要 生产者消费者共享缓冲区 xff0c 生产者向缓冲区中放数据 xff0c 消费者从缓冲取中取数据 xff0c 当缓冲区中被放满时 xff0c 生产者进程就必须进入挂起状态 xff0c 直到消费者从缓冲中
  • archlinux/manjaro 安装wps-office

    安装 需要添加AUR库并且安装好yay span class token function sudo span pacman s yay 从AUR安装 yay S wps office mui zh cn wps office mime c
  • 学C++就学服务端,先把apue和unp两卷看了,接着libevent,出来找工作应该没问题

    学C 43 43 就学服务端 xff0c 先把apue和unp两卷看了 xff0c 接着libevent xff0c 出来找工作应该没问题
  • 【2022小米秋招(2023校招)】软件开发方向 笔试题1——链表反转

    题目 xff1a 给你单链表的头指针 head 和两个整数 left 和 right xff0c 其中 left lt 61 right 请你反转从位置 left 到位置 right 的链表节点 xff0c 返回反转后的链表 输入描述 xf
  • c++17实现同步阻塞队列

    话不多说 xff0c 上代码 xff1a pragma once include lt condition variable gt include lt deque gt include lt mutex gt include lt sha
  • 【系统】VMware虚拟机安装Windows11

    去年微软推出了Windows11操作系统 xff0c 但由于新系统BUG多或者纯属更喜欢win10等原因 xff0c 很多同学都跟冰冰一样依旧不选择升级 xff0c 但有些情况又需要使用win11 xff0c 比如说使用某些软件或者做测试等
  • 【js】点击让窗口抖动动画效果

    比如说用户的未输入密码就点击登录按钮 xff0c 则输入框会晃动一下提示用户需要输入 xff0c 实现这种效果很简单 xff0c 只需要给元素添加一个类 xff0c 然后做一个关键帧动画即可 css代码 span class token s
  • 【unity 】第一人称角色控制器手机虚拟双摇杆

    1 说明 第一人称角色控制器很常见 xff0c unity的标准资源包里也有 xff0c 但试了一下 xff0c 那个好像只有摇杆移动方向 xff0c 无法使用摇杆进行视角旋转 xff0c 所以我这里还是自己动手实现一个吧 制作两个虚拟摇杆
  • 【python】多线程下载m3u8分段视频

    1 说明 m3u8是一种传输数据的方式 xff0c 比如说一集20分钟的完整视频被分割成一千多段一两秒的小视频 xff0c 客户端播放的时候是感觉是连续 xff0c 但如果你要下载这集视频 xff0c 那就要把一千多个小视频全都下载然后自己
  • 【小程序】微信小程序重复循环平移动画

    1 说明 需求是让一张图片不断重复地从下往上移动 xff0c 实现方法由多种 xff0c wx createAnimation 关键帧动画 swiper等都能实现 2 wx createAnimation 最先想到的是使用wx create