portal-mega-panel-width-demo.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <title>门户大面板宽度 · 一级菜单较少时</title>
  7. <style>
  8. :root {
  9. --primary: #10b981;
  10. --primary-dark: #059669;
  11. --primary-bg: #ecfdf5;
  12. --header: #1ca851;
  13. --text: #0f172a;
  14. --muted: #64748b;
  15. --border: #e2e8f0;
  16. --bg: #f1f5f9;
  17. --panel-min: 720px;
  18. }
  19. * { box-sizing: border-box; }
  20. body {
  21. margin: 0;
  22. font-family: Inter, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  23. color: var(--text);
  24. background: #f8fafc;
  25. line-height: 1.5;
  26. }
  27. .page { padding: 20px 24px 48px; max-width: 1280px; margin: 0 auto; }
  28. h1 { font-size: 22px; margin: 0 0 6px; }
  29. .lead { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
  30. .note {
  31. background: #fffbeb;
  32. border: 1px solid #fde68a;
  33. color: #92400e;
  34. border-radius: 10px;
  35. padding: 10px 14px;
  36. font-size: 13px;
  37. margin-bottom: 16px;
  38. }
  39. .ok {
  40. background: var(--primary-bg);
  41. border: 1px solid #a7f3d0;
  42. color: #065f46;
  43. border-radius: 10px;
  44. padding: 10px 14px;
  45. font-size: 13px;
  46. margin-bottom: 16px;
  47. }
  48. .controls {
  49. display: flex;
  50. flex-wrap: wrap;
  51. gap: 10px 16px;
  52. align-items: center;
  53. margin-bottom: 16px;
  54. padding: 12px 14px;
  55. background: #fff;
  56. border: 1px solid var(--border);
  57. border-radius: 12px;
  58. }
  59. .controls label {
  60. font-size: 12px;
  61. color: var(--muted);
  62. font-weight: 600;
  63. margin-right: 4px;
  64. }
  65. .seg {
  66. display: inline-flex;
  67. gap: 6px;
  68. flex-wrap: wrap;
  69. }
  70. .seg button {
  71. border: 1px solid var(--border);
  72. background: #fff;
  73. border-radius: 999px;
  74. padding: 7px 12px;
  75. font-size: 13px;
  76. cursor: pointer;
  77. color: #334155;
  78. }
  79. .seg button.active {
  80. background: var(--primary);
  81. border-color: var(--primary);
  82. color: #fff;
  83. font-weight: 600;
  84. }
  85. .grid {
  86. display: grid;
  87. grid-template-columns: 1fr;
  88. gap: 16px;
  89. }
  90. .card {
  91. background: #fff;
  92. border: 1px solid var(--border);
  93. border-radius: 12px;
  94. overflow: hidden;
  95. }
  96. .card-head {
  97. display: flex;
  98. justify-content: space-between;
  99. align-items: flex-start;
  100. gap: 12px;
  101. padding: 12px 16px;
  102. border-bottom: 1px solid var(--border);
  103. background: #fafafa;
  104. }
  105. .card-head h2 {
  106. margin: 0 0 4px;
  107. font-size: 15px;
  108. }
  109. .card-head p {
  110. margin: 0;
  111. font-size: 12px;
  112. color: var(--muted);
  113. }
  114. .badge {
  115. flex-shrink: 0;
  116. font-size: 11px;
  117. font-weight: 700;
  118. padding: 4px 8px;
  119. border-radius: 999px;
  120. background: #e2e8f0;
  121. color: #475569;
  122. }
  123. .badge.bad { background: #fee2e2; color: #b91c1c; }
  124. .badge.good { background: #d1fae5; color: #047857; }
  125. .badge.rec { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
  126. .frame {
  127. height: 360px;
  128. background: var(--bg);
  129. position: relative;
  130. overflow: hidden;
  131. }
  132. .app {
  133. height: 100%;
  134. display: flex;
  135. flex-direction: column;
  136. font-size: 13px;
  137. }
  138. .top {
  139. position: relative;
  140. height: 56px;
  141. background: var(--header);
  142. display: flex;
  143. align-items: center;
  144. padding: 0 16px;
  145. flex-shrink: 0;
  146. color: #fff;
  147. z-index: 2;
  148. }
  149. .brand {
  150. font-weight: 700;
  151. font-size: 14px;
  152. flex-shrink: 0;
  153. margin-right: 12px;
  154. }
  155. .nav-slot {
  156. position: absolute;
  157. left: 50%;
  158. top: 0;
  159. height: 100%;
  160. transform: translateX(-50%);
  161. display: flex;
  162. align-items: stretch;
  163. justify-content: center;
  164. max-width: min(720px, calc(100% - 280px));
  165. }
  166. .l1 {
  167. display: flex;
  168. align-items: stretch;
  169. gap: 2px;
  170. height: 100%;
  171. }
  172. .l1-item {
  173. display: inline-flex;
  174. align-items: center;
  175. gap: 4px;
  176. padding: 0 14px;
  177. height: 100%;
  178. white-space: nowrap;
  179. border: none;
  180. border-bottom: 4px solid transparent;
  181. background: transparent;
  182. color: #fff;
  183. font-size: 13px;
  184. cursor: pointer;
  185. }
  186. .l1-item.on {
  187. font-weight: 600;
  188. background: rgba(255, 255, 255, 0.14);
  189. border-bottom-color: #fff;
  190. }
  191. .l1-item .caret { font-size: 10px; opacity: 0.75; }
  192. .end {
  193. margin-left: auto;
  194. display: flex;
  195. align-items: center;
  196. gap: 8px;
  197. flex-shrink: 0;
  198. z-index: 1;
  199. }
  200. .avatar {
  201. width: 28px;
  202. height: 28px;
  203. border-radius: 50%;
  204. background: linear-gradient(135deg, #6ee7b7, #34d399);
  205. border: 2px solid rgba(255, 255, 255, 0.4);
  206. }
  207. .main {
  208. flex: 1;
  209. position: relative;
  210. background: #eef2f7;
  211. overflow: hidden;
  212. }
  213. .page-fake {
  214. padding: 20px 24px;
  215. color: var(--muted);
  216. font-size: 13px;
  217. }
  218. .page-fake .bar {
  219. height: 10px;
  220. background: #fff;
  221. border: 1px solid var(--border);
  222. border-radius: 6px;
  223. margin-bottom: 10px;
  224. }
  225. .page-fake .bar.w2 { width: 70%; }
  226. .page-fake .bar.w3 { width: 48%; }
  227. .mask {
  228. position: absolute;
  229. inset: 0;
  230. background: rgba(15, 23, 42, 0.25);
  231. z-index: 3;
  232. }
  233. .panel {
  234. position: absolute;
  235. top: 0;
  236. z-index: 4;
  237. background: #fff;
  238. border: 1px solid var(--border);
  239. border-top: none;
  240. border-radius: 0 0 10px 10px;
  241. box-shadow: 0 14px 32px rgba(15, 23, 42, 0.12);
  242. max-height: calc(100% - 8px);
  243. overflow: auto;
  244. }
  245. .panel-body { padding: 12px 16px 16px; }
  246. .split {
  247. display: grid;
  248. grid-template-columns: 1fr 1fr;
  249. gap: 0 16px;
  250. }
  251. .pane + .pane {
  252. padding-left: 16px;
  253. border-left: 1px solid var(--border);
  254. }
  255. .row {
  256. display: grid;
  257. grid-template-columns: 88px 1fr;
  258. gap: 8px 12px;
  259. padding: 10px 0;
  260. border-bottom: 1px solid #f1f5f9;
  261. align-items: start;
  262. }
  263. .row:last-child { border-bottom: 0; }
  264. .l2 {
  265. font-size: 12px;
  266. font-weight: 500;
  267. color: #666;
  268. padding-top: 4px;
  269. }
  270. .links { display: flex; flex-direction: column; gap: 2px; }
  271. .links span {
  272. font-size: 13px;
  273. font-weight: 500;
  274. color: #0f172a;
  275. padding: 3px 0;
  276. }
  277. .links span.on { color: var(--primary-dark); font-weight: 600; }
  278. .measure {
  279. position: absolute;
  280. top: 8px;
  281. right: 10px;
  282. z-index: 5;
  283. font-size: 11px;
  284. font-weight: 700;
  285. color: #b45309;
  286. background: #fffbeb;
  287. border: 1px solid #fde68a;
  288. border-radius: 6px;
  289. padding: 3px 8px;
  290. pointer-events: none;
  291. }
  292. .measure.ok-m {
  293. color: #047857;
  294. background: #ecfdf5;
  295. border-color: #a7f3d0;
  296. }
  297. .pick {
  298. display: flex;
  299. gap: 10px;
  300. flex-wrap: wrap;
  301. margin-top: 16px;
  302. }
  303. .pick button {
  304. border: 1px solid var(--primary);
  305. background: var(--primary);
  306. color: #fff;
  307. border-radius: 8px;
  308. padding: 10px 16px;
  309. font-size: 13px;
  310. font-weight: 600;
  311. cursor: pointer;
  312. }
  313. .pick button.ghost {
  314. background: #fff;
  315. color: #334155;
  316. border-color: var(--border);
  317. }
  318. .pick button.alt {
  319. background: var(--primary-bg);
  320. color: var(--primary-dark);
  321. }
  322. </style>
  323. </head>
  324. <body>
  325. <div class="page">
  326. <h1>大面板宽度 Demo:一级菜单较少时</h1>
  327. <p class="lead">
  328. 现状:二级大面板宽度 = 一级菜单区域宽度。权限较少(仅 2 / 4 个一级)时,面板会跟着变窄,两栏二级排不开。
  329. </p>
  330. <div class="note">
  331. 先用上方开关模拟「一级数量」,再对比三种宽度策略。点选卡片或底部按钮告诉我偏好。
  332. </div>
  333. <div class="controls">
  334. <div>
  335. <label>一级菜单数量</label>
  336. <div class="seg" id="countSeg">
  337. <button type="button" data-count="2">2 个(窄权限)</button>
  338. <button type="button" data-count="4" class="active">4 个</button>
  339. <button type="button" data-count="7">7 个(接近满)</button>
  340. </div>
  341. </div>
  342. <div>
  343. <label>高亮模块</label>
  344. <div class="seg" id="activeSeg">
  345. <button type="button" class="active" data-active="0">第一个</button>
  346. <button type="button" data-active="1">第二个</button>
  347. </div>
  348. </div>
  349. </div>
  350. <div class="ok">
  351. 推荐倾向:<strong>方案 B</strong> — 面板有最小宽度(如 720px),相对一级区域居中;一级很多时仍可随区域变宽,但不超过顶栏中间槽位上限。
  352. </div>
  353. <div class="grid" id="cards"></div>
  354. <div class="pick">
  355. <button type="button" class="alt" data-choice="a">选 A · 跟一级同宽(现状)</button>
  356. <button type="button" data-choice="b">选 B · 最小宽度居中(推荐)</button>
  357. <button type="button" class="ghost" data-choice="c">选 C · 固定铺满中间槽</button>
  358. </div>
  359. </div>
  360. <script>
  361. const ALL_L1 = ['CRM', '仓储', '质检', '财务', '系统', '报表', '运营']
  362. const PANEL_ROWS_LEFT = [
  363. { l2: '客户管理', links: ['客户列表', '联系人', '公海客户'] },
  364. { l2: '商机报价', links: ['商机列表', '报价单', '价格函'] },
  365. { l2: '合同订单', links: ['合同审批', '订单跟踪'] },
  366. { l2: '拜访计划', links: ['拜访日历', '拜访记录'] },
  367. { l2: '基础资料', links: ['产品目录', '单位管理'] }
  368. ]
  369. const PANEL_ROWS_RIGHT = [
  370. { l2: '审批中心', links: ['待办审批', '已办审批'] },
  371. { l2: '设置', links: ['审批人设置', '业务参数'] }
  372. ]
  373. const strategies = [
  374. {
  375. id: 'a',
  376. title: 'A · 跟一级区域同宽(现状)',
  377. desc: '面板 left/width 完全等于一级 nav 的 getBoundingClientRect。一级少 → 面板窄。',
  378. badge: '现状问题',
  379. badgeClass: 'bad',
  380. calc(navRect, slotMax) {
  381. return { left: navRect.left, width: navRect.width }
  382. }
  383. },
  384. {
  385. id: 'b',
  386. title: 'B · 最小宽度 + 相对一级居中(推荐)',
  387. desc: 'width = max(一级宽度, min(720, 槽位上限)),相对一级中心对齐;一级很多时仍可更宽。',
  388. badge: '推荐',
  389. badgeClass: 'rec',
  390. calc(navRect, slotMax, viewportW) {
  391. const minW = Math.min(720, slotMax, viewportW - 32)
  392. const width = Math.max(navRect.width, minW)
  393. const center = navRect.left + navRect.width / 2
  394. let left = center - width / 2
  395. left = Math.max(16, Math.min(left, viewportW - width - 16))
  396. return { left, width }
  397. }
  398. },
  399. {
  400. id: 'c',
  401. title: 'C · 始终铺满顶栏中间槽位',
  402. desc: '面板宽度固定为中间槽 max-width(约 720),与一级数量无关,始终居中于顶栏。',
  403. badge: '固定宽',
  404. badgeClass: 'good',
  405. calc(navRect, slotMax, viewportW, slotRect) {
  406. const width = Math.min(720, slotMax, viewportW - 32)
  407. const center = slotRect.left + slotRect.width / 2
  408. let left = center - width / 2
  409. left = Math.max(16, Math.min(left, viewportW - width - 16))
  410. return { left, width }
  411. }
  412. }
  413. ]
  414. let l1Count = 4
  415. let activeIdx = 0
  416. function rowHtml(row) {
  417. return `<div class="row">
  418. <div class="l2">${row.l2}</div>
  419. <div class="links">${row.links.map((t, i) => `<span class="${i === 0 ? 'on' : ''}">${t}</span>`).join('')}</div>
  420. </div>`
  421. }
  422. function panelInner() {
  423. return `<div class="panel-body">
  424. <div class="split">
  425. <div class="pane">${PANEL_ROWS_LEFT.map(rowHtml).join('')}</div>
  426. <div class="pane">${PANEL_ROWS_RIGHT.map(rowHtml).join('')}</div>
  427. </div>
  428. </div>`
  429. }
  430. function render() {
  431. const cards = document.getElementById('cards')
  432. cards.innerHTML = strategies.map((s) => `
  433. <div class="card" data-choice="${s.id}" onclick="window.__pick('${s.id}')">
  434. <div class="card-head">
  435. <div>
  436. <h2>${s.title}</h2>
  437. <p>${s.desc}</p>
  438. </div>
  439. <span class="badge ${s.badgeClass}">${s.badge}</span>
  440. </div>
  441. <div class="frame" id="frame-${s.id}">
  442. <div class="app">
  443. <div class="top">
  444. <div class="brand">山峡化工</div>
  445. <div class="nav-slot" id="slot-${s.id}">
  446. <div class="l1" id="nav-${s.id}">
  447. ${ALL_L1.slice(0, l1Count).map((name, i) => `
  448. <button type="button" class="l1-item ${i === activeIdx ? 'on' : ''}">
  449. <span>${name}</span><span class="caret">▾</span>
  450. </button>
  451. `).join('')}
  452. </div>
  453. </div>
  454. <div class="end"><div class="avatar"></div></div>
  455. </div>
  456. <div class="main" id="main-${s.id}">
  457. <div class="page-fake">
  458. <div class="bar"></div>
  459. <div class="bar w2"></div>
  460. <div class="bar w3"></div>
  461. <p>内容区(示意)</p>
  462. </div>
  463. <div class="mask"></div>
  464. <div class="panel" id="panel-${s.id}">${panelInner()}</div>
  465. <div class="measure" id="measure-${s.id}"></div>
  466. </div>
  467. </div>
  468. </div>
  469. </div>
  470. `).join('')
  471. requestAnimationFrame(layoutPanels)
  472. }
  473. function layoutPanels() {
  474. strategies.forEach((s) => {
  475. const frame = document.getElementById(`frame-${s.id}`)
  476. const nav = document.getElementById(`nav-${s.id}`)
  477. const slot = document.getElementById(`slot-${s.id}`)
  478. const main = document.getElementById(`main-${s.id}`)
  479. const panel = document.getElementById(`panel-${s.id}`)
  480. const measure = document.getElementById(`measure-${s.id}`)
  481. if (!frame || !nav || !slot || !main || !panel) return
  482. const frameRect = frame.getBoundingClientRect()
  483. const navRect = nav.getBoundingClientRect()
  484. const slotRect = slot.getBoundingClientRect()
  485. const mainRect = main.getBoundingClientRect()
  486. const slotMax = Math.min(720, frameRect.width - 280)
  487. const localNav = {
  488. left: navRect.left - mainRect.left,
  489. width: navRect.width
  490. }
  491. const localSlot = {
  492. left: slotRect.left - mainRect.left,
  493. width: slotRect.width
  494. }
  495. const viewportW = mainRect.width
  496. const pos = s.calc(localNav, slotMax, viewportW, localSlot)
  497. panel.style.left = `${Math.round(pos.left)}px`
  498. panel.style.width = `${Math.round(pos.width)}px`
  499. const narrow = pos.width < 520
  500. measure.textContent = `面板宽 ≈ ${Math.round(pos.width)}px · 一级区 ≈ ${Math.round(navRect.width)}px`
  501. measure.className = `measure ${narrow ? '' : 'ok-m'}`
  502. })
  503. }
  504. window.__pick = function (id) {
  505. const names = { a: 'A 现状同宽', b: 'B 最小宽度居中', c: 'C 固定铺满中间槽' }
  506. alert(`已记录偏好:${names[id] || id}\n\n请在对话里回复确认,或继续改参数再看。`)
  507. if (typeof toggleSelect === 'function') {
  508. const el = document.querySelector(`[data-choice="${id}"]`)
  509. if (el) toggleSelect(el)
  510. }
  511. }
  512. document.getElementById('countSeg').addEventListener('click', (e) => {
  513. const btn = e.target.closest('button[data-count]')
  514. if (!btn) return
  515. l1Count = Number(btn.dataset.count)
  516. document.querySelectorAll('#countSeg button').forEach((b) => b.classList.toggle('active', b === btn))
  517. if (activeIdx >= l1Count) activeIdx = 0
  518. render()
  519. })
  520. document.getElementById('activeSeg').addEventListener('click', (e) => {
  521. const btn = e.target.closest('button[data-active]')
  522. if (!btn) return
  523. activeIdx = Math.min(Number(btn.dataset.active), l1Count - 1)
  524. document.querySelectorAll('#activeSeg button').forEach((b) => b.classList.toggle('active', b === btn))
  525. render()
  526. })
  527. document.querySelector('.pick').addEventListener('click', (e) => {
  528. const btn = e.target.closest('button[data-choice]')
  529. if (!btn) return
  530. window.__pick(btn.dataset.choice)
  531. })
  532. window.addEventListener('resize', layoutPanels)
  533. render()
  534. </script>
  535. </body>
  536. </html>