react 使用css文件_用react和CSS建立表单

news/2024/7/10 0:47:16 标签: react, css, vue, js, java

react 使用css文件

背景 (Background)

After reading Vasudha Mamtani’s blog about sign-up pages, I realized I had been taking them for granted.

阅读了Vasudha Mamtani关于注册页面的博客后,我意识到我已经把它们视为理所当然了。

Having a solid sign-up page affects whether or not someone uses your website. Would you enter your email into a wonky-looking sign-up form?

拥有可靠的注册页面会影响是否有人使用您的网站。 您是否可以将电子邮件输入看上去有点奇怪的注册表格?

After looking through the examples in Mamtani’s blog, I decided to try my hand at making a better sign-in page. I came up with this:

浏览完Mamtani博客中的示例后,我决定尝试制作一个更好的登录页面。 我想出了这个 :

Image for post
~code on GitHub~ 〜GitHub上的代码〜

The plan here is for a user to only see the abstract image when switching between signing in and creating an account.

这里的计划是让用户仅在登录和创建帐户之间切换时才能看到抽象图像。

I’m trying to imply that there’s a complicated inner working to this website. We are capable of great and complicated things. Trust us!

我试图暗示这个网站的内部工作很复杂。 我们有能力做伟大而复杂的事情。 相信我们!

讲解 (Tutorial)

In the following, I’ll explain how to code one of these sign-up pages using React and CSS.

在下文中,我将解释如何使用React和CSS编写这些注册页面之一。

Link to GitHub

链接到GitHub

目录 (Table of Contents)

  • Preliminary Junk

    初步垃圾
  • Container Layout

    容器布局
  • Banner and Form Transitions

    标语和表格过渡
  • Conditionally Render Sign-In / Sign-Up

    有条件地渲染登录/注册
  • Conclusion

    结论

初步垃圾 (Preliminary Junk)

First, as has become ritual for these blogs, I installed a create-react-app, deleted default stuff, and set up file structure and components.

首先,作为这些博客的惯例,我安装了create-react-app ,删除了默认内容,并设置了文件结构和组件。

Image for post
File Structure
档案结构

As you can see, there are three main components. Container.js is my outermost element. Inside it, there are two divs for the form side and the banner side.

如您所见,有三个主要组成部分。 Container.js是我最外面的元素。 在其中,表单面和横幅面有两个div。

Then, there are two form components for whether you are signing in or creating a new account, SignIn.js and SignUp.js respectively. These are conditionally rendered on the form side of Container.js.

然后,有两个表单组件分别用于登录或创建新帐户,分别为SignIn.jsSignUp.js 。 它们有条件地呈现在Container.js的表单侧。

I added a CSS class called cfb (center flexbox). To reduce repetition, whenever I need contents of a div centered, I throw on a cfb. As needed, I add other flex-related properties, like flex-direction, to the specific element.

我添加了一个名为cfb (中央flexbox)CSS类。 为了减少重复,每当我需要以div为中心的内容时,我都会抛出cfb 。 根据需要,我向特定元素添加了其他与flex相关的属性,例如flex-direction

Image for post

容器布局 (Container Layout)

Container div

容器div

I style the main div in Container.js like this:

我在Container.js设置主div的样式如下:

Image for post
Image for post

Form Side and Banner Side

表格面和横幅面

Next, I create divs for the banner side and form side of Container.js.

接下来,我为Container.js的横幅面和表单面创建div。

Image for post

Form:

形成:

Image for post

Banner:

旗帜:

Image for post

标语和表格过渡 (Banner and Form Transitions)

Positioning

定位方式

So the transitions work properly, I set up CSS properties for position, right, z-index, and transition.

因此过渡效果正常,我为positionrightz-indextransition设置了CSS属性。

To make the divs free to move, they must be styled with a position: absolute;. Then, they will align themselves within the nearest positioned parent element. This means I also must style the main div in Container.js to have a position: relative;.

为了使div自由移动,必须为它们设置以下position: absolute; 。 然后,它们将在最靠近的父元素内对齐。 这意味着我还必须在Container.js设置主div的样式,使其具有以下position: relative;

I’ve styled the size of the divs with percentages. I use these same sizes to set how far right they should be.

我已经用百分比设置了div的大小。 我用这些相同的尺寸,以一套多远right ,他们应该是。

First, the form-side is set at right: 0;. It will sit flush on the right side of the container.

首先,将form-side设置为right: 0; 。 它会齐平地位于容器的右侧。

Because the form-side is 65% of the parent element, I set the banner-side to be right: 65%;. It starts where the form-side ends.

因为form-side是父元素的65% ,所以我将banner-side设置为right: 65%; 。 它从form-side结束的地方开始。

To get banner-side to hover over form-side, I give banner-side a z-index: 2; and the form-side a z-index: 1;.

为了使banner-side悬停在form-side ,我给banner-side一个z-index: 2; 以及form-sidez-index: 1;

