Skip to content

Commit eff576c

Browse files
quanquan2100vanoneang
authored andcommitted
feat(sideBar): 添加手机模式侧边栏 (#230)
1 parent 0227e6d commit eff576c

File tree

2 files changed

+117
-25
lines changed

2 files changed

+117
-25
lines changed

src/components/layout/SideBar.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="app-sidebar">
3-
<div class="logo" v-if="!isCollapse">
3+
<div class="logo" v-if="!elMenuCollapse">
44
<img src="../../assets/img/logo.png" alt="">
55
</div>
66
<div class="mobile-logo" v-else>
@@ -34,7 +34,7 @@
3434
class="el-menu-vertical-demo"
3535
ref="meun"
3636
:default-active="defaultActive"
37-
:collapse="isCollapse"
37+
:collapse="elMenuCollapse"
3838
background-color="#192A5E"
3939
text-color="rgba(196,201,210,1)"
4040
active-text-color="#1890ff">
@@ -112,7 +112,6 @@ import Utils from '@/lin/utils/util'
112112
import Config from '../../config/index'
113113
114114
export default {
115-
props: ['isCollapse'],
116115
data() {
117116
return {
118117
sidebar: '',
@@ -122,6 +121,18 @@ export default {
122121
}
123122
},
124123
inject: ['eventBus'],
124+
props: {
125+
isPhone: {
126+
type: Boolean,
127+
default: false,
128+
},
129+
isCollapse: {
130+
type: Boolean,
131+
default: false,
132+
},
133+
},
134+
created() {
135+
},
125136
mounted() {
126137
this.eventBus.$on('removeSidebarSearch', () => {
127138
this.showSidebarSearch = false
@@ -188,6 +199,13 @@ export default {
188199
},
189200
},
190201
computed: {
202+
elMenuCollapse() {
203+
if (this.isPhone) {
204+
return false
205+
}
206+
207+
return this.isCollapse
208+
},
191209
// 根据当前路由设置激活侧边栏
192210
defaultActive() {
193211
for (let i = (this.stageInfo.length - 1); i >= 0; i -= 1) {
@@ -227,7 +245,7 @@ export default {
227245
return mapData
228246
},
229247
imgSrc() {
230-
return this.isCollapse === false ? '../../assets/img/left-logo.png' : '../../assets/img/logo.png'
248+
return this.elMenuCollapse === false ? '../../assets/img/left-logo.png' : '../../assets/img/logo.png'
231249
},
232250
...mapGetters(['sideBarList']),
233251
},

src/views/home/Home.vue

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
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>
@@ -26,6 +22,7 @@
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

Comments
 (0)