BottomDock.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <div class="dock-wrapper">
  3. <div class="nav-arrow left-arrow" :class="{ 'is-disabled': !canScrollLeft, 'is-active': canScrollLeft }"
  4. @click="scrollList(-1)">
  5. <img v-if="canScrollLeft" src="@/assets/main/main-right.png" class="arrow-img left-facing" />
  6. <img v-else src="@/assets/main/main-left.png" class="arrow-img" />
  7. </div>
  8. <div class="dock-list-container" ref="listContainer" @scroll="checkScrollState">
  9. <div class="dock-list">
  10. <div v-for="(item, index) in dockItems" :key="index" class="dock-item"
  11. :class="{ 'is-active': activeIndex === index, [`theme-${item.theme}`]: item.theme}"
  12. @click="handleSelect(index, item)"
  13. @mouseenter="hoverIndex = index"
  14. @mouseleave="hoverIndex = null"
  15. >
  16. <div class="item-icon">
  17. <img v-if="item.imgUrl"
  18. :src="(activeIndex === index || hoverIndex === index) && item.activeImgUrl ? item.activeImgUrl : item.imgUrl"
  19. class="custom-icon" />
  20. <i v-else :class="item.iconClass"></i>
  21. </div>
  22. <div class="item-label">{{ item.label }}</div>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="nav-arrow right-arrow" :class="{ 'is-disabled': !canScrollRight, 'is-active': canScrollRight }"
  27. @click="scrollList(1)">
  28. <img v-if="canScrollRight" src="@/assets/main/main-right.png" class="arrow-img" />
  29. <img v-else src="@/assets/main/main-left.png" class="arrow-img left-facing" />
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. name: 'BottomDock',
  36. data() {
  37. return {
  38. activeIndex: 0,
  39. hoverIndex: null,
  40. canScrollLeft: false, // 初始默认在最左侧,所以左侧不能滚
  41. canScrollRight: true, // 初始默认右侧有内容,可以滚
  42. dockItems: [
  43. /* ... 你的菜单数据 ... */
  44. {
  45. label: '首页',
  46. imgUrl: require('@/assets/main/main-home.png'),
  47. activeImgUrl: require('@/assets/main/main-home-hover.png'),
  48. route: '/home',
  49. theme: 'blue',
  50. },
  51. {
  52. label: '状态监控',
  53. imgUrl: require('@/assets/main/main-surve.png'),
  54. activeImgUrl: require('@/assets/main/main-surve-hover.png'),
  55. route: '/surve',
  56. theme: 'blue',
  57. },
  58. {
  59. label: '特勤安保',
  60. imgUrl: require('@/assets/main/main-security.png'),
  61. activeImgUrl: require('@/assets/main/main-security-hover.png'),
  62. route: '/security',
  63. theme: 'gold',
  64. },
  65. {
  66. label: '干线协调',
  67. imgUrl: require('@/assets/main/main-coor.png'),
  68. activeImgUrl: require('@/assets/main/main-coor-hover.png'),
  69. route: '/coor',
  70. theme: 'blue',
  71. },
  72. {
  73. label: '数据分析',
  74. imgUrl: require('@/assets/main/main-watch.png'),
  75. activeImgUrl: require('@/assets/main/main-watch-hover.png'),
  76. route: '/watch',
  77. theme: 'blue',
  78. },
  79. {
  80. label: '系统设置',
  81. imgUrl: require('@/assets/main/main-setting.png'),
  82. activeImgUrl: require('@/assets/main/main-setting-hover.png'),
  83. route: '/setting',
  84. theme: 'blue',
  85. },
  86. // (建议多加几个测试数据,撑爆容器宽度,才能看到滚动和状态切换)
  87. {
  88. label: '测试1',
  89. imgUrl: require('@/assets/main/main-home.png'),
  90. theme: 'blue',
  91. },
  92. {
  93. label: '测试2',
  94. imgUrl: require('@/assets/main/main-surve.png'),
  95. theme: 'blue',
  96. },
  97. {
  98. label: '测试3',
  99. imgUrl: require('@/assets/main/main-security.png'),
  100. theme: 'blue',
  101. },
  102. ]
  103. };
  104. },
  105. watch: {
  106. $route() {
  107. this.updateActiveIndexByRoute();
  108. }
  109. },
  110. created() {
  111. this.updateActiveIndexByRoute();
  112. },
  113. mounted() {
  114. // 组件挂载后,等 DOM 渲染完毕,立即计算一次初始状态
  115. this.$nextTick(() => {
  116. this.checkScrollState();
  117. // 监听窗口大小变化,防止屏幕拉伸导致容器尺寸变化
  118. window.addEventListener('resize', this.checkScrollState);
  119. });
  120. },
  121. beforeDestroy() {
  122. // 销毁时移除监听
  123. window.removeEventListener('resize', this.checkScrollState);
  124. },
  125. methods: {
  126. updateActiveIndexByRoute() {
  127. const currentPath = this.$route.path;
  128. console.log(currentPath)
  129. // 查找当前路由路径匹配的菜单项索引
  130. const matchIndex = this.dockItems.findIndex(item => {
  131. // 精确匹配:item.route === currentPath
  132. // 模糊匹配(推荐):当前路径包含菜单的路由(解决含有子路由 /monitor/detail 时底部也高亮的问题)
  133. return item.route && currentPath.startsWith(item.route);
  134. });
  135. // 如果找到了对应的菜单,就更新 activeIndex
  136. if (matchIndex !== -1) {
  137. this.activeIndex = matchIndex;
  138. }
  139. },
  140. handleSelect(index, item) {
  141. if (this.activeIndex === index) return;
  142. this.activeIndex = index;
  143. // 【新增路由跳转逻辑】
  144. if (item.route) {
  145. // 使用 catch 拦截重复点击同一个路由时的 NavigationDuplicated 报错
  146. this.$router.push(item.route).catch(err => {
  147. if (err.name !== 'NavigationDuplicated') {
  148. console.error('路由跳转失败:', err);
  149. }
  150. });
  151. }
  152. this.$emit('change', item);
  153. },
  154. // 【新增核心方法】:检查并更新滚动状态
  155. checkScrollState() {
  156. const container = this.$refs.listContainer;
  157. if (!container) return;
  158. // 1. 如果 scrollLeft 大于 0,说明不在最左边,左侧按钮可点击 (状态1)
  159. this.canScrollLeft = container.scrollLeft > 0;
  160. // 2. 如果 scrollLeft + 可视宽度 < 实际总宽度,说明右侧还没滚到底,右侧按钮可点击 (状态1)
  161. // 使用 Math.ceil 是为了防止部分高分屏下出现小数像素导致的精度误差
  162. this.canScrollRight = Math.ceil(container.scrollLeft + container.clientWidth) < container.scrollWidth;
  163. },
  164. scrollList(direction) {
  165. // 如果处于不可滚动状态(状态2),直接 return,不执行滚动
  166. if (direction === -1 && !this.canScrollLeft) return;
  167. if (direction === 1 && !this.canScrollRight) return;
  168. const container = this.$refs.listContainer;
  169. // 1 个图标宽度 100px + 右侧间距 30px = 130px
  170. const scrollAmount = 130; // 调整每次滑动的距离
  171. if (container) {
  172. container.scrollBy({
  173. left: direction * scrollAmount,
  174. behavior: 'smooth'
  175. });
  176. // 这里的平滑滚动会自动触发上面绑定的 @scroll 事件,
  177. // 从而实时调用 checkScrollState 更新按钮状态
  178. }
  179. }
  180. }
  181. };
  182. </script>
  183. <style scoped>
  184. /* ================= 整体容器布局 (修复垂直排列的核心) ================= */
  185. .dock-wrapper {
  186. display: flex !important;
  187. flex-direction: row !important;
  188. /* 1. 强制左右水平排列 */
  189. align-items: center;
  190. justify-content: center;
  191. width: 100%;
  192. height: 160px;
  193. position: relative;
  194. }
  195. /* 视窗容器:定宽,超出部分隐藏并允许横向滚动
  196. 【精算容器宽度】:
  197. 要想完美不漏边,容器宽度必须等于:(显示个数 * 元素宽度) + ((显示个数 - 1) * 间距)
  198. 假设你想完美显示 6 个:(6 * 100px) + (5 * 30px) = 750px
  199. */
  200. .dock-list-container {
  201. width: 750px;
  202. height: 160px;
  203. overflow-x: auto;
  204. overflow-y: hidden;
  205. white-space: nowrap;
  206. /* 2. 强制内部文本/元素绝对不换行 */
  207. -ms-overflow-style: none;
  208. scrollbar-width: none;
  209. }
  210. .dock-list-container::-webkit-scrollbar {
  211. display: none;
  212. }
  213. /* 真实的菜单列表:尺寸由内容撑开 */
  214. .dock-list {
  215. display: flex !important;
  216. flex-direction: row !important;
  217. /* 3. 菜单项强制水平排列 */
  218. flex-wrap: nowrap !important;
  219. /* 4. 绝对不允许换行 */
  220. align-items: flex-end;
  221. /* 底部对齐 */
  222. height: 100%;
  223. width: max-content;
  224. /* 5. 关键:真实宽度由内部项决定,从而完美触发父级滚动 */
  225. min-width: 100%;
  226. padding-bottom: 20px;
  227. gap: 30px;
  228. /* 图标之间的横向间距 */
  229. }
  230. /* ================= 左右控制箭头 ================= */
  231. /* 容器基础样式 */
  232. .nav-arrow {
  233. flex-shrink: 0;
  234. width: 40px;
  235. height: 40px;
  236. display: flex;
  237. justify-content: center;
  238. align-items: center;
  239. transition: all 0.3s;
  240. margin: 0 15px;
  241. /* 【修改】:去掉之前的底色和边框,因为你现在用整张切图了 */
  242. background: transparent !important;
  243. border: none !important;
  244. }
  245. .arrow-img {
  246. width: 100%;
  247. height: 100%;
  248. object-fit: contain;
  249. /* transition: transform 0.2s ease, filter 0.3s ease; */
  250. }
  251. /* 强制让左侧的图片掉头指向上一个 */
  252. .left-facing {
  253. transform: rotate(180deg);
  254. }
  255. /* 状态2:允许切换时的悬浮放大效果 */
  256. .nav-arrow.is-active {
  257. cursor: pointer;
  258. }
  259. .nav-arrow.is-active:hover .arrow-img {
  260. transform: scale(1.1);
  261. filter: drop-shadow(0 0 10px rgba(0, 229, 255, 0.8));
  262. }
  263. /* 【关键修复】:由于左箭头自带 rotate(180deg),悬浮放大时不能丢掉它的旋转角度,否则它会瞬间掉头! */
  264. .nav-arrow.left-arrow.is-active:hover .arrow-img.left-facing {
  265. transform: rotate(180deg) scale(1.1);
  266. }
  267. /* 状态1:不允许切换时稍微变暗 */
  268. .nav-arrow.is-disabled {
  269. cursor: not-allowed;
  270. opacity: 0.6;
  271. }
  272. /* ================= 单个导航项 ================= */
  273. .dock-item {
  274. flex-shrink: 0;
  275. /* 7. 关键:禁止菜单项自身被挤压 */
  276. position: relative;
  277. display: flex;
  278. flex-direction: column;
  279. /* 菜单项内部(图标+文字)是上下垂直排布的 */
  280. align-items: center;
  281. justify-content: flex-end;
  282. cursor: pointer;
  283. width: 100px;
  284. height: 90px;
  285. transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  286. }
  287. /* --- 图标部分 --- */
  288. .item-icon {
  289. font-size: 32px;
  290. color: #a0cfff;
  291. z-index: 3;
  292. margin-bottom: 5px;
  293. transition: all 0.3s;
  294. }
  295. /* --- 文字部分 --- */
  296. .item-label {
  297. color: #c0c4cc;
  298. font-size: 14px;
  299. text-align: center;
  300. transition: all 0.3s;
  301. letter-spacing: 1px;
  302. }
  303. /* ================= 交互状态:悬浮与选中 ================= */
  304. .dock-item:hover,
  305. .dock-item.is-active {
  306. transform: translateY(-15px) scale(1.15);
  307. }
  308. .dock-item:hover .item-icon,
  309. .dock-item.is-active .item-icon {
  310. color: #ffffff;
  311. text-shadow: 0 0 15px #00e5ff;
  312. }
  313. .dock-item:hover .item-label,
  314. .dock-item.is-active .item-label {
  315. color: #ffffff;
  316. font-weight: bold;
  317. text-shadow: 0 0 8px #00e5ff;
  318. }
  319. .dock-item:hover .top-solid,
  320. .dock-item.is-active .top-solid {
  321. background: linear-gradient(135deg, rgba(0, 229, 255, 0.9), rgba(0, 115, 255, 0.9));
  322. box-shadow: 0 0 20px rgba(0, 229, 255, 0.6);
  323. }
  324. .dock-item.theme-gold.is-active .item-icon {
  325. color: #ffd700;
  326. text-shadow: 0 0 15px #ffaa00;
  327. }
  328. .dock-item.theme-gold.is-active .item-label {
  329. color: #ffd700;
  330. text-shadow: 0 0 8px #ffaa00;
  331. }
  332. /* --- 图标部分 --- */
  333. .item-icon {
  334. z-index: 3;
  335. margin-bottom: 5px;
  336. /* 确保图片也能有悬浮时的丝滑过渡 */
  337. transition: transform 0.3s ease, filter 0.3s ease;
  338. }
  339. /* 专门针对图片的样式 */
  340. .custom-icon {
  341. width: 88px;
  342. /* 根据你切图的实际大小调整 */
  343. height: 64px;
  344. object-fit: contain;
  345. /* 保证图片不变形 */
  346. }
  347. /* 悬浮时,如果想让图片也发光,可以使用 CSS 的 drop-shadow 滤镜 */
  348. .dock-item:hover .custom-icon,
  349. .dock-item.is-active .custom-icon {
  350. filter: drop-shadow(0 0 8px rgba(0, 229, 255, 0.8));
  351. }
  352. </style>