浏览代码

refactor[ScrollBar]: use el-scrollbar (#90)

花裤衩 7 年之前
父节点
当前提交
f01a1ad7b7

+ 0 - 57
src/components/ScrollBar/index.vue

@@ -1,57 +0,0 @@
1
-<template>
2
-  <div class="scroll-container" ref="scrollContainer" @wheel.prevent="handleScroll" >
3
-    <div class="scroll-wrapper" ref="scrollWrapper" :style="{top: top + 'px'}">
4
-      <slot></slot>
5
-    </div>
6
-  </div>
7
-</template>
8
-
9
-<script>
10
-const delta = 15
11
-
12
-export default {
13
-  name: 'scrollBar',
14
-  data() {
15
-    return {
16
-      top: 0
17
-    }
18
-  },
19
-  methods: {
20
-    handleScroll(e) {
21
-      const eventDelta = e.wheelDelta || -e.deltaY * 3
22
-      const $container = this.$refs.scrollContainer
23
-      const $containerHeight = $container.offsetHeight
24
-      const $wrapper = this.$refs.scrollWrapper
25
-      const $wrapperHeight = $wrapper.offsetHeight
26
-      if (eventDelta > 0) {
27
-        this.top = Math.min(0, this.top + eventDelta)
28
-      } else {
29
-        if ($containerHeight - delta < $wrapperHeight) {
30
-          if (this.top < -($wrapperHeight - $containerHeight + delta)) {
31
-            this.top = this.top
32
-          } else {
33
-            this.top = Math.max(this.top + eventDelta, $containerHeight - $wrapperHeight - delta)
34
-          }
35
-        } else {
36
-          this.top = 0
37
-        }
38
-      }
39
-    }
40
-  }
41
-}
42
-</script>
43
-
44
-<style rel="stylesheet/scss" lang="scss" scoped>
45
-@import '../../styles/variables.scss';
46
-
47
-.scroll-container {
48
-  position: relative;
49
-  width: 100%;
50
-  height: 100%;
51
-  background-color: $menuBg;
52
-  .scroll-wrapper {
53
-    position: absolute;
54
-     width: 100%!important;
55
-  }
56
-}
57
-</style>

+ 1 - 0
src/store/modules/app.js

@@ -16,6 +16,7 @@ const app = {
16
         Cookies.set('sidebarStatus', 0)
16
         Cookies.set('sidebarStatus', 0)
17
       }
17
       }
18
       state.sidebar.opened = !state.sidebar.opened
18
       state.sidebar.opened = !state.sidebar.opened
19
+      state.sidebar.withoutAnimation = false
19
     },
20
     },
