vue2和elementUI?實(shí)現(xiàn)落日余暉登錄頁和滑塊校驗(yàn)功能
前言
標(biāo)題很夸張,實(shí)則是AI的功能,今天咱也搞一個(gè)登錄頁,其實(shí)滿簡單的一個(gè)東東,大家也都會用到,本次僅限前端,沒有任何后臺交互,技術(shù)vue
、vue-router
、element-ui
,因?yàn)楸尘皥D是落日,所以就叫它落日余暉登錄頁
吧
1 項(xiàng)目搭建
使用指令直接構(gòu)建的,選擇vue2版本
vue create login-admin
構(gòu)建后的項(xiàng)目,刪掉了原始的helloworld組件,最終目標(biāo)結(jié)構(gòu)如下:
2 依賴引入
npm install element-ui npm install vue-router@3
由于項(xiàng)目是基于vue2的,故vue-router不能使用4.x版本,后面會有問題,在文末說了。
3 項(xiàng)目調(diào)整
項(xiàng)目構(gòu)建成功后,刪掉最初的helloworld組件
①vue-router
新建router/index.js
文件,將我們要寫的登錄頁路徑放進(jìn)去
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
中的全部內(nèi)容,然后我寫一個(gè)簡單的router-view
,讓他來展示我們的login頁面
<template> <div id="app"> <router-view></router-view> </div> </template> <script> </script> <style> body { margin: 0px; } </style>
這里面的body,由于下面有小坑,所以先給margin清空了
③ main.js
簡單調(diào)整,將我們寫的router引進(jìn)來,以及element-ui導(dǎo)入進(jìn)來
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中寫的路徑保持一致,太長了,我就簡單復(fù)制一下
<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 寫滑塊校驗(yàn)
這里直接給他封裝成組件了,來自chatgpt的大力支持,新建文件 components/SilderVerify/index.vue
,代碼搞進(jìn)去,太長了,我就簡單復(fù)制一下
<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: "" /*拖動最大寬度,依據(jù)滑塊寬度算出來的*/, confirmWords: "向右拖動滑塊驗(yàn)證" /*滑塊文字*/, confirmSuccess: false /*驗(yàn)證成功判斷*/, }; }, methods: { //mousedown 事件 mouseDownFn: function (e) { console.log('mouseDownFn' + e.clientX) if (!this.confirmSuccess) { e.preventDefault && e.preventDefault(); //阻止文字選中等 瀏覽器默認(rèn)事件 this.mouseMoveState = true; this.beginClientX = e.clientX; } }, //驗(yàn)證成功函數(shù) successFunction() { this.confirmSuccess = true; this.confirmWords = "驗(yàn)證通過"; 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 問題解決
①項(xiàng)目一直報(bào)錯
解決:由于安裝 vue-router 時(shí),直接運(yùn)行了 npm install vue-router 命令,造成直接下載最新版 vue-router 4.x,而 4 以后的版本適用于 vue3.0 版本,用在 vue2.0+ 會報(bào)錯,故換版本
② 背景圖存在白邊
可以看見,左右都有白邊,采用了最粗暴的方法,給body的樣式
margin:0px
可以解決,上面也寫到了
到此這篇關(guān)于vue2和elementUI 實(shí)現(xiàn)落日余暉登錄頁和滑塊校驗(yàn)功能的文章就介紹到這了,更多相關(guān)vue2和elementUI 登錄頁和滑塊校驗(yàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element el-checkbox-group v-model不支持對象(object)解決方案
本文主要介紹了Element el-checkbox-group v-model不支持對象(object)解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vue使用ajax獲取后臺數(shù)據(jù)進(jìn)行顯示的示例
今天小編就為大家分享一篇vue使用ajax獲取后臺數(shù)據(jù)進(jìn)行顯示的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08VUE3+mqtt封裝解決多頁面使用需重復(fù)連接等問題(附實(shí)例)
最近了解到mqtt這樣一個(gè)協(xié)議,可以在web上達(dá)到即時(shí)通訊的效果,下面這篇文章主要給大家介紹了關(guān)于VUE3+mqtt封裝解決多頁面使用需重復(fù)連接等問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04vue addRoutes實(shí)現(xiàn)動態(tài)權(quán)限路由菜單的示例
本篇文章主要介紹了vue addRoutes實(shí)現(xiàn)動態(tài)權(quán)限路由菜單的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05vue中根據(jù)時(shí)間戳判斷對應(yīng)的時(shí)間(今天 昨天 前天)
這篇文章主要介紹了vue中 根據(jù)時(shí)間戳 判斷對應(yīng)的時(shí)間(今天 昨天 前天),需要的朋友可以參考下2019-12-12element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的方法
最近項(xiàng)目中需要用到日期選擇器,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的相關(guān)資料,需要的朋友可以參考下2023-09-09