You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React简单组件无法实现登录/注册标签切换动画问题求助

解决React登录/注册标签切换动画失效问题

你的问题主要出在三个核心点上,对应解决方法如下:

1. 单选框状态未与React状态绑定

原生radio的选中状态需要由React的useState统一管理,否则状态更新会滞后,导致CSS无法正确触发动画。修改你的组件代码,添加状态控制逻辑:

import React, { useState } from 'react';
import './Login.css';

function Login() {
  // 初始化选中的标签,默认选中Sign In
  const [activeTab, setActiveTab] = useState('tab-1');

  const handleTabChange = (e) => {
    setActiveTab(e.target.id);
  };

  return (
    <div className="login-wrap">
      <div className="login-html">
        <input 
          id="tab-1" 
          type="radio" 
          name="tab" 
          className="sign-in"
          checked={activeTab === 'tab-1'}
          onChange={handleTabChange}
        />
        <label htmlFor="tab-1" className="tab">Sign In</label>
        
        <input 
          id="tab-2" 
          type="radio" 
          name="tab" 
          className="sign-up"
          checked={activeTab === 'tab-2'}
          onChange={handleTabChange}
        />
        <label htmlFor="tab-2" className="tab">Sign Up</label>

        {/* 补充登录/注册表单内容,这是动画的载体 */}
        <div className="login-form">
          <div className="sign-in-htm">
            <div className="group">
              <label htmlFor="user" className="label">Username</label>
              <input id="user" type="text" className="input"/>
            </div>
            <div className="group">
              <label htmlFor="pass" className="label">Password</label>
              <input id="pass" type="password" className="input" data-type="password"/>
            </div>
          </div>
          <div className="sign-up-htm">
            <div className="group">
              <label htmlFor="user" className="label">Username</label>
              <input id="user" type="text" className="input"/>
            </div>
            <div className="group">
              <label htmlFor="pass" className="label">Password</label>
              <input id="pass" type="password" className="input" data-type="password"/>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default Login;

2. 补充关键CSS动画样式

Codepen中的切换动画完全依赖CSS实现,你需要把以下核心样式复制到Login.css中:

.login-wrap {
  width: 100%;
  max-width: 525px;
  margin: 30px auto;
  position: relative;
}

.login-html {
  width: 100%;
  height: 100%;
  position: absolute;
  padding: 90px 70px 50px 70px;
  background: rgba(40,57,101,.9);
}

.login-html .tab {
  font-size: 22px;
  margin-right: 15px;
  padding-bottom: 5px;
  margin: 0 15px 10px 0;
  display: inline-block;
  border-bottom: 2px solid transparent;
  color: #fff;
  cursor: pointer;
  transition: border-color .3s ease;
}

.login-html .sign-in:checked + .tab,
.login-html .sign-up:checked + .tab {
  color: #fff;
  border-color: #1161ee;
}

.login-form {
  min-height: 345px;
  position: relative;
  perspective: 1000px;
  transform-style: preserve-3d;
}

.login-form .group {
  margin-bottom: 15px;
}

.login-form .group .label,
.login-form .group .input {
  width: 100%;
  color: #fff;
  display: block;
}

.login-form .group .input {
  border: none;
  padding: 15px 20px;
  border-radius: 25px;
  background: rgba(255,255,255,.1);
}

.login-form .group .label {
  color: #aaa;
  font-size: 12px;
}

.login-form .sign-in-htm,
.login-form .sign-up-htm {
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  position: absolute;
  transform: rotateY(180deg);
  backface-visibility: hidden;
  transition: all .4s linear;
}

.login-html .sign-in:checked + .tab + .sign-up + .tab + .login-form .sign-in-htm {
  transform: rotate(0);
}

.login-html .sign-up:checked + .tab + .login-form .sign-up-htm {
  transform: rotate(0);
}

3. 确保结构完整性

原示例的动画不仅包含标签切换,还涉及表单内容的3D翻转效果,必须保留.sign-in-htm和.sign-up-htm这两个内容容器,否则动画没有可作用的载体。

按以上步骤修改后,就能实现和原示例一致的标签切换动画效果。

内容的提问来源于stack exchange,提问作者eemamedo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:07:25