20
     CLOSE_SIDEBAR: (state, withoutAnimation) => {
21
     CLOSE_SIDEBAR: (state, withoutAnimation) => {
21
       Cookies.set('sidebarStatus', 1)
22
       Cookies.set('sidebarStatus', 1)

+ 20 - 1
src/styles/index.scss

@@ -5,26 +5,45 @@
5
 @import './sidebar.scss';
5
 @import './sidebar.scss';
6
 
6
 
7
 body {
7
 body {
8
+  height: 100%;
8
   -moz-osx-font-smoothing: grayscale;
9
   -moz-osx-font-smoothing: grayscale;
9
   -webkit-font-smoothing: antialiased;
10
   -webkit-font-smoothing: antialiased;
10
   text-rendering: optimizeLegibility;
11
   text-rendering: optimizeLegibility;
11
   font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
12
   font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
12
 }
13
 }
13
 
14
 
15
+label {
16
+  font-weight: 700;
17
+}
18
+
14
 html {
19
 html {
20
+  height: 100%;
15
   box-sizing: border-box;
21
   box-sizing: border-box;
16
 }
22
 }
17
 
23
 
24
+#app{
25
+  height: 100%;
26
+}
27
+
18
 *,
28
 *,
19
 *:before,
29
 *:before,
20
 *:after {
30
 *:after {
21
   box-sizing: inherit;
31
   box-sizing: inherit;
22
 }
32
 }
23
 
33
 
24
-div:focus{
34
+a,
35
+a:focus,
36
+a:hover {
37
+  cursor: pointer;
38
+  color: inherit;
25
   outline: none;
39
   outline: none;
40
+  text-decoration: none;
26
 }
41
 }
27
 
42
 
43
+div:focus{
44
+  outline: none;
45
+ }
46
+
28
 a:focus,
47
 a:focus,
29
 a:active {
48
 a:active {
30
   outline: none;
49
   outline: none;

+ 31 - 18
src/styles/sidebar.scss

@@ -1,18 +1,13 @@
1
 #app {
1
 #app {
2
-
3
   // 主体区域
2
   // 主体区域
4
   .main-container {
3
   .main-container {
5
     min-height: 100%;
4
     min-height: 100%;
6
     transition: margin-left .28s;
5
     transition: margin-left .28s;
7
     margin-left: 180px;
6
     margin-left: 180px;
8
   }
7
   }
9
-
10
-   // 侧边栏
8
+  // 侧边栏
11
   .sidebar-container {
9
   .sidebar-container {
12
-    .horizontal-collapse-transition {
13
-      transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out;
14
-    }
15
-    transition: width .28s;
10
+    transition: width 0.28s;
16
     width: 180px !important;
11
     width: 180px !important;
17
     height: 100%;
12
     height: 100%;
18
     position: fixed;
13
     position: fixed;
@@ -22,19 +17,33 @@
22
     left: 0;
17
     left: 0;
23
     z-index: 1001;
18
     z-index: 1001;
24
     overflow: hidden;
19
     overflow: hidden;
20
+    //reset element-ui css
21
+    .horizontal-collapse-transition {
22
+      transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out;
23
+    }
24
+    .scrollbar-wrapper {
25
+      height: calc(100% + 15px);
26
+      .el-scrollbar__view {
27
+        height: 100%;
28
+      }
29
+    }
30
+    .is-horizontal {
31
+      display: none;
32
+    }
25
     a {
33
     a {
26
       display: inline-block;
34
       display: inline-block;
27
       width: 100%;
35
       width: 100%;
36
+      overflow: hidden;
28
     }
37
     }
29
     .svg-icon {
38
     .svg-icon {
30
       margin-right: 16px;
39
       margin-right: 16px;
31
     }
40
     }
32
     .el-menu {
41
     .el-menu {
33
       border: none;
42
       border: none;
43
+      height: 100%;
34
       width: 100% !important;
44
       width: 100% !important;
35
     }
45
     }
36
   }
46
   }
37
-
38
   .hideSidebar {
47
   .hideSidebar {
39
     .sidebar-container {
48
     .sidebar-container {
40
       width: 36px !important;
49
       width: 36px !important;
@@ -50,22 +59,28 @@
50
       }
59
       }
51
     }
60
     }
52
     .el-submenu {
61
     .el-submenu {
62
+      overflow: hidden;
53
       &>.el-submenu__title {
63
       &>.el-submenu__title {
54
         padding-left: 10px !important;
64
         padding-left: 10px !important;
55
-        &>span {
56
-          height: 0;
57
-          width: 0;
58
-          overflow: hidden;
59
-          visibility: hidden;
60
-          display: inline-block;
61
-        }
62
         .el-submenu__icon-arrow {
65
         .el-submenu__icon-arrow {
63
           display: none;
66
           display: none;
64
         }
67
         }
65
       }
68
       }
66
     }
69
     }
70
+    .el-menu--collapse {
71
+      .el-submenu {
72
+        &>.el-submenu__title {
73
+          &>span {
74
+            height: 0;
75
+            width: 0;
76
+            overflow: hidden;
77
+            visibility: hidden;
78
+            display: inline-block;
79
+          }
80
+        }
81
+      }
82
+    }
67
   }
83
   }
68
-
69
   .sidebar-container .nest-menu .el-submenu>.el-submenu__title,
84
   .sidebar-container .nest-menu .el-submenu>.el-submenu__title,
70
   .sidebar-container .el-submenu .el-menu-item {
85
   .sidebar-container .el-submenu .el-menu-item {
71
     min-width: 180px !important;
86
     min-width: 180px !important;
@@ -84,7 +99,6 @@
84
       margin-left: 0px;
99
       margin-left: 0px;
85
     }
100
     }
86
     .sidebar-container {
101
     .sidebar-container {
87
-      top: 50px;
88
       transition: transform .28s;
102
       transition: transform .28s;
89
       width: 180px !important;
103
       width: 180px !important;
90
     }
104
     }
@@ -95,7 +109,6 @@
95
       }
