react前端ui的使用_使用React Morph变形UI过渡

news/2024/7/7 3:18:39

react前端ui的使用

Love them or hate them, animations in modern web design are probably here to stay. Unlike the glory days of jQuery, React out of the box does not provide any mechanism to perform such animations. Sure you could do a small bit of CSS wizardry to animate elements, but what if want to morph one set of elements into another? What’s were react-morph comes into play.

爱他们或恨他们,现代网页设计中的动画可能会保留下来。 与jQuery辉煌的时代不同,React开箱即用不提供任何执行此类动画的机制。 当然,您可以做一点CSS向导来制作元素动画,但是如果要将一组元素变形为另一组元素该怎么办? 什么是react-morph发挥作用。

react-morph is a small JavaScript library that provides a <ReactMorph> component that leverages render props to easily create morphing effects in your React app.

react-morph是一个小型JavaScript库,提供了一个<ReactMorph>组件,该组件利用渲染道具轻松在React应用程序中创建变形效果。

入门 (Getting Started)

Before we can get started, we need to add react-morph to our project with either npm or yarn:

在开始之前,我们需要使用npmyarn添加react-morph到我们的项目中:

# via npm
$ npm install --save react-morph

# via yarn
$ yarn add react-morph

Be sure to include react-morph in your project wherever you’d like to use it:

无论您想在哪里使用,请确保在项目中包括react-morph

import ReactMorph from "react-morph";

陷阱 (Gotchas)

Before we dive deeper into react-morph, I want to talk a bit about the caveats or “gotchas” with this library.

在更深入地讨论react-morph ,我想先谈谈关于此库的警告或“陷阱”。

First, like most open source offerings, it is a work in progress. A lot of things are works in progress though. Heck, even React is ;)

首先,像大多数开源产品一样,它仍在进行中。 尽管很多事情仍在进行中。 哎呀,甚至React都是;)

Next, react-morph is quite sensitive to whitespace as it can introduce additional spacing to an element and throw off the morphing animation. You may need to set your element to display: inline-block or wrap all of the content in another element.

接下来, react-morph对空格非常敏感,因为它可以为元素引入额外的间距并放弃变形动画。 您可能需要将元素设置为display: inline-block或将所有内容包装在另一个元素中。

As mentioned, additional spacing can cause problems so things like margins can also give you grief. Be sure to match the margins on both elements in your transition or simply wrap things up with another element.

如前所述,额外的间距可能会引起问题,因此诸如边距之类的东西也会使您感到悲伤。 确保在过渡中两个元素的边距匹配,或者简单地将另一个元素包装起来。

List items introduce similar issues and applying list-style: none should help things along.

列表项引入了类似的问题,并采用了list-style: none可以帮助事情。

If things are still acting weird, you may need to add in some placeholder elements to avoid additional distortion.

如果情况仍然很怪异,则可能需要添加一些占位符元素,以避免其他失真。

Animations in general can be tricky to get just right so I don’t think any of these little quirks should sway you from this library.

总体而言,动画制作起来可能会很困难,因此我认为这些小怪癖都不会让您从该库中脱身。

用法 (Usage)

The usage of react-morph is pretty simple.

react-morph的用法非常简单。

Using <ReactMorph> exposes a series of properties that we can use to flag our different transition states (from and to), animation tweens (fadeIn and fadeOut) and a function that you can fire to start the transition (go).

使用<ReactMorph>可以显示一系列属性,我们可以使用这些属性来标记不同的过渡状态( fromto ),动画补间( fadeInfadeOut )以及可以触发以开始过渡的函数( go )。

The from and to properties take a string property that will serve as the key to link the states together for transitioning.

fromto属性采用字符串属性,该属性将用作将状态链接在一起以进行过渡的键。

基本变形 (Basic Morphing)

The most basic example I could think of is taking some text and morphing it into some other text.

我能想到的最基本的示例是获取一些文本并将其变形为其他文本。

<ReactMorph>
  {({ from, to, fadeIn, fadeOut, go }) => (
    <div>
      <a onClick={() => go(1)}>
        <h3 {...from("text")} style={{ display: "inline-block" }}>
          See ya later, alligator...
        </h3>
        <p {...fadeOut()}>Click the text above ;)</p>
      </a>
      <div>
        <h3 {...to("text")} style={{ display: "inline-block" }}>
          After awhile, crocodile!
        </h3>
        <div>
          <a onClick={() => go(0)} {...fadeIn()}>
            Would you like to redo?
          </a>
        </div>
      </div>
    </div>
  )}
</ReactMorph>

Nothing too crazy here. We simply wrap both of our states in <ReactMorph>, setup our click events to use go() to go between states and to(key) to identify the parts of the state.

这里没什么太疯狂的。 我们将两个状态都包装在<ReactMorph> ,设置单击事件以使用go()在状态之间切换,并使用to(key)识别状态的各个部分。

As you can see, some of the elements have been styled to display inline-block. This removed some weird quirks with the elements popping around on the page a bit.

如您所见,某些元素已设置样式以显示inline-block 。 这消除了一些奇怪的怪癖,其中的元素在页面上突然弹出。

It’s also worth noting that react-morph appears to only handle two states. Since go() accepts an integer, I thought I’d try to set things up with a third state and well, no dice.

