HTML 粘性导航栏在使用 JS 加载时变得不粘性

发布于 2025-01-10 16:14:02 字数 858 浏览 2 评论 0原文

我尝试在各种静态 html 页面上加载粘性导航栏标题。

我使用这个 jquery 脚本来执行此操作:

<script
    src="https://code.jquery.com/jquery-3.3.1.js"
    integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
    crossorigin="anonymous">
</script>
<script> 
$(function(){
  $("#header").load("header.html"); 
});
</script> 

并将其加载到我想要的位置,

    <header id="header" class="header-transparent header-transparent-light-bottom-border header-transparent-light-bottom-border-1 header-effect-shrink" data-plugin-options="{'stickyEnabled': true, 'stickyEffect': 'shrink', 'stickyEnableOnBoxed': true, 'stickyEnableOnMobile': false, 'stickyChangeLogo': true, 'stickyStartAt': 30, 'stickyHeaderContainerHeight': 70}">
            </header>

除了导航栏的粘性部分之外,一切都正常。由于我不知道的原因,它决定充当非粘性导航栏并随页面内容滚动。

I try to load a sticky navigation bar header on various static html pages.

I use this jquery script to do so:

<script
    src="https://code.jquery.com/jquery-3.3.1.js"
    integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
    crossorigin="anonymous">
</script>
<script> 
$(function(){
  $("#header").load("header.html"); 
});
</script> 

and this to load it where I want

    <header id="header" class="header-transparent header-transparent-light-bottom-border header-transparent-light-bottom-border-1 header-effect-shrink" data-plugin-options="{'stickyEnabled': true, 'stickyEffect': 'shrink', 'stickyEnableOnBoxed': true, 'stickyEnableOnMobile': false, 'stickyChangeLogo': true, 'stickyStartAt': 30, 'stickyHeaderContainerHeight': 70}">
            </header>

Everything works fine except for the sticky part of the navigation bar. For a reason unknown to me it decides to act as a non-sticky navigation bar and scroll with the content of the page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2025-01-17 16:14:02

请确保您的 CSS 加载正确&没有被覆盖。检查浏览器控制台以查看 Javascript 错误和还要检查您添加的样式是否正在实施。

另一种解决方法,请尝试添加内联样式:

<div class="header" style="position: fixed; top: 0;" >
  <h2>My Header</h2>
</div>

Please make sure your CSS is loading properly & is not overridden. Check browser console to see Javascript errors & also check the style you added is being implemented or not.

Another workaround, Please try adding inline-style:

<div class="header" style="position: fixed; top: 0;" >
  <h2>My Header</h2>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文