阻止网页页面跳到顶部

时间:2015-09-25 16:53:37

标签: javascript jquery html css navbar

我正在尝试使用粘贴式导航栏更新我的网站:

<div id ="navbar">
    <ul class="nav navbar-nav nav-top"><!--Navigation Bar-->
        <li class="navbar-brand">TechU</li>
        <li><a href="index.aspx">About</a></li>
        <li><a href="projects.aspx">Projects</a></li>
        <li><a href="resume.aspx">Resume</a></li>
        <li class="dropdown"><!--Start Dropdown-->
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Social Media
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"><!--Dropdown Menu-->
                <li><a href="#">Facebook <img src="Media/Images/Icons/FaceBook.ico" /></a></li>
                <li><a href="#">Twitter <img src="Media/Images/Icons/Twitter(1).ico" /></a></li>
                <li><a href="#">WordPress <img src="Media/Images/Icons/Wordpress.ico" /></a></li>
                <li><a href="#">Android Apps <img src="Media/Images/Icons/android.png" /></a></li>
                <li><a href="#">YouTube <img src="Media/Images/Icons/Youtube.ico" /></a></li>
            </ul><!--End Dropdown Menu-->
        </li>
    </ul><!--End Navigation Bar-->
</div>

这是我正在使用的JavaScript:

    var mn = $(".nav"),
    hdr = $('#hero').height();

    $(window).scroll(function () {
        var scrollPos = $(this).scrollTop();
        if (scrollPos >= hdr) {
            mn.addClass('navbar-fixed-top');
            mn.removeClass('nav-top');
        } else {
            mn.removeClass('navbar-fixed-top');
            mn.addClass('nav-top');
        }
    });

我遇到的问题是,如果页面不足以滚动,但页面顶部会触及导航栏的顶部,则页面将直接跳回到顶部。我的网站是TechUHost.com,所以你也可以自己去看看。

谢谢!

2 个答案:

答案 0 :(得分:0)

我建议不要使用bootstrap类,而是创建自己的类,因为它可以使它看起来更流畅。像css类这样的东西。

<div id="navSpy"></div>
    <div id="nav" class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <button id="nav-button" class="navbar-toggle" data-target=".navbar-collapse" data-toggle="collapse" type="button">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar icon-bar-up"></span>
                    <span class="icon-bar icon-bar-middle"></span>
                    <span class="icon-bar icon-bar-down"></span>
                </button>
                <a class="navbar-brand" href="/">Deft</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li ><a href="/about/">About</a></li>
                    <li><a href="/contact/">Contact</a></li>
                    <li ><a href="/blog/">Blog</a></li>
                </ul>
            </div>
        </div>
    </div>


$(function () {
    var e = $("#navSpy").offset().top;
    var t = $(window).scrollTop();
    $(window).resize(function () {
        e = $("#navSpy").offset().top + 15;
        t = $(window).scrollTop()
    });
    $(window).scroll(function () {
        t = $(window).scrollTop();
        if (e < t) {
            $("#nav").addClass("navbar-sticky");
        } else {
            $("#nav").removeClass("navbar-sticky");
        }
    })
});

//CSS
.navbar-sticky {
    position: fixed;
    top: 0;
    margin-top: 0;
}

答案 1 :(得分:0)

您的代码实际上没有引导程序navbar。根据bootstrap文档,

Be sure to use a <nav> element or, if using a more generic element such as a <div>, add a role="navigation" to every navbar to explicitly identify it as a landmark region for users of assistive technologies.

有多种方法可以实现粘性导航栏。假设您不需要响应,我希望以下代码可以帮助您。

<body>
  <div id="hero">
    ...
  </div>
  <nav id ="navbar" class="navbar navbar-default">
    <a class="navbar-brand" href="#">TechU</a>
    <ul class="nav navbar-nav">
      <li><a href="#">About</a></li>
      <li><a href="#">Projects</a></li>
      <li><a href="#">Resume</a></li>
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
          Social Media
          <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
          <li><a href="#">Facebook</a></li>
          <li><a href="#">Twitter</a></li>
          <li><a href="#">WordPress</a></li>
          <li><a href="#">Android</a></li>
          <li><a href="#">YouTube</a></li>
        </ul>
      </li>
    </ul>
  </nav>
  ...
  ...
</body>

相应的javascript是,

  var navbar = $("#navbar");
  var heroHeight = $('#hero').height();
  $(window).scroll(function () {
    var scrolled = $(this).scrollTop();
    if(scrolled >= heroHeight)
      navbar.addClass('navbar-fixed-top');
    else
      navbar.removeClass('navbar-fixed-top');
  });