Finally, I give them each a transition: 2s;. When I change their right properties, they will move fluidly from their starting place to the next for the duration of two seconds.

最后,我给他们一个transition: 2s; 。 当我更改其right属性时,它们将在两秒钟的时间内从起始位置流畅地移动到下一个位置。

Triggering Transitions by Updating Classes

通过更新类触发转换

The positioning of banner-side and form-side will be based on whether I am rendering a sign-in or sign-up form.

banner-sideform-side将取决于我是呈现登录表单还是注册表单。

I set up two new classes for where the banner-side and form-side will be when they are reversed.

我设置了两个新的类,分别用于反转banner-sideform-side

Image for post

I apply these new classes with functions.

我将这些新类与函数一起应用。

Image for post
Image for post
Image for post

My useState hook contains a boolean called welcome. When welcome is updated, the component will re-render and the classes will be updated based based on that boolean.

我的useState挂钩包含一个名为welcome的布尔值。 更新welcome ,该组件将重新呈现,并且基于该布尔值将更新类。

Finally, I put up a button in the banner to trigger setWelcome() in the useState hook.

最后,我在横幅中放置一个按钮以触发useState挂钩中的useState setWelcome()

Image for post
Image for post

有条件地渲染登录/注册 (Conditionally Render Sign-In / Sign-Up)

The final touch is to change the forms and and banner text based on whether our user intends to sign-in or create a new account. To do this, I used the same welcome from my useState hook.

最后一步是根据我们的用户是否打算登录或创建新帐户来更改表单和标题文本。 为此,我使用了useState挂钩中的相同welcome useState

Image for post

Because this is a dummy project, I didn’t do anything specific with the forms themselves. If you’d like, you can check them out on GitHub.

因为这是一个虚拟项目,所以我对表单本身没有做任何特定的事情。 如果愿意,可以在GitHub上检出它们。

结论 (Conclusion)

This was a fun one to figure out. The newest concept for me was a deeper understanding of position and z-index.

这是一个有趣的发现。 对我来说,最新的概念是对positionz-index的更深入的了解。

I’ve used this properties before, but I had been flying by the seat of my pants. I’m happy for my newfound confidence with them.

我以前曾使用过此属性,但我一直在裤子旁边放飞。 我为我对他们的新信心感到高兴。

Taking this project further, I would also animate the rendering of the text and forms themselves. A smooth transition would improve the page.

进一步推进该项目,我还将为文本和表单本身的动画制作动画。 平稳过渡将改善页面。

As always, thanks for reading. I hope this helped you somehow.

与往常一样,感谢您的阅读。 希望这对您有所帮助。

翻译自: https://medium.com/swlh/build-this-cool-sign-in-form-with-react-and-css-f9339d14d140

react 使用css文件


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

相关文章

angular桌面应用开发_使用angular构建高性能的本机和跨平台桌面应用程序

angular桌面应用开发As you may already know, Angular is used on many platforms.您可能已经知道,Angular在许多平台上使用。 Ionic (https://ionicframework.com/) 离子( https://ionicframework.com/ ) NativeScript (https://www.nativescript.org/) NativeSc…

远程实习面试_充分利用您的远程实习

远程实习面试This year has been a year of relinquishing any idea of control. If you had told me back in December (in the before time) that the internship offer I was ecstatic to receive would not be completed in Amazon’s Austin offices, but virtually durin…

机器学习 归一化 标准化_机器学习中的标准化

机器学习 归一化 标准化Normalization is a technique often applied as part of data preparation for machine learning. The goal of normalization is to change the values of numeric columns in the dataset to use a common scale, without distorting differences in …

复选框隐藏复选框_复选框软件工程师

复选框隐藏复选框背景 (Background) TL, DR.TL,DR First and foremost, I honestly don’t consider myself a software engineer yet a person who learned how to write computer programs while in high school.首先,老实说,我不认为自己…

iaas vs paas vs saas

If your ecommerce business is progressing to the Cloud, you need to be familiar with these three main types of cloud computing:如果您的电子商务业务正在向云计算发展,则需要熟悉以下三种主要的云计算类型: IaaS — Infrastructure as a Servi…

如何成为一名优秀的工程师_成为一名优秀软件工程师的七件事

如何成为一名优秀的工程师Multiple times many of you might have wondered what makes someone a good software engineer. In most cases, there’s a tendency to think that having an intelligence way above the average is the only way to be a good software engineer…

判断控件_时间选择控件在不同场景下的应用

点击上方蓝字关注我文 | 4240字预计阅读 | 10 分钟时间选择控件在产品设计中是一个非常常见且通用的元素,在各种产品中几乎都有应用。随着各种App和网站类产品在不同操作场景中的渗透发展,我们在进行产品设计时也应更追求功能实现方式的“恰如其分”。本…

jupyter 嵌入_嵌入Jupyter笔记本

jupyter 嵌入We’ve recently started building a new service for sharing data between funders. As much of this data is highly restricted, all analysis must occur within our platform. Users will not be able to download data to their local machine. We’ve ther…