View Transitions 与 Popover-Anchor Positioning 组合交互与避障实践概述在导航与局部状态变更中,弹层与共享元素需保持对齐与稳定。通过 View Transitions 与 Popover、Anchor Positioning 的协作,可实现自然的过渡与避障。关键参数/概念`view-transition-name` 定义共享元素标识,用于跨文档匹配。Popover 原生弹层与 `anchor-name` 提供稳定对齐与避障能力。`prefers-reduced-motion` 控制动画强度与禁用路径。实践/示例<style> .thumb { view-transition-name: post-thumb } ::view-transition-old(post-thumb), ::view-transition-new(post-thumb) { animation: fade 180ms ease } @keyframes fade { from { opacity: 0 } to { opacity: 1 } } @media (prefers-reduced-motion: reduce) { ::view-transition-old(post-thumb), ::view-transition-new(post-thumb) { animation: none } } .anchor { anchor-name: --details } .popover:popover-open { position-anchor: --details } .popover { inset-area: bottom } .popover { animation: fade 120ms ease } @media (prefers-reduced-motion: reduce) { .popover { animation: none } } .thumb { display: inline-block } .popover { display: block } .thumb, .popover { will-change: transform } }</style> <button class="thumb anchor" popovertarget="details">详情</button> <div id="details" class="popover" popover> 内容 <button popovertarget="details" popovertargetaction="hide">关闭</button> <a href="/post/next" id="to-next">下一页</a> </div> <script> function withTransition(run) { const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches if (reduce || !document.startViewTransition) { run(); return } document.startViewTransition(() => run()) } document.getElementById('to-next').addEventListener('click', (e) => { e.preventDefault() withTransition(async () => { const html = await (await fetch(e.target.href)).text() document.open(); document.write(html); document.close() }) }) </script> 验证方法过渡一致性:弹层开启状态下导航,检查共享元素与弹层位置稳定。可访问性:在减少动态效果偏好下禁用动画,检查可达性与信息完整性。多断点:移动与桌面断点验证定位与避障行为一致。注意事项避免在过渡期间改变参与元素尺寸与定位。为参与过渡与锚点元素提供独立样式作用域,减少外部影响。弹层开启状态下导航需确保页面激活后状态可预期与可关闭。参考资料MDN: ViewTransition 与 `startViewTransition()`https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransitionhttps://developer.mozilla.org/en-US/docs/Web/API/ViewTransitionMDN: Popover 与 Anchor Positioninghttps://developer.mozilla.org/en-US/docs/Web/API/Popover_APIhttps://developer.mozilla.org/en-US/docs/Web/CSS/anchor-name

发表评论 取消回复