//data.js
import { createApp, h } from 'vue';
import HomeView from './HomeView.vue';
const bulletBox = (obj: { title: string; content: string; HTML?: boolean | null } = {
HTML: false,
title: '提示',
content: '内容',
}) => new Promise<string>((resolve) => {
const app = createApp({
render() {
return h(HomeView, {
title: obj.title,
content: obj.content,
HTML: obj.HTML || false,
onConfirm: (action: string) => {
resolve(action);
},
onCancel: (action: string) => {
resolve(action);
},
});
},
});
const vm = app.mount(document.createElement('div'));
document.body.appendChild(vm.$el);
});
export default bulletBox;
// HomeView.vue
<template>
<div id="BulletBox_id_home" @click="Element.close">
<div class="BulletBox_class_home_box" @click.stop>
<div class="BulletBox_move"
@mouseup="Element.mouseup"
@mousedown="Element.mousedown"
@touchend="Element.mouseup"
@touchstart="Element.mousedown"
style="padding-top: 10px">
<div style="padding:0px 10px 10px 10px" class="BulletBox_move_content" @mousedown.stop>
<!-- 标题 -->
<div class="BulletBox_class_title">{{ title }}</div>
<!-- 内容 -->
<div v-if="HTML" v-html="content"></div>
<div class="BulletBox_class_content" v-else>
{{ content }}
</div>
<div class="BulletBox_class_button">
<!-- 确定按钮 -->
<div @click="Element.Confirm" class="BulletBox_class_button_next">
确定
</div>
<!-- 取消按钮 -->
<div @click="Element.cancellation" class="BulletBox_class_button_cancellation">
取消
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
const props = defineProps({
title: String,
content: String,
HTML: Boolean,
onConfirm: Function,
onCancel: Function,
});
class Element {
/**
* @param {HTMLElement} BulletBoxClassHomeBox
*/
static BulletBoxClassHomeBox: HTMLElement | null = null;
/**
* @param {Number} X
*/
static X = 0;
/**
* @param {Number} Y
*/
static Y = 0;
// 关闭弹框
static close(): void {
/**
* @param {HTMLElement} bulletBox
*/
const bulletBox = document.querySelector('#BulletBox_id_home');
if (bulletBox) {
bulletBox.classList.add('BulletBox_animation');
setTimeout(() => {
bulletBox.parentNode!.removeChild(bulletBox);
}, 200);
}
}
// 确定回调
static Confirm(): void {
Element.close();
if (typeof props.onConfirm === 'function') {
props.onConfirm('确定');
}
}
// 取消回调
static cancellation(): void {
Element.close();
if (typeof props.onCancel === 'function') {
props.onCancel('取消');
}
}
static move(event: never | MouseEvent | TouchEvent): void {
const touchEvent = event instanceof MouseEvent ? event : event.touches[0];
if (Element.BulletBoxClassHomeBox) {
Element.BulletBoxClassHomeBox.style.left = `${touchEvent.clientX - Element.X + 200}px`;
Element.BulletBoxClassHomeBox.style.top = `${touchEvent.clientY - Element.Y}px`;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static mousedown(event: any | MouseEvent | TouchEvent): void {
const touchEvent = event instanceof MouseEvent ? event : event.touches[0];
this.X = touchEvent.offsetX;
this.Y = touchEvent.offsetY;
if (
event.target instanceof HTMLElement
&& event.target.classList.contains('BulletBox_move')
) {
// 监听鼠标/触摸移动事件
document.addEventListener('mousemove', Element.move);
document.addEventListener('touchmove', Element.move);
// 获取弹框元素
this.BulletBoxClassHomeBox = document.querySelector('.BulletBox_class_home_box') as HTMLElement;
} else {
throw new Error('未获取到DOM元素');
}
}
static mouseup(): void {
// 停止监听鼠标/触摸移动事件
document.removeEventListener('mousemove', Element.move);
document.removeEventListener('touchmove', Element.move);
}
}
</script>
<style scoped>
/* 可以添加样式 */
#BulletBox_id_home {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: #00000030;
overflow: hidden;
z-index: 2026;
overflow: auto;
}
.BulletBox_move {
position: relative;
z-index: 2023;
}
.BulletBox_move_content {
padding-bottom: 20px;
}
.BulletBox_move_content:hover {
cursor: auto;
}
.BulletBox_move:hover {
cursor: move;
}
.BulletBox_animation {
animation: bulletBox 0.3s;
}
@keyframes bulletBox {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes BulletBoxAnimation {
0% {
opacity: 0;
top: 180px;
}
100% {
opacity: 1;
top: 200px;
}
}
.BulletBox_class_home_box {
position: absolute;
top: 200px;
left: 50%;
transform: translateX(-50%);
width: 400px;
height: fit-content;
background: #fff;
border-radius: 10px;
animation: BulletBoxAnimation 0.3s;
}
.BulletBox_class_title {
font-size: 18px;
color: #303133;
}
.BulletBox_class_content {
padding: 10px 0px;
color: #606266;
word-wrap: break-word;
}
.BulletBox_class_button {
display: flex;
justify-content: flex-end;
}
.BulletBox_class_button_next:active {
background: #abd3fb;
}
.BulletBox_class_button_next:hover {
opacity: 0.7;
}
.BulletBox_class_button_cancellation:active {
background: #dddcdc53;
}
.BulletBox_class_button_cancellation:hover {
opacity: 0.7;
}
.BulletBox_class_button_next {
width: 60px;
font-size: 14px;
background: #409eff;
text-align: center;
height: 30px;
line-height: 30px;
border-radius: 6px;
color: #fff;
margin-right: 10px;
cursor: pointer;
}
.BulletBox_class_button_cancellation {
background: #fff;
height: 30px;
line-height: 30px;
border-radius: 6px;
width: 60px;
font-size: 14px;
text-align: center;
border: 1px solid #dcdfe6;
margin-right: 10px;
cursor: pointer;
}
</style>
import { createApp } from 'vue';
install(app: ReturnType<typeof createApp>) {
const modifiedApp = app;
modifiedApp.config.globalProperties.$Bullet = Bullet;
},
//全局部署
我是一个新手 希望大佬们指点不足