javascript - 如何使用 "fixed" header 使 anchor 标记直接指向页面上的正确位置?

标签 javascript jquery html css anchor

我有一个网站 position: fixed;始终显示在页面顶部的 header div。

<div id="header"></div>  

<section id="aso">
*text text with around 400px + height * 
</section>

<section id="one">
*text text with around 400px + height * 
</section>

<section id="two">
*text text with around 400px + height * 
</section>

<section id="three">
*text text with around 400px + height * 
</section>

标题中带有重定向到 www.example.com/#first 的按钮 它使用一个很好的 JavaScript 代码向下滚动(具有滑动效果)页面并减去标题的 184px,因此它会滚动到正确的位置。这是 .js 部分:

$(document).ready(function()
{
    // Scroll the whole document
  $.localScroll({
        offset:{left: 0, top:-184 }
    });


    });

当使用这个:www.example.com/#third(例如来自外部网站)时,这个 .js 函数不会运行,它会直接将我引导到 <section id="third"> 的中间。并且不减去 184px。

最好的解决方案是什么?

最佳答案

之所以会这样,是因为在重定向时,您的 js 函数甚至都没有被调用。您必须在重定向时显式调用它。我建议你使用图书馆 https://github.com/alvarotrigo/fullPage.js .它也带来了相同的滚动效果处理 anchor 。您只需编写正确的标记并使用此库。它会完成所有这一切。

关于javascript - 如何使用 "fixed" header 使 anchor 标记直接指向页面上的正确位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102441/

相关文章:

javascript - 将字符串数组转换为对象数组的最佳方法是什么?

javascript - 通过查找平均 FPS 提高 Canvas 游戏性能

javascript - JQuery 访问单击的元素而不使用闭包

jquery - html/css 响应式网格,可正确调整大小和填充宽度

html - 从底部而不是顶部开始高度动画

javascript - jQuery 将元素的类保存在静态变量中并重用

javascript - 在 Javascript/Jquery 中执行 url 而不在浏览器上打开它

javascript - 有没有办法检测用户是否真的离开了计算机

html - 使用 CSS 将 DIV 对齐到不相关的 DIV 的底部(无父/子关系)

javascript - ionic 3/angular 4 - 多个 ng 模型的 ionic 范围单一功能