1
1
<template >
2
2
<div style =" height :100% ;" >
3
3
<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 >
6
6
</el-aside >
7
7
<el-container >
8
8
<el-header class =" header" >
9
9
<div class =" left" >
10
10
<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" />
16
12
<nav-bar ></nav-bar >
17
13
</div >
18
14
<el-collapse-transition >
26
22
</el-main >
27
23
<back-top :right =" 50" :bottom =" 50" :fontSize =" 34" ></back-top >
28
24
</el-container >
25
+ <div class =" sidenav-mask" :class =" {show: isPhone && isCollapse}" @click =" changeSlidebarState" ></div >
29
26
</el-container >
30
27
</div >
31
28
</template >
@@ -55,24 +52,46 @@ export default {
55
52
clientWidth: 0 , // 页面宽度
56
53
clientHeight: 0 , // 页面高度
57
54
foldState: false , // 控制左侧菜单栏按键
55
+ isPhone: false ,
58
56
}
59
57
},
58
+ created () {
59
+ },
60
60
mounted () {
61
- const _this = this
62
61
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
+ }
63
76
// 监测屏幕宽度 折叠左侧菜单栏
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
75
83
}
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
+ // }
76
95
}
77
96
78
97
this .eventBus .$on (' noReuse' , () => {
@@ -83,6 +102,29 @@ export default {
83
102
})
84
103
},
85
104
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
+ },
86
128
methods: {
87
129
// 控制菜单折叠
88
130
changeSlidebarState () {
@@ -105,14 +147,28 @@ export default {
105
147
},
106
148
watch: {
107
149
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
+ }
109
162
},
110
163
$route () {
111
164
this .showBackTop = false
112
165
if (this .scrollY <= 70 ) {
113
166
// MenuTab组件高度
114
167
this .backTop ()
115
168
}
169
+ if (this .isPhone && this .isCollapse ) {
170
+ this .changeSlidebarState ()
171
+ }
116
172
},
117
173
},
118
174
components: {
@@ -134,7 +190,8 @@ export default {
134
190
.aside {
135
191
background : rgb (25 , 42 , 94 );
136
192
overflow-x : hidden ;
137
- & ::-webkit-scrollbar {
193
+
194
+ & ::-webkit-scrollbar {
138
195
width : 0px ;
139
196
height : 0px ;
140
197
}
@@ -208,8 +265,25 @@ export default {
208
265
border-radius : 50% ;
209
266
z-index : 99 ;
210
267
background : #fff ;
268
+
211
269
.iconfont {
212
270
font-size : 36px ;
213
271
}
214
272
}
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
+ }
215
289
</style >
0 commit comments