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

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos