首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在魅力元素中使用动画价值

如何在魅力元素中使用动画价值
EN

Stack Overflow用户
提问于 2018-01-25 18:16:37
回答 1查看 92关注 0票数 0

我正在为网络使用动画,我想知道是否有可能在一个迷人的组件中使用动画的价值?

比如:

代码语言:javascript
复制
const AnimatedImageContainer = glamorous(Animated.div)(({ translateY }) => ({
  transform: `translateY(${translateY._value}px)`,
}));

<AnimatedImageContainer
  translateY={this.state.anim.interpolate({
    inputRange: [-200, 100],
    outputRange: [200, 0],
    extrapolate: 'clamp',
  })}
>
  <AnimatedImage src={image} />
</AnimatedImageContainer>

或者说:

代码语言:javascript
复制
<AnimatedImageContainer
  css={{
    transform: [
      {
        translateY: this.state.anim.interpolate({
          inputRange: [-200, 100],
          outputRange: [200, 0],
          extrapolate: 'clamp',
        }),
      },
    ],
  }}
>
  <AnimatedImage src={image} />
</AnimatedImageContainer>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-30 23:00:53

类似于ReactiveNative您可以在createAnimatedComponent()中封装组件。

如下所示(https://codesandbox.io/s/93202y1qyo):

代码语言:javascript
复制
import React from "react";
import { render } from "react-dom";
import glamorous from "glamorous";
import Animated from "animated";

const View = glamorous.div({
    display: "flex"
});

const Button = Animated.createAnimatedComponent(
    glamorous.button({
        position: "absolute",
        fontSize: 16,
        border: "none",
        display: "inline-block",
        padding: "10px 20px",
        textAlign: "center",
        borderRadius: 4,
        color: "#fff",
        boxShadow: "0 4px 6px rgba(50,50,93,.11), 0 1px 3px rgba(0,0,0,.08)",
        backgroundColor: "red"
    })
);

class App extends React.Component {
    state = {
        animation: new Animated.Value(0)
    };

    componentDidMount() {
        const { animation } = this.state;

        Animated.timing(animation, { toValue: 1, duration: 3000 }).start();
    }

    render() {
        const { animation } = this.state;

        return (
            <View>
                <Button
                    style={{
                        opacity: animation
                    }}
                >
                    Fade in...
                </Button>
            </View>
        );
    }
}

render(<App />, document.getElementById("root"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48449427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档