溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用css實現微信小程序簡潔登錄頁面

發布時間:2021-12-06 10:12:11 來源:億速云 閱讀:297 作者:iii 欄目:開發技術
# 怎么用CSS實現微信小程序簡潔登錄頁面

## 前言

在移動互聯網時代,登錄頁面作為用戶與產品交互的第一道門戶,其設計直接影響用戶體驗和轉化率。微信小程序憑借其輕量級、即用即走的特性,已成為眾多企業和開發者的首選平臺。本文將詳細介紹如何使用CSS打造一個符合微信小程序設計規范的簡潔登錄頁面,涵蓋布局結構、樣式設計、交互優化等核心知識點。

## 一、微信小程序登錄頁面設計原則

### 1.1 簡潔性原則
微信官方設計指南強調"簡潔至上",建議:
- 去除冗余信息,保留核心功能
- 使用留白創造呼吸感
- 限制色彩數量(通常不超過3種)

### 1.2 品牌一致性
- 保持與主品牌一致的色調
- 使用標準化的圖標和按鈕樣式
- 遵循微信的視覺層次規范

### 1.3 移動優先設計
- 適配不同屏幕尺寸(重點考慮375px-414px寬度)
- 觸控區域不小于44×44pt
- 優化鍵盤彈出時的布局

## 二、HTML結構搭建

