2021-08-11 20:28:38 +00:00
|
|
|
import { defineNuxtPlugin } from '#app'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
|
|
nuxtApp.hook('app:mounted', () => {
|
2021-01-18 12:46:19 +00:00
|
|
|
const el = document.createElement('div')
|
|
|
|
el.id = 'nuxt-progress'
|
|
|
|
document.body.appendChild(el)
|
|
|
|
el.style.position = 'fixed'
|
|
|
|
el.style.backgroundColor = 'black'
|
|
|
|
el.style.height = '2px'
|
|
|
|
el.style.top = '0px'
|
|
|
|
el.style.left = '0px'
|
|
|
|
el.style.transition = 'width 0.1s, opacity 0.4s'
|
|
|
|
const duration = 3000
|
|
|
|
const progress = 10000 / Math.floor(duration)
|
2022-08-12 17:47:58 +00:00
|
|
|
let timeout: ReturnType<typeof setTimeout> | undefined
|
|
|
|
let interval: ReturnType<typeof setInterval> | undefined
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.hook('page:start', () => {
|
2021-01-18 12:46:19 +00:00
|
|
|
if (timeout) { return }
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
let width = 10
|
|
|
|
el.style.opacity = '100%'
|
|
|
|
el.style.width = '10%'
|
|
|
|
interval = setInterval(() => {
|
|
|
|
if (width >= 100) { return }
|
|
|
|
width = Math.floor(width + progress)
|
|
|
|
el.style.width = `${width}%`
|
|
|
|
}, 100)
|
|
|
|
}, 200)
|
|
|
|
})
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.hook('page:finish', () => {
|
2021-01-18 12:46:19 +00:00
|
|
|
timeout && clearTimeout(timeout)
|
2022-08-12 17:47:58 +00:00
|
|
|
timeout = undefined
|
2021-01-18 12:46:19 +00:00
|
|
|
interval && clearInterval(interval)
|
2022-08-12 17:47:58 +00:00
|
|
|
interval = undefined
|
2021-01-18 12:46:19 +00:00
|
|
|
el.style.width = '100%'
|
|
|
|
el.style.opacity = '0%'
|
|
|
|
setTimeout(() => {
|
|
|
|
el.style.width = '0%'
|
|
|
|
}, 500)
|
|
|
|
})
|
|
|
|
})
|
2021-06-18 17:16:51 +00:00
|
|
|
})
|