vue2和elementUI?實現(xiàn)落日余暉登錄頁和滑塊校驗功能

前言
標題很夸張,實則是AI的功能,今天咱也搞一個登錄頁,其實滿簡單的一個東東,大家也都會用到,本次僅限前端,沒有任何后臺交互,技術vue、vue-router、element-ui,因為背景圖是落日,所以就叫它落日余暉登錄頁吧

1 項目搭建
使用指令直接構建的,選擇vue2版本
vue create login-admin
構建后的項目,刪掉了原始的helloworld組件,最終目標結構如下:

2 依賴引入
npm install element-ui npm install vue-router@3
由于項目是基于vue2的,故vue-router不能使用4.x版本,后面會有問題,在文末說了。

3 項目調整
項目構建成功后,刪掉最初的helloworld組件
①vue-router
新建router/index.js文件,將我們要寫的登錄頁路徑放進去
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Login',
component: () => import('@/views/login.vue'),
}
]
const router = new VueRouter({
routes
})
export default router;② App.vue
移除掉老的App.vue中的全部內容,然后我寫一個簡單的router-view,讓他來展示我們的login頁面
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
</script>
<style>
body {
margin: 0px;
}
</style>這里面的body,由于下面有小坑,所以先給margin清空了
③ main.js
簡單調整,將我們寫的router引進來,以及element-ui導入進來
import Vue from 'vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
Vue.use(ElementUI);
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')4 寫登錄頁
新建頁面views/login.vue這就是我們的核心頁面,需要跟上面router中寫的路徑保持一致,太長了,我就簡單復制一下
<template>
<div class="background">
<el-form
:rules="rules"
ref="loginForm"
:model="loginForm"
class="loginContainer"
>
<h3 class="loginTitle">系統(tǒng)登錄</h3>
<el-form-item prop="username">
<el-input
type="text"
prefix-icon="el-icon-user"
v-model="loginForm.username"
placeholder="請輸入用戶名"
>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
type="password"
prefix-icon="el-icon-link"
v-model="loginForm.password"
placeholder="請輸入密碼"
>
</el-input>
</el-form-item>
<el-form-item>
<SilderVerify ref="verify"></SilderVerify>
</el-form-item>
<el-checkbox v-model="checked" class="loginRemember">記住我</el-checkbox>
<el-button type="primary" style="width: 100%" @click="submitLogin"
>登錄</el-button
>
</el-form>
</div>
</template>然后,換上我落日余暉的背景,逼格一下就上來了
.background {
position: absolute;
background-image: url("../assets/bg.jpg");
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
height: 100vh;
width: 100%;
}5 寫滑塊校驗
這里直接給他封裝成組件了,來自chatgpt的大力支持,新建文件 components/SilderVerify/index.vue,代碼搞進去,太長了,我就簡單復制一下
<template>
<div class="drag" ref="dragDiv">
<div class="drag_bg"></div>
<div class="drag_text">{{ confirmWords }}</div>
<div
ref="moveDiv"
@mousedown="mouseDownFn($event)"
:class="{ handler_ok_bg: confirmSuccess }"
class="handler handler_bg"
style="position: absolute; top: 0px; left: 0px"
></div>
</div>
</template>
<script>
export default {
name: "SilderVerify",
data() {
return {
beginClientX: 0 /*距離屏幕左端距離*/,
mouseMoveState: false /*觸發(fā)拖動狀態(tài) 判斷*/,
maxWidth: "" /*拖動最大寬度,依據滑塊寬度算出來的*/,
confirmWords: "向右拖動滑塊驗證" /*滑塊文字*/,
confirmSuccess: false /*驗證成功判斷*/,
};
},
methods: {
//mousedown 事件
mouseDownFn: function (e) {
console.log('mouseDownFn' + e.clientX)
if (!this.confirmSuccess) {
e.preventDefault && e.preventDefault(); //阻止文字選中等 瀏覽器默認事件
this.mouseMoveState = true;
this.beginClientX = e.clientX;
}
},
//驗證成功函數
successFunction() {
this.confirmSuccess = true;
this.confirmWords = "驗證通過";
if (window.addEventListener) {
document
.getElementsByTagName("html")[0]
.removeEventListener("mousemove", this.mouseMoveFn);
document
.getElementsByTagName("html")[0]
.removeEventListener("mouseup", this.moseUpFn);
} else {
document
.getElementsByTagName("html")[0]
.removeEventListener("mouseup", () => {});
}
document.getElementsByClassName("drag_text")[0].style.color = "#fff";
document.getElementsByClassName("handler")[0].style.left =
this.maxWidth + "px";
document.getElementsByClassName("drag_bg")[0].style.width =
this.maxWidth + "px";
},
//mousemove事件
mouseMoveFn(e) {
if (this.mouseMoveState) {
let width = e.clientX - this.beginClientX;
if (width > 0 && width <= this.maxWidth) {
document.getElementsByClassName("handler")[0].style.left =
width + "px";
document.getElementsByClassName("drag_bg")[0].style.width =
width + "px";
} else if (width > this.maxWidth) {
this.successFunction();
}
}
},
//mouseup事件
moseUpFn(e) {
console.log('moseUpFn' + e.clientX)
this.mouseMoveState = false;
var width = e.clientX - this.beginClientX;
if (width < this.maxWidth) {
document.getElementsByClassName("handler")[0].style.left = 0 + "px";
document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
}
},
},
mounted() {
this.maxWidth =
this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
document
.getElementsByTagName("html")[0]
.addEventListener("mousemove", this.mouseMoveFn);
document
.getElementsByTagName("html")[0]
.addEventListener("mouseup", this.moseUpFn);
},
};
</script>6 源碼下載
https://dxz.jb51.net/202306/yuanma/login-demo_jb51.rar
7 問題解決
①項目一直報錯
解決:由于安裝 vue-router 時,直接運行了 npm install vue-router 命令,造成直接下載最新版 vue-router 4.x,而 4 以后的版本適用于 vue3.0 版本,用在 vue2.0+ 會報錯,故換版本

② 背景圖存在白邊
可以看見,左右都有白邊,采用了最粗暴的方法,給body的樣式
margin:0px可以解決,上面也寫到了

到此這篇關于vue2和elementUI 實現(xiàn)落日余暉登錄頁和滑塊校驗功能的文章就介紹到這了,更多相關vue2和elementUI 登錄頁和滑塊校驗內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Element el-checkbox-group v-model不支持對象(object)解決方案
本文主要介紹了Element el-checkbox-group v-model不支持對象(object)解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
VUE3+mqtt封裝解決多頁面使用需重復連接等問題(附實例)
最近了解到mqtt這樣一個協(xié)議,可以在web上達到即時通訊的效果,下面這篇文章主要給大家介紹了關于VUE3+mqtt封裝解決多頁面使用需重復連接等問題的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
vue addRoutes實現(xiàn)動態(tài)權限路由菜單的示例
本篇文章主要介紹了vue addRoutes實現(xiàn)動態(tài)權限路由菜單的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的方法
最近項目中需要用到日期選擇器,所以這里給大家總結下,這篇文章主要給大家介紹了關于element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的相關資料,需要的朋友可以參考下2023-09-09

