11<template >
22 <div style =" height :100% ;" >
33 <el-container >
4- <el-aside :width =" sideBarWidth" class =" aside" >
5- <side-bar :isCollapse =" isCollapse" ></side-bar >
4+ <el-aside :width =" sideBarWidth" class =" aside" :style = " asideStyle " >
5+ <side-bar :isCollapse =" isCollapse" :is-phone = " isPhone " ></side-bar >
66 </el-aside >
77 <el-container >
88 <el-header class =" header" >
99 <div class =" left" >
1010 <div class =" operate" ref =" operate" >
11- <i
12- class =" iconfont icon-fold"
13- :class =" {rotate: foldState}"
14- @click =" changeSlidebarState"
15- />
11+ <i class =" iconfont icon-fold" :class =" {rotate: foldState}" @click =" changeSlidebarState" />
1612 <nav-bar ></nav-bar >
1713 </div >
1814 <el-collapse-transition >
2622 </el-main >
2723 <back-top :right =" 50" :bottom =" 50" :fontSize =" 34" ></back-top >
2824 </el-container >
25+ <div class =" sidenav-mask" :class =" {show: isPhone && isCollapse}" @click =" changeSlidebarState" ></div >
2926 </el-container >
3027 </div >
3128</template >
@@ -55,24 +52,46 @@ export default {
5552 clientWidth: 0 , // 页面宽度
5653 clientHeight: 0 , // 页面高度
5754 foldState: false , // 控制左侧菜单栏按键
55+ isPhone: false ,
5856 }
5957 },
58+ created () {
59+ },
6060 mounted () {
61- const _this = this
6261 this .setResize ()
62+ // console.log(this.clientWidth)
63+ if (this .clientWidth < 500 ) {
64+ this .isPhone = true
65+ } else {
66+ this .isPhone = false
67+ // 检测屏幕宽度, 确定默认是否展开
68+ if (this .clientWidth <= 768 ) {
69+ this .eventBus .$emit (' removeSidebarSearch' )
70+ this .isCollapse = true
71+ } else {
72+ this .isCollapse = false
73+ this .eventBus .$emit (' showSidebarSearch' )
74+ }
75+ }
6376 // 监测屏幕宽度 折叠左侧菜单栏
64- window .onresize = function temp () {
65- _this .setResize ()
66- if (_this .clientWidth <= 768 ) {
67- // 页面宽度 768
68- if (_this .isCollapse === false ) {
69- _this .eventBus .$emit (' removeSidebarSearch' )
70- _this .isCollapse = true
71- }
72- } else if (_this .isCollapse === true ) {
73- _this .eventBus .$emit (' showSidebarSearch' )
74- _this .isCollapse = false
77+ window .onresize = () => {
78+ this .setResize ()
79+ if (this .clientWidth <= 500 ) {
80+ this .isPhone = true
81+ } else if (this .clientWidth <= 800 ) {
82+ this .isPhone = false
7583 }
84+
85+ // if (_this.clientWidth <= 768) {
86+ // // 页面宽度 768
87+ // if (_this.isCollapse === false) {
88+ // _this.eventBus.$emit('removeSidebarSearch')
89+ // _this.isCollapse = true
90+ // }
91+ // } else if (_this.isCollapse === true) {
92+ // _this.eventBus.$emit('showSidebarSearch')
93+ // _this.isCollapse = false
94+ // }
7695 }
7796
7897 this .eventBus .$on (' noReuse' , () => {
@@ -83,6 +102,29 @@ export default {
83102 })
84103 },
85104 inject: [' eventBus' ],
105+ computed: {
106+ elMenuCollapse () {
107+ if (this .isPhone ) {
108+ return false
109+ }
110+
111+ return this .isCollapse
112+ },
113+ asideStyle () {
114+ const style = {}
115+ if (this .isPhone ) {
116+ style .position = ' absolute'
117+ style .height = ` ${ this .clientHeight } px`
118+ style .zIndex = 12
119+ if (this .isCollapse === false ) {
120+ style .transform = ` translateX(-${ sideBarWidth} )`
121+ } else {
122+ style .transform = ' translateX(0)'
123+ }
124+ }
125+ return style
126+ },
127+ },
86128 methods: {
87129 // 控制菜单折叠
88130 changeSlidebarState () {
@@ -105,14 +147,28 @@ export default {
105147 },
106148 watch: {
107149 isCollapse () {
108- this .sideBarWidth = this .isCollapse === false ? sideBarWidth : ' 64px'
150+ if (this .isPhone ) {
151+ // 手机模式
152+ this .sideBarWidth = sideBarWidth
153+ if (this .isCollapse === false ) {
154+ this .transX = 0
155+ } else {
156+ this .transX = - 1 * sideBarWidth
157+ }
158+ } else {
159+ this .transX = 0
160+ this .sideBarWidth = this .isCollapse === false ? sideBarWidth : ' 64px'
161+ }
109162 },
110163 $route () {
111164 this .showBackTop = false
112165 if (this .scrollY <= 70 ) {
113166 // MenuTab组件高度
114167 this .backTop ()
115168 }
169+ if (this .isPhone && this .isCollapse ) {
170+ this .changeSlidebarState ()
171+ }
116172 },
117173 },
118174 components: {
@@ -134,7 +190,8 @@ export default {
134190.aside {
135191 background : rgb (25 , 42 , 94 );
136192 overflow-x : hidden ;
137- & ::-webkit-scrollbar {
193+
194+ & ::-webkit-scrollbar {
138195 width : 0px ;
139196 height : 0px ;
140197 }
@@ -208,8 +265,25 @@ export default {
208265 border-radius : 50% ;
209266 z-index : 99 ;
210267 background : #fff ;
268+
211269 .iconfont {
212270 font-size : 36px ;
213271 }
214272}
273+
274+ .sidenav-mask {
275+ position : absolute ;
276+ top : 0 ;
277+ left : 0 ;
278+ width : 100% ;
279+ height : 100% ;
280+ background-color : rgba (0 , 0 , 0 , .3 );
281+ z-index : 10 ;
282+ display : none ;
283+ cursor : pointer ;
284+
285+ & .show {
286+ display : block ;
287+ }
288+ }
215289 </style >
0 commit comments