codecamp

百度智能小程序 创建animation

swan.createAnimation

解释:创建一个动画实例 animation

方法参数

Object object

返回值

Animation

object参数说明

属性名类型必填默认值说明

duration

Number

400

动画持续时间,单位 ms 。

timingFunction

String

‘linear’

定义动画的效果

delay

Number

0

动画延迟时间,单位 ms 。

transformOrigin

String

‘50% 50% 0’

动画

timingFunction 有效值

说明

linear

以相同速度开始至结束。

ease

慢速开始,然后变快,然后慢速结束。

ease-in

慢速开始的过渡效果。

ease-in-out

动画以低速开始和结束。

ease-out

动画以低速结束。

step-start

动画第一帧就跳至结束状态直到结束。

step-end

动画一直保持开始状态,最后一帧跳到结束状态。

示例



代码示例 1: 

在开发者工具中打开

<view class="wrap">
    <view class="animation-element-wrapper flexTop">
        <view class="animation-element" animation="{{animation}}"></view>
    </view>
    <view class="card-area flexBottom swan-security-padding-bottom" style="height:{{isIPhoneX ? 5.7 : 4.1}}rem">
        <view class="top-description border-bottom">展示动画</view>
        <scroll-view scroll-y class="scroll">
            <button type="primary" bindtap="rotate">旋转</button>   
            <button type="primary" bindtap="scale">缩放</button>       
            <button type="primary" bindtap="translate">移动</button>       
            <button type="primary" bindtap="skew">倾斜</button>       
            <button type="primary" bindtap="rotateAndScale">旋转并缩放</button>       
            <button type="primary" bindtap="rotateThenScale">旋转后缩放</button>      
            <button type="primary" bindtap="all">同时展示全部动作</button>      
            <button type="primary" bindtap="allInQueue">顺序展示全部动作</button>     
            <button bindtap="reset">还原</button> 
        </scroll-view>
    </view>
</view>
Page({
    data: {
        isIPhoneX: false,
        animation: {}
    },
    onLoad() {
        this.animation = swan.createAnimation({
            duration: 1000,
            timingFunction: 'ease',
            delay: 0,
            transformOrigin: '50% 50% 0'
        });
        swan.getSystemInfo({
            success: systemInfo => {
                // 针对适配某一机型和模拟器
                if (systemInfo.model
                    && (systemInfo.model.indexOf('iPhone X') > -1)
                    || (systemInfo.model === 'iPhone Simulator <x86-64>'
                    && systemInfo.screenWidth === 375)
                ) {
                    this.setData({
                        isIPhoneX: true
                    });
                }
            }
        });
    },
    rotate() {
        this.animation.rotate(Math.random() * 720 - 360).step();
        this.setData({
            animation: this.animation.export()
        });
    },
    scale() {
        this.animation.scale(Math.random() * 2).step();
        this.setData({
            animation: this.animation.export()
        });
    },
    translate() {
        this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step();
        this.setData({
            animation: this.animation.export()
        });
    },
    skew() {
        this.animation.skew(Math.random() * 90, Math.random() * 90).step();
        this.setData({
            animation: this.animation.export()
        });
    },
    rotateAndScale() {
        this.animation.rotate(Math.random() * 720 - 360)
            .scale(Math.random() * 2)
            .step();
        this.setData({
            animation: this.animation.export()
        });
    },
    rotateThenScale() {
        this.animation.rotate(Math.random() * 720 - 360).step()
            .scale(Math.random() * 2).step();
        this.setData({
            animation: this.animation.export()
        });
    },
    all() {
        this.animation.rotate(Math.random() * 720 - 360)
            .scale(Math.random() * 2)
            .translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
            .skew(Math.random() * 90, Math.random() * 90)
            .step();
        this.setData({
            animation: this.animation.export()
        });
    },
    allInQueue() {
        this.animation.rotate(Math.random() * 720 - 360).step()
            .scale(Math.random() * 2).step()
            .translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
            .step()
            .skew(Math.random() * 90, Math.random() * 90)
            .step();
        this.setData({
            animation: this.animation.export()
        });
    },
    reset() {
        this.animation.rotate(0, 0)
            .scale(1)
            .translate(0, 0)
            .skew(0, 0)
            .step({
                duration: 0
            });
        this.setData({
            animation: this.animation.export()
        });
    }
});
.animation-element-wrapper {
    display: flex;
    overflow: hidden;
    height: 2.18rem;
    margin: .13rem .17rem;
    border-radius: 8px 8px;
    background-color: #fff;
}

.flexTop {
    position: fixed;
    height: 2.18rem;
    width: 3.88rem;
    height: 2.18rem;
}

.animation-element {
    width: .6rem;
    height: .6rem;
    margin-top: .7rem;
    margin-left: 1.6rem;
    background-color: #2b99ff;
}

.flexBottom {
    position: fixed;
    bottom: 0;
    width: 3.88rem;
}

.scroll {
    height: 3.5rem;
}

代码示例 2: 参数默认值 

在开发者工具中打开

<view class="wrap">
    <view class="anim" bindtap="startToAnimate" animation="{{animationData}}"></view>
</view>
Page({
    data: {
        animationData: {}
    },
    startToAnimate() {
        const animation = swan.createAnimation();
        animation.rotate(90).translateY(10).step();
        this.setData({
            animationData: animation.export()
        });
        console.log('createAnimation', animation);
    }
});


代码示例 3: timingFunction 

在开发者工具中打开

<view class="wrap">
    <view class="anim" bindtap="startToAnimate" animation="{{animationData}}"></view>
</view>
Page({
    data: {
        animationData: {}
    },
    startToAnimate() {
        const animation = swan.createAnimation({
            transformOrigin: '50% 50%',
            duration: 1000,
            // timingFunction可选值还有 ease,ease-in,ease-in-out,ease-out,step-start,step-end
            timingFunction: 'linear',
            delay: 0
        });
        animation.rotate(90).translateY(10).step();
        this.setData({
            animationData: animation.export()
        });
        console.log('createAnimation', animation);
    }
});


百度智能小程序 设置窗口下拉背景loading样式
百度智能小程序 Animation
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }