Explorar el Código

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

花裤衩 hace 6 años
padre
commit
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 16
         Cookies.set('sidebarStatus', 0)
17 17
       }
18 18
       state.sidebar.opened = !state.sidebar.opened
19
+      state.sidebar.withoutAnimation = false
19 20
     },
20 21
     CLOSE_SIDEBAR: (state, withoutAnimation) => {
21 22
       Cookies.set('sidebarStatus', 1)

+ 20 - 1
src/styles/index.scss

@@ -5,26 +5,45 @@
5 5
 @import './sidebar.scss';
6 6
 
7 7
 body {
8
+  height: 100%;
8 9
   -moz-osx-font-smoothing: grayscale;
9 10
   -webkit-font-smoothing: antialiased;
10 11
   text-rendering: optimizeLegibility;
11 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 19
 html {
20
+  height: 100%;
15 21
   box-sizing: border-box;
16 22
 }
17 23
 
24
+#app{
25
+  height: 100%;
26
+}
27
+
18 28
 *,
19 29
 *:before,
20 30
 *:after {
21 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 39
   outline: none;
40
+  text-decoration: none;
26 41
 }
27 42
 
43
+div:focus{
44
+  outline: none;
45
+ }
46
+
28 47
 a:focus,
29 48
 a:active {
30 49
   outline: none;

+ 31 - 18
src/styles/sidebar.scss

@@ -1,18 +1,13 @@
1 1
 #app {
2
-
3 2
   // 主体区域
4 3
   .main-container {
5 4
     min-height: 100%;
6 5
     transition: margin-left .28s;
7 6
     margin-left: 180px;
8 7
   }
9
-
10
-   // 侧边栏
8
+  // 侧边栏
11 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 11
     width: 180px !important;
17 12
     height: 100%;
18 13
     position: fixed;
@@ -22,19 +17,33 @@
22 17
     left: 0;
23 18
     z-index: 1001;
24 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 33
     a {
26 34
       display: inline-block;
27 35
       width: 100%;
36
+      overflow: hidden;
28 37
     }
29 38
     .svg-icon {
30 39
       margin-right: 16px;
31 40
     }
32 41
     .el-menu {
33 42
       border: none;
43
+      height: 100%;
34 44
       width: 100% !important;
35 45
     }
36 46
   }
37
-
38 47
   .hideSidebar {
39 48
     .sidebar-container {
40 49
       width: 36px !important;
@@ -50,22 +59,28 @@
50 59
       }
51 60
     }
52 61
     .el-submenu {
62
+      overflow: hidden;
53 63
       &>.el-submenu__title {
54 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 65
         .el-submenu__icon-arrow {
63 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 84
   .sidebar-container .nest-menu .el-submenu>.el-submenu__title,
70 85
   .sidebar-container .el-submenu .el-menu-item {
71 86
     min-width: 180px !important;
@@ -84,7 +99,6 @@
84 99
       margin-left: 0px;
85 100
     }
86 101
     .sidebar-container {
87
-      top: 50px;
88 102
       transition: transform .28s;
89 103
       width: 180px !important;
90 104
     }
@@ -95,7 +109,6 @@
95 109
       }
96 110
     }
97 111
   }
98
-
99 112
   .withoutAnimation {
100 113
     .main-container,
101 114
     .sidebar-container {

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

@@ -1,5 +1,6 @@
1 1
 <template>
2 2
   <div class="app-wrapper" :class="classObj">
3
+    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"></div>
3 4
     <sidebar class="sidebar-container"></sidebar>
4 5
     <div class="main-container">
5 6
       <navbar></navbar>
@@ -34,6 +35,11 @@ export default {
34 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 45
 </script>
@@ -46,4 +52,13 @@ export default {
46 52
     height: 100%;
47 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 64
 </style>

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

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