• 请不要在回答技术问题时复制粘贴 AI 生成的内容
drymonfidelia
V2EX  ›  程序员

带路由模块的 SPA 跳转 id="example" 这个 div 的最佳实践是什么?例如 Angular,用 #example 直接跳转到了 ./example 这个路由

  •  
  •   drymonfidelia · Oct 2, 2024 · 1555 views
    This topic created in 619 days ago, the information mentioned may be changed or developed.
    3 replies    2024-10-02 19:12:02 +08:00
    MagicalCarl
        1
    MagicalCarl  
       Oct 2, 2024
    const anchor = document.getElementById(`example`);
    if (anchor) {
    anchor.scrollIntoView({
    behavior: 'smooth',
    block: 'start'
    });
    }
    drymonfidelia
        2
    drymonfidelia  
    OP
       Oct 2, 2024
    @MagicalCarl 这样变成要自己 handle 这个逻辑了,也没办法通过 https://xxx.com/my/page#example 这样的链接来直接分享某个章节,想找找有没有更优雅、完美的解法
    crysislinux
        3
    crysislinux  
       Oct 2, 2024 via Android
    就 angular 来说应该可以只用 history api 吧,这样就不占用这个东西了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2699 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:23 · PVG 10:23 · LAX 19:23 · JFK 22:23
    ♥ Do have faith in what you're doing.