Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

vue 2.0 子组件中的弹窗组件问题

问题如图所示,如何才能做到遮罩整个页面,目前只能遮罩到使用弹窗的子组件中

图片描述

Modal.vue

<template>
    <transition name="modal">
      <div class="modal-mask">
        <div class="modal-wrapper">
          <div class="modal-container">
            <div class="modal-header">
              <slot name="header">
                default header
              </slot>
            </div>

            <div class="modal-body">
              <slot name="body">
                default body
              </slot>
            </div>

            <div class="modal-footer">
              <slot name="footer">
                default footer
                <button class="modal-default-button" @click="$emit('close')">
                  OK
                </button>
              </slot>
            </div>
          </div>
        </div>
      </div>
    </transition>
</template>

<script>
  import Vue from 'vue'

  export default {

  }
</script>

<style rel="stylesheet/scss" lang="scss">

  .modal-mask {
    position: fixed;
    z-index: 9998;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, .5);
    display: table;
    transition: opacity .3s ease;
  }

  /*垂直居中*/
  .modal-wrapper {
    display: table-cell;
    vertical-align: middle;
  }

  .modal-container {
    width: 80%;
    height: calc(100vh - 38px * 2);
    margin: 0 auto;
    margin-top: 100px;
    padding: 20px 30px;
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
    transition: all .3s ease;
  }

  .modal-enter {
    opacity: 0;
  }

  .modal-leave-active {
    opacity: 0;
  }

  .modal-enter .modal-container,
  .modal-leave-active .modal-container {
    -webkit-transform: scale(1.1);
    transform: scale(1.1);
  }
</style>

使用

<template>

  <div>
 
   
    //页面主内容。。。
    
    <modal v-if="showModal" @close="showModal = false">
      <div slot="body">
        <p>123</p>
        <p>fsf</p>
        <p>12fsdfds3</p>
        <p>1vcxv23</p>
        <p>1vxcv23</p>
      </div>
    </modal>

  </div>
</template>

整个页面结构

<template>
  <div>
    <navbar :show="showNavBar" :title="title"></navbar>
    <sidebar v-on:onItemClick="clickItem($event)" :show="showSideBar"></sidebar>
    <app-main></app-main>
    <!--<fab></fab>-->
  </div>

</template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我是将弹窗的组件放在最外层组件中的,然后用vuex进行通信,这样来解决不占全屏的问题


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...