vue.js点击弹窗以外的区域关闭弹窗

点击弹窗外的区域关闭弹窗

代码结构

html:

<div class="publish_mask_popup" @click="hideMaskPopup" v-show="showMaskPopup">
	<div class="mask_popup">
		内容代码.....
	</div>
 </div>

js:

	data(){
		showMaskPopup:false
	},
	methods: {
       hideMaskPopup(e){
              let mask = document.querySelector(".mask_popup");
              if (mask) {
                  if (!mask.contains(e.target)) {//判断所点击的区域是否在.mask_popup中
                      this.showMaskPopup = false;
                  }
              }
        }
    },
	

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue.js框架中的模态框组件来实现点击按钮弹窗的功能。以下是一个简单的示例: 1. 在你的Vue组件中,引入模态框组件: ``` <template> <div> <button @click="showModal = true">点击弹窗</button> <modal v-if="showModal" @close="showModal = false"> <h3>这是一个弹窗标题</h3> <p>这是弹窗内容</p> </modal> </div> </template> <script> import Modal from './Modal.vue'; export default { components: { Modal }, data() { return { showModal: false } } } </script> ``` 2. 创建一个模态框组件Modal.vue: ``` <template> <div class="modal-mask"> <div class="modal-wrapper"> <div class="modal-container"> <div class="modal-header"> <slot name="header"></slot> <button class="modal-close-btn" @click="$emit('close')">关闭</button> </div> <div class="modal-body"> <slot></slot> </div> </div> </div> </div> </template> <script> export default { name: 'modal' } </script> <style> /* 模态框样式 */ .modal-mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 9998; } .modal-wrapper { display: table; width: 100%; height: 100%; } .modal-container { display: table-cell; vertical-align: middle; text-align: center; } .modal-header { margin-bottom: 10px; font-size: 18px; font-weight: bold; color: #333; } .modal-close-btn { position: absolute; top: 10px; right: 10px; padding: 5px 10px; border: none; background-color: #ddd; cursor: pointer; } .modal-body { padding: 20px; font-size: 16px; color: #666; } </style> ``` 3. 在你的CSS文件中添加以下样式来美化模态框: ``` /* 模态框样式 */ .modal-mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 9998; } .modal-wrapper { display: table; width: 100%; height: 100%; } .modal-container { display: table-cell; vertical-align: middle; text-align: center; } .modal-header { margin-bottom: 10px; font-size: 18px; font-weight: bold; color: #333; } .modal-close-btn { position: absolute; top: 10px; right: 10px; padding: 5px 10px; border: none; background-color: #ddd; cursor: pointer; } .modal-body { padding: 20px; font-size: 16px; color: #666; } ``` 4. 运行你的Vue应用程序,点击按钮就可以弹出模态框了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值