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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市瑰抵,隨后出現(xiàn)的幾起案子你雌,更是在濱河造成了極大的恐慌,老刑警劉巖谍憔,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匪蝙,死亡現(xiàn)場離奇詭異,居然都是意外死亡习贫,警方通過查閱死者的電腦和手機(jī)逛球,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苫昌,“玉大人颤绕,你說我怎么就攤上這事∷钌恚” “怎么了奥务?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長袜硫。 經(jīng)常有香客問我氯葬,道長,這世上最難降的妖魔是什么婉陷? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任帚称,我火速辦了婚禮,結(jié)果婚禮上秽澳,老公的妹妹穿的比我還像新娘闯睹。我一直安慰自己,他們只是感情好担神,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布楼吃。 她就那樣靜靜地躺著,像睡著了一般妄讯。 火紅的嫁衣襯著肌膚如雪孩锡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天捞挥,我揣著相機(jī)與錄音浮创,去河邊找鬼。 笑死砌函,一個胖子當(dāng)著我的面吹牛斩披,可吹牛的內(nèi)容都是我干的溜族。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼垦沉,長吁一口氣:“原來是場噩夢啊……” “哼煌抒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起厕倍,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤寡壮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后讹弯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體况既,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年组民,在試婚紗的時候發(fā)現(xiàn)自己被綠了棒仍。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡臭胜,死狀恐怖莫其,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耸三,我是刑警寧澤乱陡,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站仪壮,受9級特大地震影響憨颠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜积锅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一烙心、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧乏沸,春花似錦、人聲如沸爪瓜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽铆铆。三九已至蝶缀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間薄货,已是汗流浹背翁都。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留谅猾,地道東北人柄慰。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓鳍悠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親坐搔。 傳聞我的和親對象是個殘疾皇子藏研,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

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

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