0

每当我单击导航栏上的切换菜单按钮时,窗口都会滚动到页面顶部。有没有办法让菜单出现在当前页面的任何位置?

我有一种感觉,它与导航栏的部分有关,由于溢出,我不得不隐藏这些部分。

代码笔: https ://codepen.io/Ribeye/pen/BajOoeb

谢谢!

脚本

var $header_top = $('.header-top');
    var $nav = $('nav');
    
$header_top.find('a').on('click', function() {
  $(this).parent().toggleClass('open-menu');
});

CSS

.imgServices {
    width: 100%;
    height: 100%;
    border-style: solid;
    border-color: white;
    border-width: 5px;
    margin-left: auto;
    margin-right: auto;
    object-fit: cover;
}

a {
  text-decoration: none;
  color: white;
}

ul,
li {
  list-style-type: none;
}

.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}

.clearfix:after {
  clear: both;
}

.l-left {
  float: left;
}

.l-right {
  float: right;
}

.end {
  margin-top: 30px;
  font-size: 3em;
  font-weight: bold;
  opacity: 0;
  -webkit-transform: translateY(300px);
  transform: translateY(300px);
  transition: opacity, -webkit-transform 1s;
  transition: opacity, transform 1s;
  transition: opacity, transform 1s, -webkit-transform 1s;
  transition-delay: 1s;
}

.header-top {
  background: white;
  height: 70px;
  padding: 0 10px;
  position: fixed;
  top: 0;
  width: 100%;
  min-width: 300px;
  z-index: 12;
  box-sizing: border-box;
}

h1 {
  line-height: 70px;
  height: 70px;
}

h1 a {
  color: red;
  padding: 0 10px;
  font-family: "arial black";
  font-size: 35px;
}

.first-letter {
  color: red;
  padding: 0px;
  font-family: "arial black";
  font-size: 45px;
}

.toggle-menu {
  width: 50px;
  height: 50px;
  display: inline-block;
  position: relative;
  top: 10px;
}

.toggle-menu i {
  position: absolute;
  display: block;
  height: 2px;
  background: red;
  width: 30px;
  left: 10px;
  transition: all .3s;
}

.toggle-menu i:nth-child(1) {
  top: 16px;
}

.toggle-menu i:nth-child(2) {
  top: 24px;
}

.toggle-menu i:nth-child(3) {
  top: 32px;
}

.open-menu i:nth-child(1) {
  top: 25px;
  -webkit-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}

.open-menu i:nth-child(2) {
  background: transparent;
}

.open-menu i:nth-child(3) {
  top: 25px;
  -webkit-transform: rotateZ(-45deg);
  transform: rotateZ(-45deg);
}

nav {
  height: 0;
  opacity: 0;
  box-sizing: border-box;
  background: rgba(0, 47, 77, .25);
  position: fixed;
  top: 70px;
  width: 100%;
  transition: all 1s;
  overflow: hidden;
}

.open-menu~nav {
  opacity: 1;
  padding: 80px 0;
  z-index: 15;
  height: calc(90vh - 70px);
}

nav ul {
  padding: 0 10px;
  display: flex;
}

nav li {
  flex: 1;
}

nav li a {
  font-size: 2em;
  display: block;
  padding: 30px;
  text-align: center;
  transition: background .3s;
}

nav li a {
  background: #ff4b4b;
  margin-left: 20px;
}

nav li a:hover {
  background: #ADD8E6;
}


/*These 3 sections add the drop dwon menus in the headers*/

ul li ul.services-dropdown {
  display: none;
  z-index: 999;
  width: 100%;
}

ul li:hover ul.services-dropdown {
  display: inline-block;
  /* Display the dropdown */
}

ul li ul.services-dropdown li {
  display: block;
}

section {
  text-align: center;
}

h2 {
  font-size: 13px;
}

h2 a {
  padding: 8 8 8 8px;
  float: left;
  color: white;
  background-color: red;
  font-family: 'Open Sans';
  font-weight: bold;
}

