Vue制作彈出對話框組件

之前寫過一個組件的文章,算是入門,也是官網(wǎng)的例子,這個純粹自己再寫一遍,之前的文章

html結(jié)構(gòu)

這也是官網(wǎng)的一個例子
首先我把生成后的html復(fù)制過來,分成兩部分,一部分是一個按鈕,用來彈出對話框,另外一部分是彈出對話框,最外層是一層遮罩,然后是對話框的主體。

<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組件初步

參照另外一篇,進(jìn)行模板的結(jié)構(gòu)調(diào)整

<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>

這樣就有了組件的基本結(jié)構(gòu),下面就開始css練習(xí)階段

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,是為了強(qiáng)制雙向綁定,一把情況下組件的數(shù)據(jù)流是單向的。

<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">

然后我們有兩個關(guān)于顯示的交互,一個在外面,通過按鈕

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

另外一個在里面

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

slot的用法

slot是關(guān)于內(nèi)容分發(fā)的,用來代替子組件的內(nèi)容,這里的對話框有三類內(nèi)容,header,body,footer三部分內(nèi)容,把對話框組件看成子組件,那么可以利用slot在子組件代替相關(guān)內(nèi)容,比如最后的渲染效果如下

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

我們在子組件里面定義slot,name可以用來標(biāo)識

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

在定義Modal的時候,我們可以利用slot的屬性來替代子組件相應(yīng)的部分。

<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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,646評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,595評論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,560評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,814評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,224評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,444評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,988評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,804評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,998評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,237評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,706評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,993評論 2 374

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,665評論 25 708
  • 1.安裝 可以簡單地在頁面引入Vue.js作為獨(dú)立版本,Vue即被注冊為全局變量,可以在頁面使用了。 如果希望搭建...
    Awey閱讀 11,065評論 4 129
  • 此文基于官方文檔,里面部分例子有改動,加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,839評論 5 14
  • 五絕?詠蘆葦(新韻) 李鱷淚 莫說白頭老,能生貧賤田。秋風(fēng)吹做雪,一夜?jié)M長安。
    雷純閱讀 659評論 0 4
  • 人,為什么做個屁大點(diǎn)事都要戰(zhàn)戰(zhàn)兢兢呢? 是怕旁人鄙夷的目光還是朋友的不點(diǎn)贊不評論? 如果是的話,未免活的太累了。想...
    滿滿_ce31閱讀 120評論 0 0