Vue实现简单弹框

App.vue

<template>
  <div id="app">
    <button
    type="button"
    class="btn"
    @click="showPopup"
    >Click me</button>
    <popup
    v-show="isPopupVisible"
    @close="closePopup">
    </popup>
  </div>
</template>

<script>
import popup from './components/popup'
export default{
  name: 'app',
  components: {
    popup
  },
  data (){
    return {
      isPopupVisible: false
    }
  },
  methods: {
    showPopup(){
      this.isPopupVisible = true
    },
    closePopup(){
      this.isPopupVisible = false
    }

  }
}
</script>

popup.vue

<template>
  <div class="container">
    <div class="popup">
      <header class="popup-header">
        <slot name="header">
          this is default header
          <button
          type="button"
          class="btn-close"
          @click="close">x</button>
        </slot>
      </header>
      <section class="popup-body">
        <slot name="body">
          I am the default body
        </slot>
      </section>
      <footer class="popup-footer">
        <slot name="footer">
          I am the default footer
          <button
          type="button"
          class="btn-green"
          @click="close">close me!
          </button>
        </slot>
      </footer>
    </div>
  </div>
</template>

<script>
export default {
  name: 'popup',
  methods:{
    close (){
      this.$emit('close')
    }
  }

}
</script>

<style>
  .container {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.3);
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .popup {
    background: #FFFFFF;
    box-shadow: 2px 2px 20px 1px;
    overflow-x: auto;
    display: flex;
    flex-direction: column;
  }
  .popup-header, .popup-footer{
    padding: 15px;
    display: flex;
  }
  .popup-header {
    border-bottom: 1px solid #eeeeee;
    color: #4AAE9B;
    justify-content: space-between;
  }
  .popup-footer {
    border-top: 1px solid #eeeeee;
    justify-content: flex-end;
  }
  .popup-body {
    position: relative;
    padding: 20px 10px;
  }
  .btn-close {
    border: none;
    font-size: 20px;
    padding: 20px;
    cursor: pointer;
    font-weight: bold;
    color: #4AAE9B;
    background: transparent;
  }
  .btn-green {
    color: white;
    background: #4AAE9B;
    border: 1px solid #4AAE9B;
    border-radius: 2px;
  }
</style>

参考链接:
https://baijiahao.baidu.com/s?id=1590850243088192932&wfr=spider&for=pc

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,591评论 2 45
  • 【新闻原链接】 中美就经贸问题达成共识 停止相互加征新的关税 https://baijiahao.baidu.co...
    产品思享汇阅读 218评论 0 0
  • 海德格尔说:人是被抛在这个世界上的。第一次接触存在主义的时候还是在初中,当时不能明白这句话,笑笑就过了。 ...
    自嘲成习惯阅读 452评论 0 0
  • 通透的效果太难画出来了 多画才是王道!
    海蝴蝶PB阅读 1,000评论 2 9
  • HTTP 协议定义了浏览器(即万维网客户进程)怎样向万维网服务器请求万维网文档,以及服务器怎样把文档传送给浏览器。...
    Q南南南Q阅读 655评论 0 1