vue 弹出框通用模板

1 篇文章 0 订阅
本文介绍了一个Vue弹出框通用模板,详细展示了如何通过引入、注册及使用组件来实现动态自定义内容的弹出框。通过示例代码,包括Vue页面、JS和Style部分,以及调用方法的步骤,帮助开发者轻松集成和控制弹出框行为。
摘要由CSDN通过智能技术生成

介绍:该模板的图片,内容,按钮等都可以动态自定义内容

vue页面代码

<template>
  <transition name="confirm-fade">
    <div v-if="isShowConfirm" class="my-confirm" @click.stop="clickFun('clickCancel')">
      <div class="confirm-content-wrap" @click.stop>
      	<li class="el-icon-close" style="padding: 15px;float: right;" @click="clickFun('clickCancel')"></li>
      	<div style="margin-top: 20px;text-align: center;">
      		<img :src="titleText" class="warm-icon"/>
      		<span style="display: block;">{{ content }}</span>
      	</div>
      	<div class="btnDiv">
		    	<button type="button" class="editbtn" @click="clickFun('clickConfirm')">{{ confirmText }}</button>
		    	<button type="button" class="returnbtn" v-if="type==='confirm'" @click="clickFun('clickCancel')">{{ cancelText }}</button>
		    </div>
      </div>
    </div>
  </transition>
</template>

js代码

<script>
export default {
  data () {
    return {
      isShowConfirm: false, // 用于控制整个窗口的显示/隐藏
      titleText: require('@/assets/images/warn_03.png'), // 提示图片
      content: '确认删除用户xx', // 提示框的内容
      cancelText: '取消', // 取消按钮的文字
      confirmText: '确认', // 确认按钮的文字
      type: 'confirm', // 表明弹框的类型:confirm - 确认弹窗(有取消按钮);alert - 通知弹框(没有取消按钮)
      outerData: null // 用于记录外部传进来的数据,也可以给外部监听userBehavior,事件的函数提供判断到底是哪个事件触发的
    }
  },
  methods: {
    show (content, config) {
      this.content = content || 'Say Something ...'   //文本内容

      if (Object.prototype.toString.call(config) === '[object Object]') {
        // 确保用户传递的是一个对象
        this.titleText = config.titleText || '操作提示图片'
        this.cancelText = config.cancelText || '取消'
        this.confirmText = config.confirmText || '确认'
        this.type = config.type || 'confirm'
        this.outerData = config.data || null
      }

      this.isShowConfirm = true
    },
    hidden () {
      this.isShowConfirm = false
      this.titleText = '操作提示'
      this.cancelText = '取消'
      this.confirmText = '确认'
      this.type = 'confirm'
      this.outerData = null
    },
    clickFun (type) {
    	console.log(type)
      this.$emit('userBehavior', type, this.outerData)
      console.log("弹出框",this.outerData)
      this.hidden()
    }
  }
}
</script>

style代码

<style scoped>
  .my-confirm {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 998;
    /* 这里防止当用户长按屏幕,出现的黑色背景色块,以及 iPhone 横平时字体的缩放问题 */
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  }

  /* 进入和出去的动画 */
  .confirm-fade-enter-active {
    animation: opacity 0.3s;
  }
  .confirm-fade-enter-active .confirm-content-wrap {
    animation: scale 0.3s;
  }
  .confirm-fade-leave-active {
    animation: outOpacity 0.2s;
  }

  /* 包裹层容器样式 */
  .confirm-content-wrap {
    position: absolute;
    top: 28%;
    left: 0;
    right: 0;
    width: 340px;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 2px;
    z-index: 999;
    user-select: none;
  }

  .warm-icon{
  	width: 40px;
  	height: 40px;
  	background-size: cover;
  	margin-bottom: 20px;
  	margin-top: 20px;
  	margin-left: 25px;
  }
	 .editbtn{
  	width: 100px;
  	background: #0098e4;
  	height: 25px;
  	border: 1px solid #0098e4;
  	color: #FFFFFF;
  }
  .editbtn{
  	cursor: pointer;
  }
  .returnbtn{
  	width: 100px;
  	background: #d0dde4;
  	height: 25px;
  	border: 1px solid #d0dde4;
  	color: #000000;
  	margin-left: 30px;
  }
  .returnbtn:hover{
  	cursor: pointer;
  }
  .btnDiv{
  	margin-top: 40px;
  	display: flex;
  	justify-content: center;
  	align-content: center;
  	margin-bottom: 20px;
  }
  /* 进来的动画 */
  @keyframes opacity {
    0% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }
  @keyframes scale {
    0% {
      transform: scale(0);
    }
    60% {
      transform: scale(1.1);
    }
    100% {
      transform: scale(1);
    }
  }

  /* 出去的动画 */
  @keyframes outOpacity {
    0% {
      opacity: 1;
    }
    100% {
      opacity: 0;
    }
  }
</style>

使用方式:

1.引入模板  import Confirm  from './Confirm'

2.注册组件

3.在页面种使用组件

<confirm ref="myConfirm" @userBehavior="userBehaviorFun"></confirm>  <!--弹出框组件引入-->

5.js中调用方法

6.弹出框方法调用

 

引用来自:https://blog.csdn.net/six_six_six_666/article/details/82709821

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值