亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例

 更新時(shí)間:2022年04月26日 09:53:08   作者:小丫頭呀  
本文主要介紹了Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.最終實(shí)現(xiàn)的效果圖

2.  實(shí)現(xiàn)左側(cè)菜單折疊與展開功能步驟

2.1 首先應(yīng)該在菜單頂部放一個(gè)折疊展開的按鈕條

2.2 接下來,畫按鈕條UI結(jié)構(gòu),實(shí)現(xiàn)折疊與展開功能

1. 在側(cè)邊欄內(nèi)部,在側(cè)邊欄菜單區(qū)域之前,放置一個(gè)DIV。

 2. 在該DIV上面添加文本,通過點(diǎn)擊該DIV,觸發(fā)菜單折疊與展開

3.通過類選擇器,為該DIV 添加相關(guān)樣式

.toggle-button{
 // 添加背景顏色
 background-color: #4A5064;
 // 設(shè)置文本大小
 font-size:10px;
 // 設(shè)置文本行高
 line-height:24px;
 // 設(shè)置文本顏色
 color:#fff;
 // 設(shè)置文本居中
 text-align: center;
 // 設(shè)置文本間距
 letter-spacing: 0.2em;
 // 設(shè)置鼠標(biāo)懸浮變小手效果
 cursor:pointer;
}

4. 添加完樣式后的效果

2.3  實(shí)現(xiàn)點(diǎn)擊該DIV時(shí),觸發(fā)菜單折疊與展開

1. 首先,需要為該DIV 按鈕條,綁定單擊事件

2. 為側(cè)邊欄菜單綁定 collapse 屬性

collapse 屬性   說明:是否水平折疊收起菜單(僅在 mode 為 vertical 時(shí)可用)

該屬性默認(rèn)值是 False,把該值改變 True,就可以實(shí)現(xiàn)折疊與展開效果了 

Menu 菜單 | Element Plus (gitee.io)

如上,為菜單欄綁定了屬性并賦值后,左側(cè)菜單欄折疊與展開效果還并未生效。 

 3. 接下來先為側(cè)邊欄菜單 collapse 屬性  動(dòng)態(tài)賦值,并實(shí)現(xiàn)按鈕條的點(diǎn)擊單擊事件,讓折疊與展開效果生效  

首先在數(shù)據(jù)區(qū)定義一個(gè)接受bool 變量對(duì)象

當(dāng)觸發(fā)DIV 點(diǎn)擊事件時(shí),對(duì)該bool 值對(duì)象進(jìn)行動(dòng)態(tài)賦值切換

 修改側(cè)邊欄,動(dòng)態(tài)接受bool 值

 通過collapse-transition 關(guān)閉側(cè)邊欄收縮動(dòng)畫效果。就是左側(cè)菜單欄收縮是,是否有動(dòng)畫效果。默認(rèn)值是true

 4.效果

2.4  折疊與展開功能是實(shí)現(xiàn)了,但是背景顏色并沒有隨著菜單折疊時(shí)跟著變小或展開時(shí)跟著變大

1. 是什么原因?qū)е碌?/p>

首先,整個(gè)紅色區(qū)域是屬于左側(cè)菜單欄的,那么查看代碼的UI結(jié)構(gòu)時(shí),就會(huì)發(fā)現(xiàn),這個(gè)菜單側(cè)邊欄是固定寫死了一個(gè)200寬度的像素。 也就是說,是固定的這個(gè)寬度值,導(dǎo)致左側(cè)菜單欄背景無法跟著動(dòng)態(tài)折疊起來的原因。

2. 如何解決 只需要在判斷折疊與展開時(shí),賦不同的寬度值即可解決

也就是說,如果 isCollapse 值為true(折疊) 的時(shí)候,賦值為46 px (把像素值變?。?。isCollapse 值為false (展開)的時(shí)候,賦值為200 px (把像素值還原)。

通過三元運(yùn)算符解決

2.5. 最終效果

 3. Home.vue 代碼

<template>
  <el-container class="home_container">
    <!-- 頭部區(qū)域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>電商后臺(tái)管理系統(tǒng)</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 頁面主體區(qū)域 -->
    <el-container>
      <!-- 側(cè)邊欄 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 側(cè)邊欄菜單區(qū)域 -->
        <el-menu active-text-color="#409Eff"
        background-color="#545c64"
        text-color="#fff" unique-opened :collapse="isCollapse" :collapse-transition="false">
        <!-- 一級(jí)菜單 -->
        <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
          <!-- 一級(jí)菜單模板區(qū)域 -->
          <template #title>
            <el-icon :class="iconsObj[item.id]"></el-icon>
            <span>{{item.authName}}</span>
          </template>
          <!-- 二級(jí)菜單 -->
          <el-menu-item :index="subItem.id+''" v-for="subItem in item.children" :key="subItem.id">
          <template #title>
            <el-icon><iconMenu /></el-icon>
            <span>{{subItem.authName}}</span>
          </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
      </el-aside>
      <!-- 右側(cè)內(nèi)容主體區(qū)域 -->
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      // 左側(cè)菜單數(shù)據(jù)對(duì)象
      menulist: [],
      // 字體圖標(biāo)對(duì)象
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折疊
      isCollapse: false
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 獲取所有的菜單數(shù)據(jù)
    async  getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 成功了,進(jìn)行賦值
      this.menulist = res.data
      console.log(res)
    },
    // 點(diǎn)擊按鈕,切換菜單的折疊與展開
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  // 給頭部設(shè)置一下彈性布局
  display: flex;
  // 讓它貼標(biāo)左右對(duì)齊
  justify-content: space-between;
  // 清空?qǐng)D片左側(cè)padding
  padding-left: 0;
  // 按鈕居中
  align-items: center;
  // 文本顏色
  color: #fff;
  // 設(shè)置文本字體大小
  font-size: 20px;
  // 嵌套
  > div {
    // 彈性布局
    display: flex;
    // 縱向上居中對(duì)齊
    align-items: center;
    // 給文本和圖片添加間距,使用類選擇器
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
 // 添加背景顏色
 background-color: #4A5064;
 // 設(shè)置文本大小
 font-size:10px;
 // 設(shè)置文本行高
 line-height:24px;
 // 設(shè)置文本顏色
 color:#fff;
 // 設(shè)置文本居中
 text-align: center;
 // 設(shè)置文本間距
 letter-spacing: 0.2em;
 // 設(shè)置鼠標(biāo)懸浮變小手效果
 cursor:pointer;
}
</style>

到此這篇關(guān)于Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例的文章就介紹到這了,更多相關(guān)Vue3+Element-Plus左側(cè)菜單折疊與展開內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論