109
       }
96
     }
110
     }
97
   }
111
   }
98
-
99
   .withoutAnimation {
112
   .withoutAnimation {
100
     .main-container,
113
     .main-container,
101
     .sidebar-container {
114
     .sidebar-container {

+ 15 - 0
src/views/layout/Layout.vue

@@ -1,5 +1,6 @@
1
 <template>
1
 <template>
2
   <div class="app-wrapper" :class="classObj">
2
   <div class="app-wrapper" :class="classObj">
3
+    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"></div>
3
     <sidebar class="sidebar-container"></sidebar>
4
     <sidebar class="sidebar-container"></sidebar>
4
     <div class="main-container">
5
     <div class="main-container">
5
       <navbar></navbar>
6
       <navbar></navbar>
@@ -34,6 +35,11 @@ export default {
34
         mobile: this.device === 'mobile'
35
         mobile: this.device === 'mobile'
35
       }
36
       }
36
     }
37
     }
38
+  },
39
+  methods: {
40
+    handleClickOutside() {
41
+      this.$store.dispatch('CloseSideBar', { withoutAnimation: false })
42
+    }
37
   }
43
   }
38
 }
44
 }
39
 </script>
45
 </script>
@@ -46,4 +52,13 @@ export default {
46
     height: 100%;
52
     height: 100%;
47
     width: 100%;
53
     width: 100%;
48
   }
54
   }
55
+  .drawer-bg {
56
+    background: #000;
57
+    opacity: 0.3;
58
+    width: 100%;
59
+    top: 0;
60
+    height: 100%;
61
+    position: absolute;
62
+    z-index: 999;
63
+  }
49
 </style>
64
 </style>

+ 3 - 4
src/views/layout/components/Sidebar/index.vue

@@ -1,5 +1,5 @@
1
 <template>
1
 <template>
2
-  <scroll-bar>
2
+  <el-scrollbar wrapClass="scrollbar-wrapper">
3
     <el-menu
3
     <el-menu
4
       mode="vertical"
4
       mode="vertical"
5
       :show-timeout="200"
5
       :show-timeout="200"
@@ -11,16 +11,15 @@
11
     >
11
     >
12
       <sidebar-item :routes="routes"></sidebar-item>
12
       <sidebar-item :routes="routes"></sidebar-item>
13
     </el-menu>
13
     </el-menu>
14
-  </scroll-bar>
14
+  </el-scrollbar>
15
 </template>
15
 </template>
16
 
16
 
17
 <script>
17
 <script>
18
 import { mapGetters } from 'vuex'
18
 import { mapGetters } from 'vuex'
19
 import SidebarItem from './SidebarItem'
19
 import SidebarItem from './SidebarItem'
20
-import ScrollBar from '@/components/ScrollBar'
21
 
20
 
22
 export default {
21
 export default {
23
-  components: { SidebarItem, ScrollBar },
22
+  components: { SidebarItem },
24
   computed: {
23
   computed: {
25
     ...mapGetters([
24
     ...mapGetters([
26
       'sidebar'
25
       'sidebar'