動態頭部-vue

h5項目中,常常用到的頭部是樣式是一致的,只是左右按鈕和中間標題會不一致。html

vue主打組件化,爲了減小代碼冗餘,能夠將頭部提取成一個單獨的組件。接下來考慮是否須要左右按鈕,是否固定在頁面上不動,中間標題是否爲動態。vue

先寫一個簡單的頭部,position設置成變量customFixed。左右按鈕經過<slot>來控制。中間標題設置成變量customTitle經過父子組件傳值過來。app

設置好樣式之後給customFixed和customTitle默認值和類型。組件化

<template>
    <div id="header" :style="{'position' : customFixed ? 'fixed' : 'absolute'}">
      <slot name="left"></slot>
      {{customTitle}}
      <slot name="right"></slot>
    </div>
</template>

<script>
    export default {
      name: "my-header",
      props:{
        customTitle : {
            type : String,
            default : '標題'
          },
        customFixed: {
            type: Boolean,
            default: false
        }
      }
    }
</script>

<style scoped>
#header{
  width: 100%;height: 40px;background: #666;color: white;text-align: center;line-height: 40px;
  position: absolute;left:0;top: 0;z-index: 10;
}
  #header button {height: 100%;padding: 0 50px;}
  #header button:nth-of-type(1){float: left}
  #header button:nth-of-type(2){float: right}
</style>

在用到頭部的地方:spa

<template>
  <div id="app">
    <my-header custom-title="通信錄" custom-fixed>
      <button @click="backBtn" slot="left">返回</button>
      <button @click="homeBtn" slot="right">主頁</button>
    </my-header>
  </div>
</template>
相關文章
相關標籤/搜索