在现代Web开发中,登录页面是用户与系统交互的第一道门槛。一个美观且功能完善的登录页面不仅能提升用户体验,还能增强用户对系统的信任感。本文将详细介绍如何在一个基于Vue.js和Element UI的登录页面中添加背景图片,并对现有代码进行优化,使其更加美观和实用。
登录页面是用户进入系统的入口,其设计直接影响用户的第一印象。一个优秀的登录页面应具备以下特点:
在本文中,我们将基于一个现有的Vue.js登录页面,添加背景图片,并优化其样式和布局。
本文使用的技术栈和工具包括:
首先,我们需要在登录页面的容器中添加背景图片。以下是具体步骤:
在Vue组件的模板部分,找到 .login-container 容器,并确保其覆盖整个页面:
<template>
<div class="login-container">
<!-- 登录表单内容 -->
</div>
</template>在样式部分,为 .login-container 添加背景图片,并设置其覆盖整个页面:
.login-container {
min-height: 100vh; // 使用视口高度
width: 100%;
background-image: url('~@/assets/login-bg.jpg'); // 替换为你的背景图片路径
background-size: cover; // 背景图片覆盖整个容器
background-position: center; // 背景图片居中
display: flex;
justify-content: center;
align-items: center;
}为了让表单在背景图片上更加清晰,我们需要调整表单的背景色、边框和阴影等样式。
为 .login-form 添加半透明的背景色和圆角边框:
.login-form {
width: 520px;
padding: 40px;
background-color: rgba(45, 58, 75, 0.8); // 半透明背景色
border-radius: 8px; // 圆角边框
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); // 阴影效果
}调整输入框的背景色和光标颜色,确保在深色背景下输入内容清晰可见:
.login-container .el-input input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: #fff; // 输入文字颜色
height: 47px;
caret-color: #fff; // 光标颜色
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px rgba(45, 58, 75, 0.8) inset !important;
-webkit-text-fill-color: #fff !important;
}
}为了让页面更加友好,我们需要调整布局和交互细节。
使用Flexbox布局将表单居中显示:
.login-container {
display: flex;
justify-content: center;
align-items: center;
}调整登录按钮的样式,使其更加醒目:
.el-button--primary {
width: 100%;
margin-bottom: 30px;
background-color: #409EFF; // 按钮背景色
border-color: #409EFF; // 按钮边框色
}以下是优化后的完整代码:
<template>
<div class="login-container">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
<div class="title-container">
<h3 class="title">就业信息管理系统</h3>
</div>
<el-form-item prop="username">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input
ref="username"
v-model="loginForm.username"
placeholder="Username"
name="username"
type="text"
tabindex="1"
auto-complete="on"
/>
</el-form-item>
<el-form-item prop="password">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
placeholder="Password"
name="password"
tabindex="2"
auto-complete="on"
@keyup.enter.native="handleLogin"
/>
<span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
</span>
</el-form-item>
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
</el-form>
</div>
</template>
<script>
import { validUsername } from '@/utils/validate'
export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error('请输入正确的用户名'))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('密码不能少于 6 位'))
} else {
callback()
}
}
return {
loginForm: {
username: 'admin',
password: '111111'
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
loading: false,
passwordType: 'password',
redirect: undefined
}
},
watch: {
$route: {
handler: function(route) {
this.redirect = route.query && route.query.redirect
},
immediate: true
}
},
methods: {
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = ''
} else {
this.passwordType = 'password'
}
this.$nextTick(() => {
this.$refs.password.focus()
})
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm).then(() => {
this.$router.push({ path: this.redirect || '/' })
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
}
}
}
</script>
<style lang="scss">
$bg: rgba(45, 58, 75, 0.8);
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $cursor;
}
}
.login-container {
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
}
}
</style>
<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
min-height: 100vh;
width: 100%;
background-image: url('~@/assets/login-bg.jpg');
background-size: cover;
background-position: center;
display: flex;
justify-content: center;
align-items: center;
.login-form {
width: 520px;
padding: 40px;
background-color: $bg;
border-radius: 8px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 26px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
}
</style>通过本文的优化,我们成功地为登录页面添加了背景图片,并优化了表单的样式和布局。这些改进不仅提升了页面的美观度,还增强了用户的交互体验。
未来,我们可以进一步优化以下方面:
希望本文对你有所帮助!如果有任何问题或建议,欢迎留言讨论。