js實(shí)現(xiàn)自動(dòng)鎖屏功能
有這么一個(gè)需求,開發(fā)了一套系統(tǒng),當(dāng)用戶離開桌面或者一段時(shí)間不操作的話,需要把該系統(tǒng)所有打開頁面鎖定起來,就跟桌面鎖屏一樣,只能輸入密碼驗(yàn)證成功后,或者重新登錄,才可以繼續(xù)操作頁面,如果刷新頁面,也要保持鎖定。就像下圖一樣。當(dāng)然用戶也可以手動(dòng)觸發(fā)鎖屏。目的是防止他人隨意操作系統(tǒng)的重要內(nèi)容。那么該如何去實(shí)現(xiàn)呢?
5s鎖屏效果如下:
有點(diǎn)繞,需要好好捋一捋。
3.代碼實(shí)現(xiàn)以下代碼是不完全代碼,html結(jié)構(gòu)省略了,大家自由發(fā)揮。
// app.vuedata () { return { timeOut: 5000, timer: null, isLock: ’false’ }},mounted () { this.timer = setTimeout(this.lockPro, this.timeOut) // 首次設(shè)置操作時(shí)間 localStorage.setItem(’moveTime’, Date.now()) // 首次判斷狀態(tài) this.modalStatus() // 輪詢監(jiān)聽狀態(tài) setInterval(this.modalStatus, 1000) // 監(jiān)聽鼠標(biāo)事件 this.events()},methods:{ events() { window.onmousemove = () => {// console.log(’鼠標(biāo)移動(dòng)了’)if (!this.isLock) { localStorage.setItem(’moveTime’, Date.now()) this.clearLocaPro(’continue’)} } }, modalStatus() { if (localStorage.getItem(’isLock’) === ’true’) {// console.log(’鎖屏了’)this.isLock = truethis.clearLocaPro() } else {// console.log(’當(dāng)前沒鎖屏’)this.isLock = falsethis.clearLocaPro(’continue’) } }, lockPro() { if (!this.timeOut) {localStorage.setItem(’isLock’, ’false’)this.clearLocaPro(’continue’)return } if (Date.now() - localStorage.getItem(’moveTime’) < this.timeOut) {localStorage.setItem(’isLock’, ’false’)this.clearLocaPro(’continue’) } else {localStorage.setItem(’isLock’, ’true’)this.clearLocaPro() } }, clearLocaPro(status) { if(this.timer){ clearTimeout(this.timer) } if (status === ’continue’) {this.timer = setTimeout(this.lockPro, this.timeOut) } }, // 手動(dòng)鎖定 handleLock(){ this.clearLocaPro() localStorage.setItem(’isLock’, ’true’) }, // 密碼解鎖 unlock(){ localStorage.removeItem(’isLock’) localStorage.setItem(’moveTime’, Date.now()) this.clearLocaPro(’continue’) }, ... // 別忘了退出登錄也要清除isLock}
到此這篇關(guān)于js實(shí)現(xiàn)自動(dòng)鎖屏功能的文章就介紹到這了,更多相關(guān)js 自動(dòng)鎖屏 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門精解之結(jié)構(gòu)與語法2. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. 利用CSS3新特性創(chuàng)建透明邊框三角5. XML入門的常見問題(一)6. HTML5 Canvas繪制圖形從入門到精通7. 概述IE和SQL2k開發(fā)一個(gè)XML聊天程序8. HTML <!DOCTYPE> 標(biāo)簽9. HTML DOM setInterval和clearInterval方法案例詳解10. XML入門的常見問題(二)
