微信小程序--动态切换登录注册标签页

1、try.js的

1.1、data函数

添加

复制代码
activeTab: 'login', // 当前激活的标签,默认为登录

1.2、添加一个函数

复制代码
// 切换登录/注册标签
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },

2、try.wxml的代码

复制代码
<!--pages/try/try.wxml-->
<view class="login-container">
  <view class="header">
    <text class="logo">DataDevices</text>
    <text class="slogan">智能设备管理平台</text>
  </view>

  <view class="form-container">
    <view class="tab-container">
      <text class="tab-item {{activeTab === 'login' ? 'active' : ''}}" bindtap="switchTab" data-tab="login">登录</text>
      <text class="tab-item {{activeTab === 'register' ? 'active' : ''}}" bindtap="switchTab" data-tab="register">注册</text>
    </view>
  </view>
  
  <!-- 登录表单 -->
  <view wx:if="{{activeTab === 'login'}}" class="form">
  <view>登录表单</view>
  </view>

  <!-- 注册表单 -->
  <view wx:if="{{activeTab === 'register'}}" class="form">
  <view>注册表单</view>
  </view>

</view>

3、try.wxss

复制代码
/* pages/try/try.wxss */
.login-container {
  padding: 40rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.header {
  text-align: center;
  margin-top: 100rpx;
  margin-bottom: 60rpx;
}
.logo {
  font-size: 48rpx;
  font-weight: bold;
  color: #1890ff;
  display: block;
  margin-bottom: 10rpx;
}
.slogan {
  font-size: 24rpx;
  color: #666;
}
.form-container {
  background-color: white;
  border-radius: 12rpx;
  padding: 40rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  margin-bottom: 40rpx;
}
.tab-container {
  display: flex;
  border-bottom: 1rpx solid #e5e5e5;
  margin-bottom: 40rpx;
}
.tab-item {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  padding: 20rpx 0;
  color: #666;
  position: relative;
}
.tab-item.active {
  color: #1890ff;
  font-weight: bold;
}
/* 给"当前选中的 tab"底部加一条蓝色横线 */
.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 25%;
  width: 50%;
  height: 4rpx;
  background-color: #1890ff;
}

欢迎欣赏。

相关推荐
小羊Yveesss1 分钟前
2026年微信小程序搭建一般需要多长时间?不同方案周期和延期点
微信小程序·小程序
禾高网络3 小时前
互联网医院|AI 互联网医院成品开发系统
java·大数据·人工智能·小程序
小羊Yveesss4 小时前
2026年微信小程序搭建算什么费用?年费、设计费、开发费和维护费
微信小程序·小程序
2501_9159184110 小时前
PerfDog 太贵?iOS 性能监控工具的功能对比与选择
android·ios·小程序·https·uni-app·iphone·webview
码农客栈20 小时前
小程序学习(二十六)之“订单详情”
小程序
weixin_440058311 天前
2026知识付费小程序:年费全包零抽成,录播题库全功能覆盖
小程序·apache·小程序开发
小羊Yveesss1 天前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾1 天前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
HackTwoHub1 天前
ARL灯塔重构版:支持APP/小程序/WEB资产同步扫描
人工智能·安全·web安全·网络安全·小程序·重构·自动化
投票竞赛1 天前
避雷按单场活动扣费平台,2026 在线投票测评不限频次新建投票评选活动
小程序