react-router解決錨點跳轉問題


添加一個onClick方法。onClick方法傳入一個錨點的id,然后用下面的函數來找到錨點並跳轉到錨點。

scrollToAnchor = (anchorName) => {
    if (anchorName) {
        // 找到錨點
        let anchorElement = document.getElementById(anchorName);
        // 如果對應id的錨點存在,就跳轉到錨點
        if(anchorElement) { anchorElement.scrollIntoView(); }
    }
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM