js實(shí)現(xiàn)自動(dòng)鎖屏功能
有這么一個(gè)需求,開(kāi)發(fā)了一套系統(tǒng),當(dāng)用戶離開(kāi)桌面或者一段時(shí)間不操作的話,需要把該系統(tǒng)所有打開(kāi)頁(yè)面鎖定起來(lái),就跟桌面鎖屏一樣,只能輸入密碼驗(yàn)證成功后,或者重新登錄,才可以繼續(xù)操作頁(yè)面,如果刷新頁(yè)面,也要保持鎖定。就像下圖一樣。當(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īng)狀態(tài) setInterval(this.modalStatus, 1000) // 監(jiān)聽(tīng)鼠標(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)前沒(méi)鎖屏’)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. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法2. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. 解析原生JS getComputedStyle4. 輕松學(xué)習(xí)XML教程5. HTML DOM setInterval和clearInterval方法案例詳解6. 阿里前端開(kāi)發(fā)中的規(guī)范要求7. xpath簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理8. jsp EL表達(dá)式詳解9. css代碼優(yōu)化的12個(gè)技巧10. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器
