BottomDock.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. position: fixed;
  195. left: 0;
  196. z-index: 9999;
  197. /* 🌟 核心修改 1:不用 transform,直接把 bottom 设为负数藏到底部外 */
  198. bottom: -140px;
  199. /* 🌟 核心修改 2:过渡动画改为监听 bottom 属性 */
  200. transition: bottom 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  201. background: linear-gradient(to top, rgba(0, 20, 30, 0.8) 0%, rgba(0, 20, 30, 0.01) 100%);
  202. pointer-events: auto !important;
  203. }
  204. /* 🌟 核心修改 3:鼠标悬浮时,bottom 归零,完美升起 */
  205. .dock-wrapper:hover {
  206. bottom: 0;
  207. }
  208. /* === 顶部的发光提示线(保持不变,加了一个穿透属性防误触) === */
  209. .dock-wrapper::before {
  210. content: '';
  211. position: absolute;
  212. top: 0;
  213. left: 50%;
  214. transform: translateX(-50%);
  215. width: 200px;
  216. height: 3px;
  217. background: rgba(0, 229, 255, 0.6);
  218. box-shadow: 0 0 10px rgba(0, 229, 255, 0.8);
  219. border-radius: 4px;
  220. transition: opacity 0.3s;
  221. pointer-events: none; /* 防止这根线挡住鼠标划入的判定 */
  222. }
  223. /* 展开的时候让提示线消失 */
  224. .dock-wrapper:hover::before {
  225. opacity: 0;
  226. }
  227. /* 视窗容器:定宽,超出部分隐藏并允许横向滚动
  228. 【精算容器宽度】:
  229. 要想完美不漏边,容器宽度必须等于:(显示个数 * 元素宽度) + ((显示个数 - 1) * 间距)
  230. 假设你想完美显示 6 个:(6 * 100px) + (5 * 30px) = 750px
  231. */
  232. .dock-list-container {
  233. width: 750px;
  234. height: 160px;
  235. overflow-x: auto;
  236. overflow-y: hidden;
  237. white-space: nowrap;
  238. /* 2. 强制内部文本/元素绝对不换行 */
  239. -ms-overflow-style: none;
  240. scrollbar-width: none;
  241. }
  242. .dock-list-container::-webkit-scrollbar {
  243. display: none;
  244. }
  245. /* 真实的菜单列表:尺寸由内容撑开 */
  246. .dock-list {
  247. display: flex !important;
  248. flex-direction: row !important;
  249. /* 3. 菜单项强制水平排列 */
  250. flex-wrap: nowrap !important;
  251. /* 4. 绝对不允许换行 */
  252. align-items: flex-end;
  253. /* 底部对齐 */
  254. height: 100%;
  255. width: max-content;
  256. /* 5. 关键:真实宽度由内部项决定,从而完美触发父级滚动 */
  257. min-width: 100%;
  258. padding-bottom: 20px;
  259. gap: 30px;
  260. /* 图标之间的横向间距 */
  261. }
  262. /* ================= 左右控制箭头 ================= */
  263. /* 容器基础样式 */
  264. .nav-arrow {
  265. flex-shrink: 0;
  266. width: 40px;
  267. height: 40px;
  268. display: flex;
  269. justify-content: center;
  270. align-items: center;
  271. transition: all 0.3s;
  272. margin: 0 15px;
  273. /* 【修改】:去掉之前的底色和边框,因为你现在用整张切图了 */
  274. background: transparent !important;
  275. border: none !important;
  276. }
  277. .arrow-img {
  278. width: 100%;
  279. height: 100%;
  280. object-fit: contain;
  281. /* transition: transform 0.2s ease, filter 0.3s ease; */
  282. }
  283. /* 强制让左侧的图片掉头指向上一个 */
  284. .left-facing {
  285. transform: rotate(180deg);
  286. }
  287. /* 状态2:允许切换时的悬浮放大效果 */
  288. .nav-arrow.is-active {
  289. cursor: pointer;
  290. }
  291. .nav-arrow.is-active:hover .arrow-img {
  292. transform: scale(1.1);
  293. filter: drop-shadow(0 0 10px rgba(0, 229, 255, 0.8));
  294. }
  295. /* 【关键修复】:由于左箭头自带 rotate(180deg),悬浮放大时不能丢掉它的旋转角度,否则它会瞬间掉头! */
  296. .nav-arrow.left-arrow.is-active:hover .arrow-img.left-facing {
  297. transform: rotate(180deg) scale(1.1);
  298. }
  299. /* 状态1:不允许切换时稍微变暗 */
  300. .nav-arrow.is-disabled {
  301. cursor: not-allowed;
  302. opacity: 0.6;
  303. }
  304. /* ================= 单个导航项 ================= */
  305. .dock-item {
  306. flex-shrink: 0;
  307. /* 7. 关键:禁止菜单项自身被挤压 */
  308. position: relative;
  309. display: flex;
  310. flex-direction: column;
  311. /* 菜单项内部(图标+文字)是上下垂直排布的 */
  312. align-items: center;
  313. justify-content: flex-end;
  314. cursor: pointer;
  315. width: 100px;
  316. height: 90px;
  317. transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  318. }
  319. /* --- 图标部分 --- */
  320. .item-icon {
  321. font-size: 32px;
  322. color: #a0cfff;
  323. z-index: 3;
  324. margin-bottom: 5px;
  325. transition: all 0.3s;
  326. }
  327. /* --- 文字部分 --- */
  328. .item-label {
  329. color: #c0c4cc;
  330. font-size: 14px;
  331. text-align: center;
  332. transition: all 0.3s;
  333. letter-spacing: 1px;
  334. }
  335. /* ================= 交互状态:悬浮与选中 ================= */
  336. .dock-item:hover,
  337. .dock-item.is-active {
  338. transform: translateY(-15px) scale(1.15);
  339. }
  340. .dock-item:hover .item-icon,
  341. .dock-item.is-active .item-icon {
  342. color: #ffffff;
  343. text-shadow: 0 0 15px #00e5ff;
  344. }
  345. .dock-item:hover .item-label,
  346. .dock-item.is-active .item-label {
  347. color: #ffffff;
  348. font-weight: bold;
  349. text-shadow: 0 0 8px #00e5ff;
  350. }
  351. .dock-item:hover .top-solid,
  352. .dock-item.is-active .top-solid {
  353. background: linear-gradient(135deg, rgba(0, 229, 255, 0.9), rgba(0, 115, 255, 0.9));
  354. box-shadow: 0 0 20px rgba(0, 229, 255, 0.6);
  355. }
  356. .dock-item.theme-gold.is-active .item-icon {
  357. color: #ffd700;
  358. text-shadow: 0 0 15px #ffaa00;
  359. }
  360. .dock-item.theme-gold.is-active .item-label {
  361. color: #ffd700;
  362. text-shadow: 0 0 8px #ffaa00;
  363. }
  364. /* --- 图标部分 --- */
  365. .item-icon {
  366. z-index: 3;
  367. margin-bottom: 5px;
  368. /* 确保图片也能有悬浮时的丝滑过渡 */
  369. transition: transform 0.3s ease, filter 0.3s ease;
  370. }
  371. /* 专门针对图片的样式 */
  372. .custom-icon {
  373. width: 88px;
  374. /* 根据你切图的实际大小调整 */
  375. height: 64px;
  376. object-fit: contain;
  377. /* 保证图片不变形 */
  378. }
  379. /* 悬浮时,如果想让图片也发光,可以使用 CSS 的 drop-shadow 滤镜 */
  380. .dock-item:hover .custom-icon,
  381. .dock-item.is-active .custom-icon {
  382. filter: drop-shadow(0 0 8px rgba(0, 229, 255, 0.8));
  383. }
  384. </style>