### 2.1 基礎WXML結構
```html
<view class="login-container">
  <view class="header">
    <image class="logo" src="/images/logo.png"></image>
    <text class="title">歡迎回來</text>
  </view>
  
  <form class="form">
    <view class="input-group">
      <input 
        type="text" 
        placeholder="請輸入手機號" 
        placeholder-class="placeholder"
        class="input"
      />
      <view class="clear-btn" wx:if="{{phoneNumber}}">×</view>
    </view>
    
    <view class="input-group">
      <input 
        type="password" 
        placeholder="請輸入密碼" 
        placeholder-class="placeholder"
        class="input"
      />
      <view class="toggle-pwd" bindtap="togglePassword">
        <image src="{{showPassword ? '/images/eye-open.png' : '/images/eye-close.png'}}"></image>
      </view>
    </view>
    
    <button class="login-btn" formType="submit">登錄</button>
  </form>
  
  <view class="footer">
    <text class="link">忘記密碼</text>
    <text class="divider">|</text>
    <text class="link">快速注冊</text>
  </view>
</view>

2.2 結構解析

  • header區域:展示品牌標識和歡迎語
  • form區域:包含輸入框組和提交按鈕
  • footer區域:提供輔助操作入口
  • 使用view替代div,符合小程序組件規范

三、CSS樣式實現

3.1 基礎樣式重置

/* app.wxss */
page {
  background-color: #f7f7f7;
  font-family: -apple-system, BlinkMacSystemFont, 
    'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 
    'Open Sans', 'Helvetica Neue', sans-serif;
  line-height: 1.6;
  color: #333;
}

button::after {
  border: none;
}

.placeholder {
  color: #ccc;
}

3.2 容器布局

.login-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 40rpx;
  box-sizing: border-box;
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 80rpx 0 120rpx;
}

.logo {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 30rpx;
  border-radius: 24rpx;
}

.title {
  font-size: 36rpx;
  font-weight: 500;
  color: #333;
}

3.3 表單樣式設計

.form {
  width: 100%;
}

.input-group {
  position: relative;
  margin-bottom: 40rpx;
  background: #fff;
  border-radius: 8rpx;
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
}

.input {
  width: 100%;
  height: 100rpx;
  padding: 0 30rpx;
  font-size: 32rpx;
  box-sizing: border-box;
  border: 1rpx solid #eee;
  border-radius: 8rpx;
  transition: border-color 0.3s;
}

.input:focus {
  border-color: #07C160;
}

.clear-btn, .toggle-pwd {
  position: absolute;
  right: 20rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 36rpx;
}

.toggle-pwd image {
  width: 40rpx;
  height: 40rpx;
}

3.4 按鈕樣式優化

.login-btn {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  background: linear-gradient(90deg, #07C160, #09D668);
  color: white;
  font-size: 34rpx;
  border-radius: 45rpx;
  margin-top: 60rpx;
  transition: opacity 0.3s;
}

.login-btn:active {
  opacity: 0.8;
}

.login-btn[disabled] {
  background: #ccc;
  color: #fff;
}

3.5 頁腳樣式

.footer {
  display: flex;
  justify-content: center;
  margin-top: 80rpx;
}

.link {
  color: #07C160;
  font-size: 28rpx;
  padding: 0 20rpx;
}

.divider {
  color: #ddd;
}

四、交互效果增強

4.1 輸入框動態效果

/* 添加輸入動畫 */
@keyframes inputFocus {
  from { transform: scale(1); box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05); }
  to { transform: scale(1.01); box-shadow: 0 6rpx 12rpx rgba(7, 193, 96, 0.1); }
}

.input:focus {
  animation: inputFocus 0.3s forwards;
}

/* 清除按鈕過渡 */
.clear-btn {
  opacity: 0;
  transition: opacity 0.3s;
}

.input-group.active .clear-btn {
  opacity: 1;
}

4.2 按鈕加載狀態

.loading-btn {
  position: relative;
  color: transparent !important;
}

.loading-btn::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 30rpx;
  height: 30rpx;
  margin: -15rpx 0 0 -15rpx;
  border: 4rpx solid rgba(255,255,255,0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

五、響應式適配技巧

5.1 使用rpx單位

  • 微信小程序特有的響應式單位
  • 1rpx = 屏幕寬度/750
  • 確保在不同設備上等比縮放

5.2 媒體查詢示例

@media screen and (min-width: 600px) {
  .login-container {
    max-width: 500px;
    margin: 0 auto;
  }
  
  .input {
    font-size: 16px;
  }
}

5.3 鍵盤彈出處理

// page.js
Page({
  data: {
    viewHeight: 0
  },
  onLoad() {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({ viewHeight: res.windowHeight })
      }
    })
  }
})
/* 當鍵盤彈出時調整布局 */
.keyboard-up .login-container {
  padding-top: 20rpx;
}

.keyboard-up .header {
  margin: 0 0 60rpx;
}

六、完整實現代碼

6.1 WXML完整代碼

<view class="login-container {{keyboardHeight > 0 ? 'keyboard-up' : ''}}">
  <!-- 頭部區域 -->
  <view class="header">
    <image class="logo" src="/images/logo.png" mode="aspectFit"></image>
    <text class="title">歡迎回來</text>
  </view>
  
  <!-- 表單區域 -->
  <form bindsubmit="formSubmit" class="form">
    <view class="input-group {{phoneNumber ? 'active' : ''}}">
      <input 
        name="phone"
        type="number" 
        placeholder="請輸入手機號" 
        placeholder-class="placeholder"
        class="input"
        bindinput="handlePhoneInput"
        value="{{phoneNumber}}"
      />
      <view class="clear-btn" bindtap="clearPhone">×</view>
    </view>
    
    <view class="input-group">
      <input 
        name="password"
        type="{{showPassword ? 'text' : 'password'}}" 
        placeholder="請輸入密碼" 
        placeholder-class="placeholder"
        class="input"
        bindinput="handlePwdInput"
      />
      <view class="toggle-pwd" bindtap="togglePassword">
        <image src="{{showPassword ? '/images/eye-open.png' : '/images/eye-close.png'}}"></image>
      </view>
    </view>
    
    <button 
      class="login-btn {{isLoading ? 'loading-btn' : ''}}" 
      formType="submit"
      disabled="{{!phoneNumber || !password || isLoading}}"
    >
      {{isLoading ? '' : '登錄'}}
    </button>
  </form>
  
  <!-- 頁腳鏈接 -->
  <view class="footer">
    <text class="link" bindtap="navigateToForget">忘記密碼</text>
    <text class="divider">|</text>
    <text class="link" bindtap="navigateToRegister">快速注冊</text>
  </view>
</view>

6.2 WXSS完整代碼

/* pages/login/login.wxss */
.login-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 40rpx;
  box-sizing: border-box;
  transition: all 0.3s ease;
}

/* 頭部樣式 */
.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 80rpx 0 120rpx;
  transition: margin 0.3s ease;
}

.logo {
  width: 150rpx;
  height: 150rpx;
  margin-bottom: 40rpx;
  border-radius: 24rpx;
  box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 38rpx;
  font-weight: 500;
  color: #333;
  letter-spacing: 1rpx;
}

/* 表單樣式 */
.form {
  width: 100%;
}

.input-group {
  position: relative;
  margin-bottom: 40rpx;
  background: #fff;
  border-radius: 12rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.input {
  width: 100%;
  height: 100rpx;
  padding: 0 30rpx;
  font-size: 32rpx;
  box-sizing: border-box;
  border: 1rpx solid #f0f0f0;
  border-radius: 12rpx;
  transition: all 0.3s ease;
}

.input:focus {
  border-color: #07C160;
  animation: inputFocus 0.3s forwards;
}

/* 功能按鈕 */
.clear-btn, .toggle-pwd {
  position: absolute;
  right: 20rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 44rpx;
  height: 44rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 36rpx;
  border-radius: 50%;
}

.clear-btn {
  background: #f0f0f0;
  opacity: 0;
  transition: opacity 0.3s;
}

.input-group.active .clear-btn {
  opacity: 1;
}

.toggle-pwd image {
  width: 36rpx;
  height: 36rpx;
}

/* 登錄按鈕 */
.login-btn {
  width: 100%;
  height: 96rpx;
  line-height: 96rpx;
  background: linear-gradient(135deg, #07C160, #09D668);
  color: white;
  font-size: 34rpx;
  font-weight: 500;
  border-radius: 48rpx;
  margin-top: 80rpx;
  transition: all 0.3s ease;
  box-shadow: 0 8rpx 24rpx rgba(7, 193, 96, 0.3);
}

.login-btn:active {
  opacity: 0.9;
  transform: translateY(2rpx);
  box-shadow: 0 4rpx 12rpx rgba(7, 193, 96, 0.3);
}

.login-btn[disabled] {
  background: #ccc;
  color: #fff;
  box-shadow: none;
  transform: none;
}

/* 加載動畫 */
.loading-btn::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 36rpx;
  height: 36rpx;
  margin: -18rpx 0 0 -18rpx;
  border: 4rpx solid rgba(255,255,255,0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s linear infinite;
}

/* 頁腳鏈接 */
.footer {
  display: flex;
  justify-content: center;
  margin-top: auto;
  padding-bottom: 40rpx;
}

.link {
  color: #07C160;
  font-size: 28rpx;
  padding: 0 24rpx;
  font-weight: 500;
}

.divider {
  color: #e0e0e0;
}

/* 動畫效果 */
@keyframes inputFocus {
  from { transform: scale(1); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05); }
  to { transform: scale(1.01); box-shadow: 0 8rpx 24rpx rgba(7, 193, 96, 0.15); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 鍵盤彈出狀態 */
.keyboard-up .login-container {
  padding-top: 20rpx;
}

.keyboard-up .header {
  margin: 0 0 60rpx;
  opacity: 0.8;
  transform: scale(0.95);
}

6.3 JS邏輯代碼

// pages/login/login.js
Page({
  data: {
    phoneNumber: '',
    password: '',
    showPassword: false,
    isLoading: false,
    keyboardHeight: 0
  },

  onLoad() {
    this.getWindowHeight()
    this.setupKeyboardListener()
  },

  getWindowHeight() {
    wx.getSystemInfo({
      success: (res) => {
        this.windowHeight = res.windowHeight
      }
    })
  },

  setupKeyboardListener() {
    wx.onKeyboardHeightChange(res => {
      this.setData({
        keyboardHeight: res.height
      })
    })
  },

  handlePhoneInput(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },

  handlePwdInput(e) {
    this.setData({
      password: e.detail.value
    })
  },

  clearPhone() {
    this.setData({
      phoneNumber: ''
    })
  },

  togglePassword() {
    this.setData({
      showPassword: !this.data.showPassword
    })
  },

  formSubmit(e) {
    if (this.data.isLoading) return
    
    const { phone, password } = e.detail.value
    
    if (!phone || !password) {
      wx.showToast({
        title: '請填寫完整信息',
        icon: 'none'
      })
      return
    }
    
    this.setData({ isLoading: true })
    
    // 模擬登錄請求
    setTimeout(() => {
      this.setData({ isLoading: false })
      wx.showToast({
        title: '登錄成功',
        icon: 'success'
      })
      // 跳轉到首頁
      wx.switchTab({
        url: '/pages/index/index'
      })
    }, 1500)
  },

  navigateToForget() {
    wx.navigateTo({
      url: '/pages/forget-password/forget-password'
    })
  },

  navigateToRegister() {
    wx.navigateTo({
      url: '/pages/register/register'
    })
  }
})

七、性能優化建議

7.1 樣式優化技巧

  1. 避免過度嵌套選擇器: “`css /* 不推薦 */ .login-container .form .input-group .input {}

/* 推薦 */ .input {} “`

  1. 使用CSS變量管理主題色: “`css page { –primary-color: #07C160; –primary-gradient: linear-gradient(135deg, #07C160, #09D668);
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

css
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女