【问题标题】:Locomotive Scroll: Is it possible to use querySelector with an URL and an anchor tag?Locomotive Scroll:是否可以将 querySelector 与 URL 和锚标记一起使用?
【发布时间】:2021-12-27 12:39:17
【问题描述】:

我的导航中的链接使用带有锚标记的 URL(例如 mysite.com/about#cv)。原因是即使我不在 f.e. 上,我也想跳转/滚动到特定部分。关于页面。 通常这工作正常。但我正在使用机车滚动库。

我目前正在使用:

<script>$('.nav-link').on('click', function() {const slider = document.querySelector('#cv');locoScroll.scrollTo(slider)</script>

当我在锚点所在的页面上时,这很好用。但我希望能够从我网站上的任何地方访问特定部分。

【问题讨论】:

    标签: jquery anchor-scroll locomotivejs locomotive-scroll


    【解决方案1】:

    您需要检查您所在的网站页面,然后为该页面注入相应的脚本:

    // get currentURL
    let link = $(location).attr('href');
    
    switch (link) { 
        case 'https://yourwebsite.com/about': 
           $('.nav-link').on('click', function() {const slider = document.querySelector('#cv');locoScroll.scrollTo(slider)
        break;
        case 'https://yourwebsite.com/homepage': 
           $('.nav-link').on('click', function() {const slider = document.querySelector('#homepage');locoScroll.scrollTo(slider)
        break;
        default:
           alert('Some script if no one case from the above happens');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      相关资源
      最近更新 更多