简体   繁体   English

锚点在某些页面上不起作用

[英]Anchors not working on some pages

Here is the site I'm working on: http://defend-foreclosure.com 这是我正在工作的网站: http : //defend-foreclosure.com

If you go to http://defend-foreclosure.com/law.html and click through the dropdown links in the nav for "Law", you'll see that the anchors work perfectly. 如果转到http://defend-foreclosure.com/law.html并单击导航栏中“法律”的下拉链接,您会发现锚点工作正常。 It was hiding the title of each section behind the header, but I fixed the issue by adding the following script to my html files. 它在标题后面隐藏了每个部分的标题,但是我通过将以下脚本添加到html文件中来解决了该问题。

<script>
  $().ready(function(){
    // Fixing Anchor links nav leaving the destination text hidden under Header    
    $(window).hashchange(function() {
      var hash = "#" + location.hash.replace(/[^A-Za-z0-9_\-]/g, "");
      if (hash && $(hash).offset()) {
        var pos = $(hash).offset().top - 55;
        $(window).scrollTop(pos);
      }
    });
  });
</script>

The problem occurs when I try to navigate to one of the anchors from any page other than law.html. 当我尝试从law.html以外的任何页面导航到锚点之一时,就会出现问题。 I think this has something to do with the script only working on .haschange, instead of universally. 我认为这与仅在.haschange上运行的脚本有关,而不是与通用脚本有关。

If you go to home, about, or contact, and try to click on one of the anchors in the dropdown, you'll notice that it still cuts off the title of the section under the nav. 如果您回家,左右交流或联系,并尝试单击下拉菜单中的锚点之一,您会注意到它仍然切断了导航下方部分的标题。 Does anyone know how I can make this work properly on all pages? 有谁知道我如何才能在所有页面上正常工作?

EDIT: 编辑:

If I add the following code outside of .haschange, followed by an alert, it works properly, alerts, and then once I click okay, it messes up and hides the title again. 如果我在.haschange之外添加以下代码,然后添加一个警报,则它可以正常工作,发出警报,然后单击“确定”后,它会混乱并再次隐藏标题。

<script>
    var hash = "#" + location.hash.replace(/[^A-Za-z0-9_\-]/g, "");
          if (hash && $(hash).offset()) {
            var pos = $(hash).offset().top - 55;
            $(window).scrollTop(pos);
          }
</script>

EDIT: I have narrowed the script down to the following 3 lines on my law.html page. 编辑:我已将脚本缩小到我law.html页面上的以下3行。

<script>
    var shiftWindow = function() { scrollBy(0, -50) };
    if (location.hash) shiftWindow();
    window.addEventListener("hashchange", shiftWindow);
</script>

This makes the function work properly only when on the law.html page. 这使该功能仅在law.html页面上才能正常工作。 I still cannot get it to work when clicking an anchor in the nav from another page. 当我从另一个页面单击导航中的锚点时,仍然无法正常工作。

This is not pretty but chrome seems to trigger it's page positioning after the page load event. 这不是很漂亮,但是Chrome在页面加载事件之后似乎会触发其页面定位。 so the only way i found is with a timeout 所以我发现的唯一方法是超时

<script>
    $(function(){
        setTimeout(function(){
            var shiftWindow = function() { scrollBy(0, -50) };
            if (location.hash) shiftWindow();
            window.addEventListener("hashchange", shiftWindow);
        },10);
    })
</script>

you could also unset the id (the browser would not find where to scroll) and scroll manualy, but you would need to reset the id after, and there is no after if you do not use a timeout 您还可以取消设置ID(浏览器无法找到滚动位置)并手动滚动,但是您需要在此之后重新设置ID,如果不使用超时,则没有设置。

EDIT : 编辑:

To be able to use this function with other events as well you will need the scroll to be absolute, because in some browser event flow is not exactly the same and may trigger twice : 为了能够与其他事件一起使用此功能,您需要滚动是绝对的,因为在某些浏览器中,事件流并不完全相同,并且可能触发两次:

<script>
    $(function(){
        setTimeout(function(){
            var shiftWindow = function() { 
                var hash = "#" + location.hash.replace(/[^A-Za-z0-9_\-]/g, "");
                if (hash && $(hash).offset()) {
                    var pos = $(hash).offset().top - 55;
                    $(window).scrollTop(pos);
                }
             };
            if (location.hash) shiftWindow();
            window.addEventListener("hashchange", shiftWindow);
        },10);
    })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM