30 lines
1.7 KiB
TypeScript
30 lines
1.7 KiB
TypeScript
/** 类目级联面板的水平定位钳制(2026-11-16 客户截图「超出视窗了」:面板锚在触发框
|
|
* left:0,下钻到 3~4 栏时宽 600~800px,而「类目」筛选框在 jd-filter-row 流式布局里
|
|
* 常落在页面中部偏右 ⇒ 面板右半截伸出视窗,末栏被窗缘裁到只剩一列 radio 圆点)。
|
|
*
|
|
* 本文件是**纯钳制数学**(零依赖,node --test 可直测):默认锚左(偏移 0);视口右缘
|
|
* 装不下时整体左移到右缘贴齐(留 margin),左移后越出左缘则贴左缘为止。组件侧在面板
|
|
* 打开与栏数/搜索态变化后用真实 rect 调它(JdCategoryCascader.vue),栏数随下钻动态
|
|
* 1~4,宽度变了要重算——右贴齐后继续下钻即向左生长,不会再伸出右缘。 */
|
|
|
|
export interface PanelClampInput {
|
|
/** 触发框(.cat-cascader)的视口左缘 x。 */
|
|
hostLeft: number
|
|
/** 面板当前实际渲染宽(含边框;栏数不同宽度不同)。 */
|
|
panelWidth: number
|
|
/** 视口宽(window.innerWidth)。 */
|
|
viewportWidth: number
|
|
/** 视口两侧最小留白,默认 8px。 */
|
|
margin?: number
|
|
}
|
|
|
|
/** 返回面板相对触发框左缘应加的 left 偏移(px;负=左移、0=默认锚左即可、正=触发框
|
|
* 本身已贴近视口左缘时把面板推回 margin 内,物理上只在窄窗极端值出现)。 */
|
|
export function clampPanelLeftX(input: PanelClampInput): number {
|
|
const margin = input.margin ?? 8
|
|
const overflowRight = input.hostLeft + input.panelWidth - (input.viewportWidth - margin)
|
|
if (overflowRight <= 0) return 0
|
|
const desiredLeft = Math.max(margin, input.hostLeft - overflowRight)
|
|
return desiredLeft - input.hostLeft
|
|
}
|