Bootstrap 4单击下拉菜单滚动到窗口顶部

时间:2018-07-18 01:15:05

标签: jquery html bootstrap-4

我有三个下拉菜单项,当我第一次单击每个菜单项时,按预期的方式工作,但是当我第二次单击其中的一个菜单项时,它会滚动到顶部,然后切换到适当的选项卡-面板。为什么它第一次能按预期工作,但第二次却不正确?我的HTML位于代码段的下方,但我无法让jQuery不会在代码段中引起错误,因此该代码段分别位于下方。

What it looks like

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <!-- <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <!-- import jQuery scrolling file below as scrolling.js -->
  <script src="js/scrolling.js"></script>
</head>

<body>
  <!-- ********** MAIN CONTENT ********** -->
  <div id="main">
    <section class="background-image d-flex align-items-center" id="home">
      <div class="container-fluid">
        <div class="row">
          <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
          <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
            <h1 class="home-header">Hello!</h1>
            <br><br><br><br><br><br><br><br><br><br>
          </div>
          <div class="col-xl-5 col-lg-5 col-md-5 col-sm-0 col-xs-0"></div>
        </div>
      </div>
    </section>

    <!-- ********** ABOUT ME ********** -->
    <section class="container-fluid" id="about">
      <div class="row">
        <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
        <div class="col-xl-10 col-lg-10 col-md-10 col-sm-12 col-xs-12">
          <h1>About Me</h1>
          <br><br><br><br><br><br><br><br><br><br>
          <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
        </div>
      </div>
    </section>

    <!-- ********** EXPERIENCE ********** -->
    <section class="container-fluid" id="experience">
      <div class="row">
        <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
        <div class="col-xl-10 col-lg-10 col-md-10 col-sm-12 col-xs-12">
          <h1>Experience</h1>
          <br><br><br><br><br><br><br><br><br><br>
        </div>
        <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
      </div>
    </section>

    <!-- ********** PROJECTS ********** -->
    <section id="projects">
      <div class="container-fluid">
        <div class="row">
          <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
          <div class="col-xl-10 col-lg-10 col-md-10 col-sm-12 col-xs-12">
            <h1>Projects</h1>
          </div>
          <div class="col-xl-1 col-lg-1 col-md-1 col-sm-0 col-xs-0"></div>
        </div>
      </div>
      <div>
        <ul class="nav nav-tabs nav-justified">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" id="dropdown">Projects</a>
            <div class="dropdown-menu dropdown-menu-center">
              <a class="dropdown-item" data-toggle="tab" href="#tab1">tab1</a>
              <a class="dropdown-item" data-toggle="tab" href="#tab2">tab2</a>
              <a class="dropdown-item" data-toggle="tab" href="#tab3">tab3</a>
            </div>
          </li>
        </ul>
      </div>
      <div class="tab-content">
        <div class="tab-pane container fade show active" role="tabpanel" id="tab1">
          <div class="row">
            <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
              <h3>tab1</h3>
            </div>
          </div>
        </div>
        <div role="tabpanel" class="tab-pane container fade" id="tab2">
          <div class="row">
            <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
              <h3>tab2</h3>
            </div>
          </div>
        </div>
        <div role="tabpanel" class="tab-pane container fade" id="tab3">
          <div class="row">
            <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
              <h3>tab3</h3>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</body>

</html>

我的jQuery:     

$(document).ready(function() {
    $("a").on('click', function(e) {
        let ref = jQuery(this).attr("class");
        if (this.getAttribute("href").charAt(0) === "#" && (ref !== "nav-link" && ref !== "dropdown-item")) {
            e.preventDefault();
            var hash = this.hash;
            $('html, body').animate({
                scrollTop: $(hash).offset().top
            }, 800, function() {
                window.location.hash = hash;
            });
        }
    });
});

1 个答案:

答案 0 :(得分:0)

使用以下代码解决了我的问题。 jQuery Code