VUE系列——弹窗代码编写与调用弹窗过程详解

简介: VUE系列——弹窗代码编写与调用弹窗过程详解

前言

本文主要介绍弹窗的代码,包括前端、js、css样式,以及点击按钮调用弹窗的过程详解。

步骤如下:


step1 创建一个弹窗

Modal.vue

template

<template>
  <div class="modal-bg" v-show="show" @mousemove="modalMove" @mouseup="cancelMove">
    <div class="modal-container">
      <div class="modal-header" @mousedown="setStartingPoint">
        {{ title }}
      </div>
      <div class="modal-main">
        <slot></slot>
        <p>弹窗里面</p>
      </div>
      <div class="modal-footer">
        <el-button round @click="cancel">取消</el-button>
        <el-button type="primary" round @click="submit">确认</el-button>
      </div>
    </div>
  </div>
</template>

script

1. <script><script>
    export default {
    name: 'Modal',
    props: {
      show: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: '弹窗'
      },
    },
    data() {
      return {
        x: 0,
        y: 0,
        node: null,
        isCanMove: false
      }
    },
    mounted() {
      this.node = document.querySelector('.modal-container')
    },
    methods: {
      cancel() {
        this.$emit('cancel')
      },
      submit() {
        this.$emit('submit')
      },
      setStartingPoint(e) {
        this.x = e.clientX - this.node.offsetLeft
        this.y = e.clientY - this.node.offsetTop
        this.isCanMove = true
      },
      modalMove(e) {
        if (this.isCanMove) {
          this.node.style.left = e.clientX - this.x + 'px'
          this.node.style.top = e.clientY - this.y + 'px'
        }
      },
      cancelMove() {
        this.isCanMove = false
      }
    }
    }
</script>

style

<style scoped>
  .modal-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.5);
    z-index: 10;
  }
  .modal-container {
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
  .modal-header {
    height: 56px;
    background: #409EFF;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: move;
  }
  .modal-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 57px;
    border-top: 1px solid #ddd;
  }
  .modal-footer button {
    width: 100px;
  }
  .modal-main {
    padding: 15px 40px;
  }
</style>

step2 在router里面引入Modal.vue

1. importimport modal from '../components/Modal';
return[{ 
    path: 'modal', 
    name: '弹窗', 
    component: modal 
}]

step3 调用

template

<Modal :show="show" @cancel="cancel" @submit="submit"></Modal>

script

import Modal from '../../components/Modal';
export default {
  data() {
    return {
      show: false,
    }
  },
  components: {
    Modal
  },
  methods: {
    cancel() {
      // 取消弹窗回调
      this.show = false
    },
    submit() {
      // 确认弹窗回调
      this.show = false
    },
    showWindow(){
      this.show = true
    },
    changeRoute() {
      this.$router.push('/welcome/page2');
    }
  }
};


OK, GAME OVER!

相关文章
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
1072 2
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
932 137
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
2244 0
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
1129 1
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
1105 1
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
855 0
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1801 4
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
2139 78
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