vue.js自定义弹出组件插入到body中的实现划词分享功能

本文主要是介绍vue.js自定义弹出组件插入到body中的实现划词分享功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现效果
在这里插入图片描述

实现原理:

  • 方式一:通过 vue.extend 实现
  • 方式二:通过 new Vue() 实现,本例采用该方法实现,比较简单,即新实例化一个vue对象,单独操作它。其实就是动态创建一个Vue实例对象。

代码实现

<!-- components/pop-share/pop-share.vue -->
<template><!-- 最外层需要一个透明遮罩,监听点击事件 --><div class="mo-mask" :style="{ 'z-index': zIndex }" @click="handleClose"><transition name="el-fade-in-linear"><div class="pop-share" :style="style" @click.stop="handleClick"><div data-value="select" class="pop-share__item"><i class="el-icon-share"></i>选中分享</div><div data-value="all" class="pop-share__item"><i class="el-icon-share"></i>全部分享</div><div data-value="work-weixin" class="pop-share__item"><i class="el-icon-position"></i>发送到企业微信</div><div data-value="search" class="pop-share__item"><i class="el-icon-search"></i>搜索</div></div></transition></div>
</template><script>
// created at 2022-06-21
export default {name: 'pop-share',// 接收参数中可以定义需要接收的参数props: {top: {type: Number | String,default: 0},left: {type: Number | String,default: 0},// 点击事件回调onItemClick: {type: Function,default: null}},components: {},data() {return {zIndex: 0};},computed: {style() {return {top: this.top + 'px',left: this.left + 'px'};}},methods: {async getData() {},handleClick(e) {if (e.target.dataset.value) {if (this.onItemClick) {this.onItemClick({ value: e.target.dataset.value });}this.handleClose();}},handleClose(e) {this.$parent.handleClose();},// js获取当前窗口最大z-indexgetMaxZIndex() {var elements = document.querySelectorAll('*');let maxZindex = 0;for (var i = 0; i < elements.length; i++) {maxZindex = Math.max(maxZindex, elements[i].style.zIndex || 0);}return maxZindex;}},mounted() {// console.log(this.getMaxZIndex() + 1);this.zIndex = this.getMaxZIndex() + 1;},created() {this.getData();}
};
</script><style lang="less">
// 遮罩层
.mo-mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;width: 100%;height: 100%;background: rgba(0, 0, 0, 0);
}.pop-share {position: fixed;background-color: #373737;color: #fff;display: flex;align-items: center;line-height: 1.5;border-radius: 6px;transform: translate(-50%, -50px);
}.pop-share::after {content: '';position: absolute;top: 100%;left: 50%;transform: translateX(-50%);width: 0;height: 0;border-left: 8px solid transparent;border-right: 8px solid transparent;border-top: 8px solid #373737;
}.pop-share__item {font-size: 12px;line-height: 32px;height: 32px;padding: 0 12px;box-sizing: border-box;color: #dfdfdf;position: relative;cursor: pointer;white-space: nowrap;
}.pop-share__item:not(:last-child)::after {position: absolute;content: '';width: 1px;height: 14px;line-height: 14px;padding: 0;box-sizing: border-box;background-color: #666;top: 50%;right: 0;transform: translateY(-50%);
}
</style><style lang="less" scoped></style>
// components/pop-share/index.js
import Vue from 'vue';
import PopShare from './pop-share.vue';// 所有实例列表
let instances = [];// 显示
function show(props) {console.log('show');// let PopShareVue = Vue.extend(PopShare);// 此处需要使用 propsData传递参数// let instance = new PopShareVue({//   propsData: props// });let instance = new Vue({render(h) {return h(PopShare, { props });},methods: {handleClose() {close(instance);}}});instance.$mount();document.body.appendChild(instance.$el);instances.push(instance);
}// 关闭
function close(instance) {console.log('close');if (instance) {document.body.removeChild(instance.$el);}let index = instances.findIndex(item => item === instance);if (index > -1) {instances.splice(index, 1);}
}function closeAll() {for (let instance of instances) {close(instance);}
}export default {show,close,closeAll
};

将其挂载到Vue实例

// main.js
import Vue from 'vue';
import App from './App.vue';import PopShare from './components/pop-share/index.js';Vue.prototype.$popShare = PopShare;const app = new Vue({render: h => h(App)
}).$mount('#app');

调用弹出分享组件

<!-- App.vue -->
<template><div @mouseup="handleMouseUp" class="meeting-content" v-html="content"></div>
</template><script>
// created at 2022-06-21export default {name: '',data(){return {content: ""}}, 	methods: {handleMouseUp(e) {// 将当前鼠标的坐标传入	this.$popShare.show({top: e.clientY,left: e.clientX,// 点击回调onItemClick: e => {console.log(e);}});}},created() {// this.getData();}
};
</script><style lang="less">
.meeting-content {white-space: pre-wrap;line-height: 1.8;color: #222222;font-size: 14px;&::selection {background-color: #b0d4fc;}
}
</style><style lang="less" scoped></style>

参考

  • vue 自定义组件插入到body中的实现
  • 通过vue.extend实现消息提示弹框
  • js获取当前窗口最大z-index

这篇关于vue.js自定义弹出组件插入到body中的实现划词分享功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/516176

相关文章

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连