liveh5-nuxt/app/components/floatingBubble/floating.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

import { createApp } from 'vue'
import MinWindow from '@/components/floatingBubble/index.vue'
let minWindowInstance = null
2025-02-26 03:11:29 +00:00
let minWindowApp = null
let container = null
// 创建悬浮窗
2025-02-26 03:11:29 +00:00
export const showMinWindow1 = (props = {}) => {
if (!process.client) return
2025-02-26 03:11:29 +00:00
// 如果在首页,不创建实例并销毁现有实例
if (window?.$nuxt?.$route?.path === '/') {
hideMinWindow1()
return null
}
// 如果已经存在实例,直接返回该实例
if (minWindowInstance) {
return minWindowInstance
}
2025-02-26 03:11:29 +00:00
// 确保清理旧的容器
const existingContainer = document.querySelector('.floating-bubble-container')
if (existingContainer) {
document.body.removeChild(existingContainer)
}
container = document.createElement('div')
container.className = 'floating-bubble-container'
document.body.appendChild(container)
const app = createApp(MinWindow, {
...props
})
minWindowApp = app
minWindowInstance = app.mount(container)
return minWindowInstance
}
export const hideMinWindow1 = () => {
if (!minWindowApp) return
2025-02-26 03:11:29 +00:00
try {
minWindowApp.unmount()
if (container && document.body.contains(container)) {
document.body.removeChild(container)
}
} catch (e) {
console.error('清理浮动气泡时出错:', e)
} finally {
// 确保状态被重置
minWindowApp = null
minWindowInstance = null
2025-02-26 03:11:29 +00:00
container = null
// 额外清理可能残留的容器
const existingContainer = document.querySelector('.floating-bubble-container')
if (existingContainer) {
document.body.removeChild(existingContainer)
}
}
}