component.vue
6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<template>
<el-container>
<layout-header></layout-header>
<el-container class="layout-container__component">
<el-aside class="layout-aside__component" width="200px">
<el-menu :default-active="activeMenu" class="layout-aside-menu__component" @select="handleSelect">
<h4 class="side-menu__group">开发指南</h4>
<el-menu-item v-for="(doc, index) in guideList" :key="`doc-${index}`" :index="doc.name">{{ doc.meta.title }}</el-menu-item>
<h4 class="side-menu__group">
组件<el-tag type="info" size="mini">{{ componentsCount }}</el-tag>
</h4>
<el-menu-item-group v-for="(component, idx) in componentList" :key="idx">
<template slot="title">{{ component.group }}</template>
<el-menu-item v-for="(data, index) in component.children" :key="index" :index="data.name">{{ data.meta.title }}</el-menu-item>
</el-menu-item-group>
</el-menu>
</el-aside>
<el-main class="layout-main__component">
<router-view></router-view>
</el-main>
<el-aside class="layout-aside__preview" width="200px">
<a class="anchor" :class="{ 'active': item.hash === currentAnchor }" v-for="(item, index) in anchorList" :key="index" @click="jumpAnchor(item)">{{ item.text }}</a>
</el-aside>
</el-container>
</el-container>
</template>
<script>
import LayoutHeader from './components/header';
import { guides, components } from '@/router/routes';
const headerHeight = 80;
export default {
name: 'componentLayout',
components: { LayoutHeader },
data() {
return {
activeMenu: 'introduce',
guideList: guides,
componentList: components,
anchorList: [],
currentAnchor: '',
}
},
created() {
const { name } = this.$route || {};
this.activeMenu = name;
},
mounted() {
this.initAnchorList();
window.onscroll = (e) => {
let rangeIndex = 0;
for (let index in this.anchorList) {
let anchor = this.anchorList[index] || {};
const dom = document.querySelector(anchor.hash) || { offsetTop: 0 };
if (window.pageYOffset >= dom.offsetTop - headerHeight) {
rangeIndex = index
}
}
this.currentAnchor = this.anchorList[rangeIndex].hash;
};
// 初次加载时根据url hash跳转到指定锚点
if (this.$route.hash) {
const anchor = document.querySelector(this.$route.hash);
document.documentElement.scrollTop = anchor.offsetTop - headerHeight;
}
},
watch:{
$route(to, from) {
this.currentAnchor = to.hash;
if (to.path !== from.path) {
this.$nextTick(this.initAnchorList);
}
}
},
computed: {
componentsCount() {
let result = 0;
this.componentList.forEach(item => {
result += (item.children || []).length;
});
return result;
}
},
methods: {
isBottom() {
// 变量scrollTop是滚动条滚动时,距离顶部的距离
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 变量windowHeight是可视区的高度
const windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
// 变量scrollHeight是滚动条的总高度
const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
// 滚动条到底部的条件
return scrollTop + windowHeight === scrollHeight;
},
initAnchorList() {
document.documentElement.scrollTop = 0;
this.anchorList = [];
const domList = document.querySelectorAll('.header-anchor');
let anchorList = [];
for (let index in domList) {
let dom = domList[index] || {};
if (dom.addEventListener) {
dom.addEventListener('click', e => {
// 阻止a标签的默认行为
e = e || window.event;
e.preventDefault();
document.documentElement.scrollTop = dom.offsetTop - headerHeight;
this.$router.replace({ path: this.$route.path, hash: dom.hash });
});
}
if (dom.parentNode) {
const text = `${dom.parentNode.innerHTML}`.replace(/<\/?.*[^>]*>/g, '').trim();
if (text === 'API') {
break;
} else {
anchorList.push({
text: `${dom.parentNode.innerHTML}`.replace(/<\/?.*[^>]*>/g, '').trim(),
href: dom.href,
hash: dom.hash,
});
}
}
}
this.anchorList = anchorList;
this.currentAnchor = (anchorList[0] || {}).hash;
},
jumpAnchor({ hash, offsetTop }) {
this.$router.replace({ path: this.$route.path, hash });
const dom = document.querySelector(hash) || { offsetTop: 0 };
document.documentElement.scrollTop = dom.offsetTop - headerHeight;
},
handleSelect(key) {
this.$router.push({ name: key });
}
}
}
</script>
<style lang="scss">
@import '@/styles/variables.scss';
.layout-container__component {
margin-top: 60px;
.layout-aside__component {
&::after {
position: absolute;
top: 50%;
right: 0;
content: '';
display: inline-block;
transform: translateY(-50%);
height: 90%;
border-right: 1px solid #e6e6e6;
}
left: 0;
position: fixed;
height: calc(100vh - 60px);
background: #fff;
.el-menu-item {
border-right: 3px solid rgba(0, 0, 0, 0);
}
.el-menu-item:hover {
color: $primary;
background-color: rgba(0, 0, 0, 0);
}
.el-menu-item.is-active {
background-color: rgba($primary, 0.1);
border-right: 3px solid $primary;
}
.el-menu-item, .el-submenu__title {
height: 40px;
line-height: 40px;
}
.layout-aside-menu__component {
background: transparent;
border-right: 0;
}
}
.layout-aside__preview {
position: fixed;
right: 0;
height: calc(100vh - 60px);
width: 150px !important;
padding: 30px 0;
.anchor {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 5px 20px;
color: $text;
transition: all 300ms;
border-left: 1px solid $border;
cursor: pointer;
}
.anchor:hover,
.active {
color: $primary;
border-left: 1px solid $primary;
}
}
.layout-main__component {
margin-left: 200px;
margin-right: 150px;
padding: 20px 40px;
}
}
</style>