简体   繁体   English

onClick跳到页面顶部

[英]onClick jumping to top of page

I am coding a simple website with a JS side navbar. 我正在用JS侧导航栏编写一个简单的网站。 When I resize the window to develop at different widths I noticed that the page would jump to the top. 当我调整窗口大小以使用不同的宽度进行开发时,我注意到页面会跳到顶部。 I then noticed that when I click the hamburger icon the page also jumps to the top. 然后,我注意到当我单击汉堡包图标时,页面也会跳到顶部。

I've looked into it and it appears to be the onClick that is the issue. 我研究了它,似乎是onClick成为了问题。 I have tried to use return false but the issue is still the same. 我尝试使用return false,但问题仍然相同。

I have used the code/tutorial provided on w3 schools . 我已经使用了w3学校提供的代码/教程。 Thanks for any help 谢谢你的帮助

Here is my code 这是我的代码

HTML: HTML:

<nav id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav();return false;">&times;</a>

    <img class="sidenavbar-logo-img" src='<?php echo get_template_directory_uri(); ?>/img/logo-NO.png'>

    <?php wp_nav_menu( array( 'theme_location' => 'sidenav-menu' ) ); ?>

<div class='sidebar-nav-info'>
    <p>Lower Trinity St,
    Birmingham,
    B9 4AG</p>
    <p>Facebook   Instagram</p>
    </div>

</nav>

  <!-- Use any element to open the sidenav -->
  <span class="hamburger" onclick="openNav(); return false;">
<i class="fas fa-bars"></i>
</span>

CSS CSS

      /* The navigation menu links */

      .sidenav ul {
        list-style-type: none;
        margin-left: 0;
      }

      .sidenav ul li {
        margin: 0;
      }

      .sidenav a {
          padding: 8px 8px 8px 0px;
          text-decoration: none;
          font-weight: 700;
          font-size: 18px;
          color: #f1f1f1;
          display: block;
          transition: 0.3s;
      }

      /* When you mouse over the navigation links, change their color */
      .sidenav a:hover {
          color: #818181;
          text-decoration: none;
      }

      /* Position and style the close button (top right corner) */
      .sidenav .closebtn {
          position: absolute;
          top: 0;
          right: 25px;
          font-size: 36px;
          margin-left: 50px;
      }

      /* Style page content - use this if you want to push the page content to the right when you open the side navigation */
      #main {
          transition: margin-left .5s;
          padding: 20px;
      }

      /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
      @media screen and (max-height: 450px) {
          .sidenav {padding-top: 15px;}
          .sidenav a {font-size: 18px;}
      }

      .sidebar-nav-info {
        padding: 30px;
        position: absolute;
        bottom: 0;
        left: 0;
        width: 200px;
        color: #E2E2E2;
        font-size: 12px;
      }


/** HAMBURGER ICON **/
      span.hamburger {
        position: sticky;
        top: 30px;
        left: 30px;
        font-size: 30px;
        font-weight: 800;
        cursor: e-resize;
      }

JS JS

<script>
/* Set the width of the side navigation to 250px and the left margin of the page content to 250px */
function openNav() {
    document.getElementById("mySidenav").style.width = "300px";
    document.getElementById("main").style.marginLeft = "300px";
}

/* Set the width of the side navigation to 0 and the left margin of the page content to 0 */
function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft = "0";
}
</script>

As you have suspected, this is caused by the href= in the a tag. 正如你所怀疑的,这是由引起href=a标签。 By default, a tags navigate the page somewhere, so having either href=# or as yours is written, will just navigate to top of page. 默认情况下,标签会在页面的某个位置导航,因此,具有href=#或您编写的代码将仅导航至页面顶部。

The return false should do the trick, but it is likely not in the correct place. return false应该可以解决问题,但是可能不在正确的位置。 Try putting it inside the closeNav function, as the final statement. 尝试将其放入closeNav函数中,作为最终声明。 Else, you might need to create an event listener for a tags and just respond with the return false. 否则,您可能需要为标签创建事件侦听器,并仅使用return false进行响应。 You can also try event.preventDefault() instead of return false (but return false should work - it does more than preventDefault. 您也可以尝试使用event.preventDefault()代替return false (但是return false应该可以工作-它比preventDefault更有效。

Update: 更新:

One thing that helps is to avoid using inline javascript (that is, where the js is on the tag itself: onclick="closeNav();return false;" ). 有帮助的一件事是避免使用嵌入式javascript(即js在标签本身上的位置: onclick="closeNav();return false;" )。 Instead, create an event listener and do your js there. 相反,创建一个事件侦听器,然后在其中执行您的js。 Here is a technical discussion regarding inline-javascript versus event listeners (the prevailing wisdom being that inline-javascript is more fraught and less desirable, particularly with respect to event bubbling, which is what you are dealing with here.) Here is a more simple article. 这是关于内联javascript与事件侦听器的技术讨论 (普遍的看法是内联javascript更令人烦恼且不太可取,尤其是在事件冒泡方面,这是您要在此处处理的问题。) 这里更为简单文章。

So, switch to an event listener structure, like this (untested): 因此,切换到事件监听器结构,如下所示(未测试):

 var cbel = document.getElementsByClassname("closebtn"); cbel.addEventListener("click", closeNav, false); var opel = document.getElementsByClassname("hamburger"); opel.addEventListener("click", openNav, false); /* Set the width of the side navigation to 250px and the left margin of the page content to 250px */ function openNav() { document.getElementById("mySidenav").style.width = "300px"; document.getElementById("main").style.marginLeft = "300px"; return false; } /* Set the width of the side navigation to 0 and the left margin of the page content to 0 */ function closeNav() { document.getElementById("mySidenav").style.width = "0"; document.getElementById("main").style.marginLeft = "0"; return false; } 

Final point: I would also recommend that you look into jQuery, because: 最后一点:我还建议您研究jQuery,因为:

1) You are using bootstrap, which uses jQuery itself, so it is already loaded 1)您正在使用引导程序,它使用jQuery本身,因此已经加载

2) It is much less typing 2)少打字

3) Most people (myself included) find it much simpler. 3)大多数人(包括我自己)发现它要简单得多。 for example, the above code in jQuery would look like this: 例如,上面的jQuery代码如下所示:

$(function(){

    $('.closebtn').click(function(){
        $("#mySidenav").css('width', '300px');
        $("#main").css('margin-left', '300px');
    });

});//END document.ready

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM