前端

Vue制作弹出对话框组件

2016-10-03  本文已影响3249人  进击的前端

之前写过一个组件的文章,算是入门,也是官网的例子,这个纯粹自己再写一遍,之前的文章

html结构

这也是官网的一个例子
首先我把生成后的html复制过来,分成两部分,一部分是一个按钮,用来弹出对话框,另外一部分是弹出对话框,最外层是一层遮罩,然后是对话框的主体。

<div id="app">
  <button id="show-modal">Show Modal</button>
  <!-- use the modal component, pass in the prop -->
  <div class="modal-mask modal-transition" style="display: none;">
    <div class="modal-wrapper">
      <div class="modal-container">
        <div class="modal-header">
          <h3 slot="header">custom header</h3>
        </div>
        <div class="modal-body">
            default body
        </div>
        <div class="modal-footer">
            default footer
            <button class="modal-default-button">
              OK
            </button>
        </div>
      </div>
    </div>
  </div>
</div>

Vue组件初步

参照另外一篇,进行模板的结构调整

<script type="x/template" id="modal-template">
     <div class="modal-mask modal-transition" style="display: none;">
    <div class="modal-wrapper">
      <div class="modal-container">
        <div class="modal-header">
          <h3 slot="header">custom header</h3>
        </div>
        <div class="modal-body">
            default body
        </div>
        <div class="modal-footer">
            default footer
            <button class="modal-default-button">
              OK
            </button>
        </div>
      </div>
    </div>
  </div>
</script>

<div id="app">
  <button id="show-modal">Show Modal</button>
  <!-- use the modal component, pass in the prop -->
    <modal></modal>
</div>

然后利用vue渲染

<script>
    // register modal component
Vue.component('modal', {
  template: '#modal-template'
})

// start app
new Vue({
  el: '#app'
})
</script>

这样就有了组件的基本结构,下面就开始css练习阶段

css

主要是利用display table来垂直居中以及fixed加上z-index

.modal-mask{
    background-color: rgba(0, 0, 0, .5);
    position: fixed;
    width: 100%;
    height:100%;
    top:0;
    left: 0;
    z-index: 99;
    display: table;
}
.modal-wrapper{
    display: table-cell;
    vertical-align: middle;
}
.modal-container{
    margin: auto;
    top: 50%;
    background-color: white;
    width: 300px;
    padding: 20px 30px;
}
.modal-header h3{
    margin-top: 0;
    color:#42b983;
}
.modal-body{
    margin: 20px 0;
}
.modal-default-button{
    float: right;
}

控制显示

用showModal来表示显示与否

// start app
new Vue({
  el: '#app',
    data: {
    showModal: false
  }
})

然后组件将值定义,注意这里加了.sync,是为了强制双向绑定,一把情况下组件的数据流是单向的。

<modal :show.sync="showModal"> </modal>

然后再组件的生命中定义show

Vue.component('modal', {
  template: '#modal-template',
  props: {
    show: {
      type: Boolean,
      required: true,
      twoWay: true
    }
  }
});

同时用show来控制mask的显示

<div class="modal-mask" v-show="show">

然后我们有两个关于显示的交互,一个在外面,通过按钮

<button id="show-modal" @click="showModel=true">Show Modal</button>

另外一个在里面

<button class="modal-default-button"  @click="show = false">  OK</button>

slot的用法

slot是关于内容分发的,用来代替子组件的内容,这里的对话框有三类内容,header,body,footer三部分内容,把对话框组件看成子组件,那么可以利用slot在子组件代替相关内容,比如最后的渲染效果如下

  <div class="modal-header">
 <h3>custom header</h3>
</div>

我们在子组件里面定义slot,name可以用来标识

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

在定义Modal的时候,我们可以利用slot的属性来替代子组件相应的部分。

<modal :show.sync="showModal">
 <h3 slot="header">custom header</h3>
</modal>

就这样还可以定义body和footer

过渡

如果我们增加了transition属性,那么我们要定义三个class

<div class="modal-mask" v-show="show" transition="modal">

然后我们增加了样式

.modal-mask{
transition: opacity .3s ease;
}
.modal-enter .modal-container,
.modal-leave .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
上一篇 下一篇

猜你喜欢

热点阅读