h3 {
  font-weight: bold;
  font-size: 60px;
  color: white;
}

#fp-nav ul li a span,
.fp-slidesNav ul li a span {
  background: white;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
}

#fp-nav ul li a.active span,
.fp-slidesNav ul li a.active span,
#fp-nav ul li:hover a.active span,
.fp-slidesNav ul li:hover a.active span {
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  background: transparent;
  box-sizing: border-box;
  border: 2px solid #212121;
}

@media (max-width: 700px) {
  .edit-name {
    display: none;
  }
}


/*Removes the tel and email when window is narrow */

@media (max-width: 1230px) {
  .narrow-hide {
    display: none;
  }
}


/*Edits the nav bar when window is narrow */

@media screen and (max-width: 767px) {
  nav ul {
    flex-direction: column;
  }
  nav li {
    margin-top: 1px;
  }
  nav li a {
    font-size: 1.5em;
  }
  .scroll-icon {
    display: none;
  }
  @media screen and (max-width: 400px) {
    html {
      font-size: 50%;
    }
    .open-menu~nav {
      padding: 20px 0;
    }
    nav li a {
      padding: 3px;
    }
  }

HTML

<header>
  <div class="header-top clearfix">
    <a class="l-right toggle-menu" href="#">
      <i></i>
      <i></i>
      <i></i>
    </a>
  </div>
  <nav class="hide">
    <ul id="menu">
      <li>
        <a href="https://www.google.com ">Home</a>
          </li>
          <li>
            <a href="https://www.google.com "> Services</a>
                 <ul class="services-dropdown ">
                    <li><a href="https://www.google.com ">whats</a>   </li>
                    <li><a href="https://www.google.com ">Stuff</a></li>
                    <li><a href="https://www.google.com ">Things</a></li>
                </ul>
          </li>
          <li>
            <a href="https://www.google.com ">News & Events</a>
          </li>
          <li>
            <a href="https://www.google.com ">Contact Us</a>
          </li>
          <li>
            <a href="https://www.google.com ">Data Protection</a>
          </li>
          <li>
            <a href="https://www.google.com ">Data Protection</a>
          </li>                               
        </ul>
      </nav>
    </header>

    <body>
<img class="imgServices" src="https://lindhill.co.uk/wp-content/uploads/2016/09/light-blue-block.png" alt="blue"></body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js "></script>
4

4 回答 4

3

这是因为href="#"存在于锚标签上。event.preventDefault();在onclick函数里面添加,就可以了

var $header_top = $('.header-top');
var $nav = $('nav');
 
$header_top.find('a').on('click', function(event) {
  event.preventDefault();
  $(this).parent().toggleClass('open-menu');
});
.imgServices {
    width: 100%;
    height: 100%;
    border-style: solid;
    border-color: white;
    border-width: 5px;
    margin-left: auto;
    margin-right: auto;
    object-fit: cover;
}

a {
    text-decoration: none;
    color: white;
}
ul, li {
    list-style-type: none;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}
.l-left {
    float: left;
}
.l-right {
    float: right;
}
.end {
    margin-top: 30px;
    font-size: 3em;
    font-weight: bold;
    opacity: 0;
    -webkit-transform: translateY(300px);
            transform: translateY(300px);
    transition: opacity, -webkit-transform 1s;
    transition: opacity, transform 1s;
    transition: opacity, transform 1s, -webkit-transform 1s;
    transition-delay: 1s;
}
.header-top {
    background: white;
    height: 70px;
    padding: 0 10px;
    position: fixed;
    top: 0;
    width: 100%;
    min-width: 300px;
    z-index: 12;
    box-sizing: border-box;
}
h1 {
    line-height: 70px;
    height: 70px;
    
}
h1 a {
    color: red;
    padding: 0 10px;
    font-family: "arial black";
    font-size: 35px;
}
.first-letter {
    color: red;
    padding: 0px;
    font-family: "arial black";
    font-size: 45px;
}
.toggle-menu {
    width: 50px;
    height: 50px;
    display: inline-block;
    position: relative;
    top: 10px;
}
.toggle-menu i {
    position: absolute;
    display: block;
    height: 2px;
    background: red;
    width: 30px;
    left: 10px;
    transition: all .3s;
}
.toggle-menu i:nth-child(1) {
    top: 16px;
}
.toggle-menu i:nth-child(2) {
    top: 24px;
}
.toggle-menu i:nth-child(3) {
    top: 32px;
}
.open-menu i:nth-child(1) {
    top: 25px;
    -webkit-transform: rotateZ(45deg);
            transform: rotateZ(45deg);
}
.open-menu i:nth-child(2) {
    background: transparent;
}
.open-menu i:nth-child(3) {
    top: 25px;
    -webkit-transform: rotateZ(-45deg);
            transform: rotateZ(-45deg);
}
nav {
    height: 0;
    opacity: 0;
  box-sizing: border-box;
    background: rgba(0, 47, 77, .25);
    position: fixed;
    top: 70px;
    width: 100%;
  transition: all 1s;
  overflow: hidden;
}
.open-menu ~ nav {
    opacity: 1;
    padding: 80px 0;
    z-index: 15;
    height: calc(90vh - 70px);
}
nav ul {
    padding: 0 10px;
    display: flex;
}
nav li {
    flex: 1;
}
nav li a {
    font-size: 2em;
    display: block;
    padding: 30px;
    text-align: center;
    transition: background .3s;
}
nav li a {
    background: #ff4b4b;
    margin-left: 20px;
}
nav li a:hover {
    background: #ADD8E6;
}

/*These 3 sections add the drop dwon menus in the headers*/

ul li ul.services-dropdown{ 
    display: none;
    z-index: 999;
    width: 100%;
}

ul li:hover ul.services-dropdown{
    display: inline-block;  /* Display the dropdown */
     }
     
ul li ul.services-dropdown li{
    display: block;
 }

section { 
    text-align: center;
}
h2 {
    font-size: 13px;
    
}

h2 a{
    padding: 8 8 8 8px;
    float: left;
    color: white;
    background-color: red;
    font-family: 'Open Sans';
    font-weight: bold;
}

h3 {
    font-weight: bold;
    font-size: 60px;
    color: white;
}
#fp-nav ul li a span, 
.fp-slidesNav ul li a span {
    background: white;
    width: 8px;
    height: 8px;
    margin: -4px 0 0 -4px;
}
#fp-nav ul li a.active span, 
.fp-slidesNav ul li a.active span, 
#fp-nav ul li:hover a.active span, 
.fp-slidesNav ul li:hover a.active span {
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    background: transparent;
    box-sizing: border-box;
    border: 2px solid #212121;
}

@media (max-width: 700px) {

  .edit-name{
    display: none;
  }
  
}

/*Removes the tel and email when window is narrow */

@media (max-width: 1230px) {

  .narrow-hide{
    display: none;
    
  }
  
}

/*Edits the nav bar when window is narrow */
 
@media screen and (max-width: 767px) {
    nav ul {
        flex-direction: column;
    }
 
    nav li {
        margin-top: 1px;
    }
  
  nav li a {
    font-size: 1.5em;
  }
  
  .scroll-icon {
    display: none;

}
 
@media screen and (max-width: 400px) {
  html {
    font-size: 50%;
  }
  
  .open-menu ~ nav {
        padding: 20px 0;
    }
 
    nav li a {
        padding: 3px;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
  <div class="header-top clearfix">
    

    <a class="l-right toggle-menu" href="#">
      <i></i>
      <i></i>
      <i></i>
    </a>
  </div>
 
  <nav class="hide">
    <ul id="menu">
      <li>
        <a href=""https://www.google.com">Home</a>
      </li>
      <li>
        <a href="https://www.google.com"> Services</a>
             <ul class="services-dropdown">
                <li><a href="https://www.google.com">whats</a>   </li>
                <li><a href="https://www.google.com">Stuff</a></li>
                <li><a href="https://www.google.com">Things</a></li>
            </ul>
      </li>

      <li>
        <a href="https://www.google.com">News & Events</a>
      </li>
      <li>
        <a href="https://www.google.com">Contact Us</a>
      </li>                                 
    </ul>
  </nav>
</header>
                                        
<body>
                                                    <img class="imgServices" src="https://lindhill.co.uk/wp-content/uploads/2016/09/light-blue-block.png" alt="blue">
                                             </body>

于 2020-07-15T11:14:43.463 回答
1

只需从 html 的第 3 行删除 de href="#" 即可。所以这:

<a class="l-right toggle-menu" href="#">
  <i></i>
  <i></i>
  <i></i>
</a>

应该是这样的:

<a class="l-right toggle-menu">
  <i></i>
  <i></i>
  <i></i>
</a>

作为解释,这里发生的事情是导航被激活到“#”,它的作用是查找给定的 id(这里没有给出 [ex. href="#myId"])并移动窗口的该元素的视口。现在,因为没有找到任何元素,它只会将您发送到文档的顶部

于 2020-07-15T11:13:36.933 回答
0

您只需要阻止锚标记的默认行为。该href属性设置为#,这就是窗口转到顶部的原因。

$header_top.find('a').on('click', function(e) {
  e.preventDefault();
  $(this).parent().toggleClass('open-menu');
});
于 2020-07-15T11:21:46.177 回答
0

您在汉堡菜单<a>标签中使用了href="#",这是错误的,我<a>用标签更改了标签<div>,在您的 JS 代码中我用 'div' 更改了查找;

var $header_top = $('.header-top');
var $nav = $('nav');
 
$header_top.find('div').on('click', function() {
  $(this).parent().toggleClass('open-menu');
});
.imgServices {
    width: 100%;
    height: 100%;
    border-style: solid;
    border-color: white;
    border-width: 5px;
    margin-left: auto;
    margin-right: auto;
    object-fit: cover;
}

a {
    text-decoration: none;
    color: white;
}
ul, li {
    list-style-type: none;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}
.l-left {
    float: left;
}
.l-right {
    float: right;
}
.end {
    margin-top: 30px;
    font-size: 3em;
    font-weight: bold;
    opacity: 0;
    -webkit-transform: translateY(300px);
            transform: translateY(300px);
    transition: opacity, -webkit-transform 1s;
    transition: opacity, transform 1s;
    transition: opacity, transform 1s, -webkit-transform 1s;
    transition-delay: 1s;
}
.header-top {
    background: white;
    height: 70px;
    padding: 0 10px;
    position: fixed;
    top: 0;
    width: 100%;
    min-width: 300px;
    z-index: 12;
    box-sizing: border-box;
}
h1 {
    line-height: 70px;
    height: 70px;
    
}
h1 a {
    color: red;
    padding: 0 10px;
    font-family: "arial black";
    font-size: 35px;
}
.first-letter {
    color: red;
    padding: 0px;
    font-family: "arial black";
    font-size: 45px;
}
.toggle-menu {
    width: 50px;
    height: 50px;
    display: inline-block;
    position: relative;
    top: 10px;
}
.toggle-menu i {
    position: absolute;
    display: block;
    height: 2px;
    background: red;
    width: 30px;
    left: 10px;
    transition: all .3s;
}
.toggle-menu i:nth-child(1) {
    top: 16px;
}
.toggle-menu i:nth-child(2) {
    top: 24px;
}
.toggle-menu i:nth-child(3) {
    top: 32px;
}
.open-menu i:nth-child(1) {
    top: 25px;
    -webkit-transform: rotateZ(45deg);
            transform: rotateZ(45deg);
}
.open-menu i:nth-child(2) {
    background: transparent;
}
.open-menu i:nth-child(3) {
    top: 25px;
    -webkit-transform: rotateZ(-45deg);
            transform: rotateZ(-45deg);
}
nav {
    height: 0;
    opacity: 0;
  box-sizing: border-box;
    background: rgba(0, 47, 77, .25);
    position: fixed;
    top: 70px;
    width: 100%;
  transition: all 1s;
  overflow: hidden;
}
.open-menu ~ nav {
    opacity: 1;
    padding: 80px 0;
    z-index: 15;
    height: calc(90vh - 70px);
}
nav ul {
    padding: 0 10px;
    display: flex;
}

nav li a {
    font-size: 2em;
    display: block;
    padding: 30px;
    text-align: center;
    transition: background .3s;
}
nav li a {
    background: #ff4b4b;
    margin-left: 20px;
}
nav li a:hover {
    background: #ADD8E6;
}

/*These 3 sections add the drop dwon menus in the headers*/

ul li ul.services-dropdown{ 
    display: none;
    z-index: 999;
    width: 100%;
}

ul li:hover ul.services-dropdown{
    display: inline-block;  /* Display the dropdown */
     }
     
ul li ul.services-dropdown li{
    display: block;
 }

section { 
    text-align: center;
}
h2 {
    font-size: 13px;
    
}

h2 a{
    padding: 8 8 8 8px;
    float: left;
    color: white;
    background-color: red;
    font-family: 'Open Sans';
    font-weight: bold;
}

h3 {
    font-weight: bold;
    font-size: 60px;
    color: white;
}
#fp-nav ul li a span, 
.fp-slidesNav ul li a span {
    background: white;
    width: 8px;
    height: 8px;
    margin: -4px 0 0 -4px;
}
#fp-nav ul li a.active span, 
.fp-slidesNav ul li a.active span, 
#fp-nav ul li:hover a.active span, 
.fp-slidesNav ul li:hover a.active span {
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    background: transparent;
    box-sizing: border-box;
    border: 2px solid #212121;
}

@media (max-width: 700px) {

  .edit-name{
    display: none;
  }
  
}

/*Removes the tel and email when window is narrow */

@media (max-width: 1230px) {

  .narrow-hide{
    display: none;
    
  }
  
}

/*Edits the nav bar when window is narrow */
 
@media screen and (max-width: 767px) {
    nav ul {
        flex-direction: column;
    }
 
    nav li {
        margin-top: 1px;
    }
  
  nav li a {
    font-size: 1.5em;
  }
  
  .scroll-icon {
    display: none;

}
 
@media screen and (max-width: 400px) {
  html {
    font-size: 50%;
  }
  
  .open-menu ~ nav {
        padding: 20px 0;
    }
 
    nav li a {
        padding: 3px;
    }
}
<header>
  <div class="header-top clearfix">
    

    <div class="l-right toggle-menu">
      <i></i>
      <i></i>
      <i></i>
    </div>
  </div>
 
  <nav class="hide">
    <ul id="menu">
      <li>
        <a href=""https://www.google.com">Home</a>
      </li>
      <li>
        <a href="https://www.google.com"> Services</a>
             <ul class="services-dropdown">
                <li><a href="https://www.google.com">whats</a>   </li>
                <li><a href="https://www.google.com">Stuff</a></li>
                <li><a href="https://www.google.com">Things</a></li>
            </ul>
      </li>

      <li>
        <a href="https://www.google.com">News & Events</a>
      </li>
      <li>
        <a href="https://www.google.com">Contact Us</a>
      </li>                                 
    </ul>
  </nav>
</header>
                                        
<body>
   <img class="imgServices" src="https://lindhill.co.uk/wp-content/uploads/2016/09/light-blue-block.png" alt="blue">
</body>

于 2020-07-15T11:14:58.227 回答