# 怎么用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>
view替代div,符合小程序組件規范/* 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;
}
.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;
}
.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;
}
.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;
}
.footer {
display: flex;
justify-content: center;
margin-top: 80rpx;
}
.link {
color: #07C160;
font-size: 28rpx;
padding: 0 20rpx;
}
.divider {
color: #ddd;
}
/* 添加輸入動畫 */
@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;
}
.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); }
}
@media screen and (min-width: 600px) {
.login-container {
max-width: 500px;
margin: 0 auto;
}
.input {
font-size: 16px;
}
}
// 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;
}
<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>
/* 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);
}
// 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'
})
}
})
/* 推薦 */ .input {} “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。