还值得注意的是, react-morph似乎只能处理两种状态。 由于go()接受一个整数,所以我认为我会尝试使用第三个状态来设置事物,而且没有骰子。

更复杂的用法 (More Complex Usage)

Morphing text is great to showcase what react-morph can do, but it’s probably not how you’d use this library in your app.

变形文本很好地展示了react-morph可以做什么,但可能不是您在应用程序中使用此库的方式。

A bit more of a real world example would taking a button that a user clicks on to reveal a menu and morphing said button into a menu:

现实世界中的示例将带有一个用户单击的按钮以显示菜单,然后将所述按钮变形为菜单:

<ReactMorph>
  {({ from, to, go }) => (
    <div
      style={{
        display: "flex",
        justifyContent: "center",
        position: "relative",
      }}
    >
      <button
        onClick={() => go(1)}
        {...from("menu")}
        style={{ position: "absolute" }}
      >
        Click to Open
      </button>
      <div
        style={{
          backgroundColor: "#6DB65B",
          border: "2px solid #008f68",
          padding: 10,
          position: "absolute",
        }}
        {...to("menu")}
      >
        <div
          style={{
            alignItems: "center",
            display: "flex",
            justifyContent: "space-between",
            width: "100%",
          }}
        >
          <strong>Menu</strong>
          <button onClick={() => go(0)}>X</button>
        </div>
        <hr />
        <div>Menu Item 1</div>
        <div>Menu Item 2</div>
        <div>Menu Item 3</div>
      </div>
    </div>
  )}
</ReactMorph>

Same concepts as before, just a bit of a different approach to the styling to get each state to seemingly occupy the same position.

与以前相同的概念,只是样式有所不同,使每个状态看起来都占据相同的位置。

结论 (Conclusion)

Caveats aside, react-morph makes it very easy to drop in some more sophisticated morphing animations between elements in your React app.

除了警告之外, react-morph使得在React应用程序中的元素之间放一些更复杂的变形动画非常容易。

I hope you enjoyed this post and if you’re interested in seeing a demo of the code from this post, you can check it out over on CodeSandbox.

希望您喜欢这篇文章,如果您有兴趣查看这篇文章中的代码演示,可以在CodeSandbox上进行查看 。

Enjoy!

请享用!

翻译自: https://www.digitalocean.com/community/tutorials/react-react-morph

react前端ui的使用


http://www.niftyadmin.cn/n/3648306.html

相关文章

技术英雄会【新闻】CSDN最有价值博客TOP10颁奖【图】【我在左边数第四个】

2007年04月06日 10:04 新浪科技夹带些私货&#xff0c;呵呵&#xff1a;社区英雄会【一】&#xff1a;问周鸿祎一个问题社区英雄会【二】&#xff1a;问CSDN一个信息过滤器的问题技术英雄会【三】&#xff1a;社区英雄们的与会感言大赏技术英雄会【四】&#xff1a;也谈如何发…

Python 简介

Python - 简介 PythonPython&#xff08;[KK] 英语发音&#xff1a; /paɪθɑn/, [DJ] 英语发音&#xff1a; /ˈpaiθən/&#xff09;&#xff0c;是一种面向对象、直译式的计算机程序设计语言&#xff0c;也是一种功能强大的通用型语言&#xff0c;已经具有近二十年的发展历…

如何使用Google Chrome DevTools和Visual Studio Code调试JavaScript

介绍 (Introduction) Learning to debug is an essential skill for developers, as it allows them to efficiently fix errors during development. Knowing how to use debugging tools may not always be obvious when working with JavaScript outside of an integrated d…

技术英雄会【二】:问CSDN一个信息过滤器的问题

zhengyun_ustc 20070406刘韧的《4月6日CSDN软件英雄会》中&#xff0c;这句话无意中透露出了CSDN的一个野心&#xff1a;“CSDN这个过滤器&#xff0c;将108人筛选出来&#xff0c;给了这108人抛头露面的机会&#xff0c;这108人同时也成了投资人、创业者、合作者的机会。”4月…

如何使用Traefik 1.7.21作为CentOS 7上Docker容器的反向代理

The author selected Girls Who Code to receive a donation as part of the Write for DOnations program. 作者选择了《编码的女孩》作为Write for DOnations计划的一部分来接受捐赠。 介绍 (Introduction) Docker can be an efficient way to run web applications in prod…

nginx-持续更新

Nginx yum安装 配置Nginx的yum源 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1安装以及重启,检查 yum -y install nginx 默认安装最新版本 启动 关闭 重启 加载 systemctl start/stop/restart/relo…

技术英雄会【三】:社区英雄们的与会感言大赏

zhengyun_ustc 20070406Update&#xff1a;曾老师已经整理了一份非常完整的与会英雄感言清单&#xff0c;敬请收看&#xff1a;CSDN技术英雄会&#xff0c;网友怎么看&#xff1f; 不知道技术英雄大会的各位英雄们的与会心得博文&#xff0c;在哪里能整理出来一个清单&#x…

redis 运维实际经验纪录之一

http://blogread.cn/it/article/1635?fsa redis 改版的项目上线有两个月了,记录一下redis 相关的经验,也给大家一个参照: 我们的redis server是一主一从,使用R710的机器,8核心,24G内存; 每天约插入200万左右的数据,现在库里有3000万条纪录,占用了9G的内存;由于现在每天内存增长…