簡體   English   中英

如何阻止我的漢堡菜單在點擊時跳到頁面頂部。

[英]How to stop my Hamburger menu from jumping me to the top of my page on click.

當我單擊漢堡包圖標時,會出現我的菜單,但它也會使我跳到頁面頂部。 我想留在頁面上的原處。 我的標題是固定的,頁面似乎在菜單顯示之前就開始滾動。 是否可以在不使用任何JavaScript的情況下解決此問題。 如果我必須很好的話。

    <header>
    <div>
    <img src="images/logo.png">
    </div>

    <input id="burger" type="checkbox" />

    <label for="burger">
        <span></span>
        <span></span>
        <span></span>
     </label>

    <nav>    
    <ul>
         <li><a href="#">About Me</a></li>
        <li><a href="#">Contact Me</a></li>
        <li><a href="#">Link #3</a></li>
    </ul>  
     </nav>
    </header>

    a {
    text-decoration: none;
    }
    header img {
    position: fixed;
    top: 40px;
    left: 40px;
    z-index: 6;
    width: 150px;
    }

    header input + label {
    position: fixed;
    top: 40px;
    right: 40px;
    height: 20px;
    width: 15px;
    z-index: 5;
    }
    header input + label span {
    position: absolute;
    width: 100%;
    height: 2px;
    top: 50%;
    margin-top: -1px;
    left: 0;
    display: block;
    background: #020304;
    transition: 0.5s;
    }
    header input + label span:first-child {
    top: 3px;
    }
    header input + label span:last-child {
    top: 16px;
    }
    header label:hover {
    cursor: pointer;
    }
    header input:checked + label span {
    opacity: 0;
    top: 50%;
    }
    header input:checked + label span:first-child {
    opacity: 1;
    transform: rotate(405deg);
    }
    header input:checked + label span:last-child {
    opacity: 1;
    transform: rotate(-405deg);
    }
    header input ~ nav {
    background: white;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100px;
    z-index: 3;
    transition: 0.5s;
    transition-delay: 0.5s;
    overflow: hidden;
    }
    header input ~ nav > ul {
    text-align: center;
    position: absolute;
    top: 35%;
    left: 20%;
    right: 20%;
    list-style-type: none;
    }
    header input ~ nav > ul > li {
    opacity: 0;
    transition: 0.5s;
    transition-delay: 0s;
    }
    header input ~ nav > ul > li > a {
    text-decoration: none;
    text-transform: uppercase;
    color: #020304;
    font-weight: 700;
    font-family: sans-serif;
    display: block;
    padding: 30px;
    }
    header input:checked ~ nav {
    height: 100%;
    transition-delay: 0s;
    }
    header input:checked ~ nav > ul > li {
    opacity: 1;
    transition-delay: 0.5s;
    }

只需更改href屬性:

<a href="javascript:;">About Me</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM