簡體   English   中英

漢堡按鈕不在正確的位置

[英]Hamburger button is not on the right place

我制作了一個網站,它是帶有粘性標題的移動響應式網站。 但是在 iPhone 上,漢堡按鈕不在正確的位置,並且也不能完美地與粘性標題一起移動。 我不知道這是因為 Safari,但是,我沒有在其他工具上遇到這個問題,只有在 iPhone 上。 如果我在一個地方改變漢堡按鈕的位置,它在另一部手機上看起來不太好。

我不使用 Bootstrap。 我的問題是,我怎樣才能讓我的漢堡按鈕在每部手機上都好看?

 window.onscroll = function() {myFunction()}; var header = document.getElementById("myHeader"); var sticky = header.offsetTop; function myFunction() { if (window.pageYOffset > sticky) { header.classList.add("sticky"); } else { header.classList.remove("sticky"); } }
 * { margin: 0px; padding: 0px; box-sizing: border-box; } body { font-family: 'Montserrat', sans-serif; margin: 0; padding: 0; } header { display: flex; height: 20vh; margin: auto; align-items: center; border-bottom: 1px solid var(--clr-accent); } .logo-container, .nav-links { display: flex; } .logo-container { flex: 1; position: relative; left: 5%; } .logo { font-weight: 400; margin: 5px; } #myLogo{ max-width: 120px; max-height: 120px; } .logo-container img{ max-width: 120px; max-height: 120px; } /* Logo container JS*/ .logo-container { display: 'none' } .logo-container.open { display: 'block' } nav { flex: 2; display: flex; /* Make nav a flexbox container , also this makes a new problem*/ } .nav-links { margin-left:15%; margin-right: 15%; justify-content: center; justify-content: space-between; list-style: none; flex: 1; /* Let it occupy rest of the container */ align-items: center; /* Align to the vertical center because logo is bigger. */ } .nav-link { color: var(--clr-dark); font-size:20px; text-decoration: none; font-weight: 600; } .sticky { position: fixed; top: 10; height: 20vh; width:100%; align-items: center; background: rgba(255, 255, 255, 1); } @keyframes drop { 0% { opacity: 0; transform: translateY(-80px); } 100% { opacity: 1; transform: translateY(0px); } } @media screen and (max-width: 767px){ /* Logo container JS*/ .logo-container { display: 'block' } .line{ width: 30px; height: 3px; background: var(--clr-accent); margin: 5px; } header{ background: white; } nav{ position: relative; } .hamburger{ position: fixed; /*this was absolute*/ cursor: pointer; right: 5%; top: 20%; transform: translate(-5%, -280%); /*this was 200 with absolute*/ z-index: 2; } .nav-links { margin-left:0%; margin-right: 0%; justify-content: space-evenly; background-color: var(--clr-light); position: fixed; height: 100vh; width:100%; flex-direction: column; clip-path: circle(0px at 57% 10%); -webkit-clip-path: circle(0px at 57% 10%); transition: all 1s ease-out; pointer-events: none; text-align: center; z-index: 1; } .nav-links.open{ clip-path: circle(1000px at 57% 10%); -webkit-clip-path: circle(1000px at 57% 10%); pointer-events:all; } .nav-links li{ opacity: 0; } .navlinks li a{ font-size: 25px; } .nav-links li:nth-child(1){ transition: all 0.5s ease 0.2s; } .nav-links li:nth-child(2){ transition: all 0.5s ease 0.4s; } .nav-links li:nth-child(3){ transition: all 0.5s ease 0.6s; } li.fade{ opacity: 1; } .nav-link { color: var(--clr-dark); font-size: 18px; } } @media screen and (max-width: 1024px) { .cta-select { border: 2px solid #f48024; background: transparent; color: #f48024; width: 100px; height: 35px; font-size: 12px; } .cta-add { background: #f48024; width: 100px; height: 35px; font-size: 12px; margin: 30px 0px 0px 10px; } .cover img { height: 65%; padding: 15px; /*safari*/ } .small-circle, .medium-circle, .big-circle { opacity: 0.25; } .nav-link { /* font-size:10px; */ text-decoration: none; font-weight: 600; } .logo-container { left: 2%; } .logo-container img{ max-width: 65px; max-height: 65px; } .calendar { right: 2%; visibility: hidden; } .logo-click{ display: none; visibility: hidden; } .header { top: 10; height: 20vh; width:100%; align-items: center; } .hamburger{ transform: translate(0%, -285%); /*this was 200 with absolute*/ z-index: 2; } } @media screen and (max-width: 420px) { .hamburger{ transform: translate(0%, -330%); z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 415px) { .hamburger{ transform: translate(0%, -300%); /*this was 200 with absolute*/ z-index: 2; } .cover img { height: 56%; margin: 5px; /*Safari*/ padding: 5px; } } @media screen and (max-width: 376px) { .hamburger{ transform: translate(0%, -320%); /*this was 200 with absolute*/ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 361px) { .hamburger{ transform: translate(0%, -270%); /*this was 200 with absolute*/ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 321px) { .hamburger{ transform: translate(0%, -245%); /*this was 200 with absolute*/ z-index: 2; } .cover img { height: 56%; border-radius: 50px; margin: 5px; padding: 5px; } }
 <body> <header class="header" id="myHeader"> <script src="https://www.google.com/recaptcha/api.js"></script> <nav role="navigation"> <div class="logo-container" id="myLogo"> <a href="#"><img src="./img/logo.png" alt="logo"/> </a> </div> <div class="hamburger" id="hamburgerID"> <div class="line"></div> <div class="line"></div> <div class="line"></div> </div> <ul class="nav-links"> <li><a class="nav-link" href="#details"> DETAILS</a></li> <li><a class="nav-link" href="#description">DESCRIPTION</a></li> <li><a class="nav-link" href="#aboutus">ABOUT US</a></li> </ul> </nav> </header>

第一種方法

添加align-items: center; 導航標簽。 那是

nav{align-items: center;}

同時去除top:20%; 漢堡班上。 那是

.hamburger{top:20%}

position:fixed元素至少分配top, left, bottom, right 之一

所以,這里正確:5% 就足夠了。

 window.onscroll = function() {myFunction()}; var header = document.getElementById("myHeader"); var sticky = header.offsetTop; function myFunction() { if (window.pageYOffset > sticky) { header.classList.add("sticky"); } else { header.classList.remove("sticky"); } }
 * { margin: 0px; padding: 0px; box-sizing: border-box; --clr-accent:#111; } body { font-family: 'Montserrat', sans-serif; margin: 0; padding: 0; } header { display: flex; height: 20vh; margin: auto; align-items: center; border-bottom: 1px solid var(--clr-accent); } .logo-container, .nav-links { display: flex; } .logo-container { flex: 1; position: relative; left: 5%; } .logo { font-weight: 400; margin: 5px; } #myLogo{ max-width: 120px; max-height: 120px; } .logo-container img{ max-width: 120px; max-height: 120px; } /* Logo container JS*/ .logo-container { display: 'none' } .logo-container.open { display: 'block' } nav { flex: 2; display: flex; /* Make nav a flexbox container , also this makes a new problem*/ align-items: center; } .nav-links { margin-left:15%; margin-right: 15%; justify-content: center; justify-content: space-between; list-style: none; flex: 1; /* Let it occupy rest of the container */ align-items: center; /* Align to the vertical center because logo is bigger. */ } .nav-link { color: var(--clr-dark); font-size:20px; text-decoration: none; font-weight: 600; } .sticky { position: fixed; top: 10px; height: 20vh; width:100%; align-items: center; background: rgba(255, 255, 255, 1); } @keyframes drop { 0% { opacity: 0; transform: translateY(-80px); } 100% { opacity: 1; transform: translateY(0px); } } @media screen and (max-width: 767px){ /* Logo container JS*/ .logo-container { display: 'block' } .line{ width: 30px; height: 3px; background: var(--clr-accent); margin: 5px; } header{ background: white; } nav{ position: relative; } .hamburger{ position: fixed; /*this was absolute*/ cursor: pointer; right: 5%; z-index: 2; } .nav-links { margin-left:0%; margin-right: 0%; justify-content: space-evenly; background-color: var(--clr-light); position: fixed; height: 100vh; width:100%; flex-direction: column; clip-path: circle(0px at 57% 10%); -webkit-clip-path: circle(0px at 57% 10%); transition: all 1s ease-out; pointer-events: none; text-align: center; z-index: 1; } .nav-links.open{ clip-path: circle(1000px at 57% 10%); -webkit-clip-path: circle(1000px at 57% 10%); pointer-events:all; } .nav-links li{ opacity: 0; } .navlinks li a{ font-size: 25px; } .nav-links li:nth-child(1){ transition: all 0.5s ease 0.2s; } .nav-links li:nth-child(2){ transition: all 0.5s ease 0.4s; } .nav-links li:nth-child(3){ transition: all 0.5s ease 0.6s; } li.fade{ opacity: 1; } .nav-link { color: var(--clr-dark); font-size: 18px; } } @media screen and (max-width: 1024px) { .cta-select { border: 2px solid #f48024; background: transparent; color: #f48024; width: 100px; height: 35px; font-size: 12px; } .cta-add { background: #f48024; width: 100px; height: 35px; font-size: 12px; margin: 30px 0px 0px 10px; } .cover img { height: 65%; padding: 15px; /*safari*/ } .small-circle, .medium-circle, .big-circle { opacity: 0.25; } .nav-link { /* font-size:10px; */ text-decoration: none; font-weight: 600; } .logo-container { left: 2%; } .logo-container img{ max-width: 65px; max-height: 65px; } .calendar { right: 2%; visibility: hidden; } .logo-click{ display: none; visibility: hidden; } .header { top: 10; height: 20vh; width:100%; align-items: center; } .hamburger{ z-index: 2; } } @media screen and (max-width: 420px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 415px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; /*Safari*/ padding: 5px; } } @media screen and (max-width: 376px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 361px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 321px) { .hamburger{ z-index: 2; } .cover img { height: 56%; border-radius: 50px; margin: 5px; padding: 5px; } } .scrolling{color:#fff;background:orange;height:800px;padding:30px;}
 <body> <header class="header" id="myHeader"> <script src="https://www.google.com/recaptcha/api.js"></script> <nav role="navigation"> <div class="logo-container" id="myLogo"> <a href="#"><img src="" alt="logo"/> </a> </div> <div class="hamburger" id="hamburgerID"> <div class="line"></div> <div class="line"></div> <div class="line"></div> </div> <ul class="nav-links"> <li><a class="nav-link" href="#details"> DETAILS</a></li> <li><a class="nav-link" href="#description">DESCRIPTION</a></li> <li><a class="nav-link" href="#aboutus">ABOUT US</a></li> </ul> </nav> </header> <div class="scrolling">Content Here</div> </body>

第二種方法

我們可以通過不翻譯而不是添加將漢堡圖標對齊到中心

top:20%; 

計算為

top: calc((20vh - 30px) / 2); // header height = 20vh and hamburger height = 30px makes half of both to align middle of header

 window.onscroll = function() {myFunction()}; var header = document.getElementById("myHeader"); var sticky = header.offsetTop; function myFunction() { if (window.pageYOffset > sticky) { header.classList.add("sticky"); } else { header.classList.remove("sticky"); } }
 * { margin: 0px; padding: 0px; box-sizing: border-box; --clr-accent:#111; } body { font-family: 'Montserrat', sans-serif; margin: 0; padding: 0; } header { display: flex; height: 20vh; margin: auto; align-items: center; border-bottom: 1px solid var(--clr-accent); } .logo-container, .nav-links { display: flex; } .logo-container { flex: 1; position: relative; left: 5%; } .logo { font-weight: 400; margin: 5px; } #myLogo{ max-width: 120px; max-height: 120px; } .logo-container img{ max-width: 120px; max-height: 120px; } /* Logo container JS*/ .logo-container { display: 'none' } .logo-container.open { display: 'block' } nav { flex: 2; display: flex; /* Make nav a flexbox container , also this makes a new problem*/ } .nav-links { margin-left:15%; margin-right: 15%; justify-content: center; justify-content: space-between; list-style: none; flex: 1; /* Let it occupy rest of the container */ align-items: center; /* Align to the vertical center because logo is bigger. */ } .nav-link { color: var(--clr-dark); font-size:20px; text-decoration: none; font-weight: 600; } .sticky { position: fixed; top: 10px; height: 20vh; width:100%; align-items: center; background: rgba(255, 255, 255, 1); } @keyframes drop { 0% { opacity: 0; transform: translateY(-80px); } 100% { opacity: 1; transform: translateY(0px); } } @media screen and (max-width: 767px){ /* Logo container JS*/ .logo-container { display: 'block' } .line{ width: 30px; height: 3px; background: var(--clr-accent); margin: 5px; } header{ background: white; } nav{ position: relative; } .hamburger{ position: fixed; /*this was absolute*/ cursor: pointer; right: 5%; top: calc((20vh - 30px) / 2); // header height = 20vh and hamburger height = 30px makes half of both to align middle of header z-index: 2; } .nav-links { margin-left:0%; margin-right: 0%; justify-content: space-evenly; background-color: var(--clr-light); position: fixed; height: 100vh; width:100%; flex-direction: column; clip-path: circle(0px at 57% 10%); -webkit-clip-path: circle(0px at 57% 10%); transition: all 1s ease-out; pointer-events: none; text-align: center; z-index: 1; } .nav-links.open{ clip-path: circle(1000px at 57% 10%); -webkit-clip-path: circle(1000px at 57% 10%); pointer-events:all; } .nav-links li{ opacity: 0; } .navlinks li a{ font-size: 25px; } .nav-links li:nth-child(1){ transition: all 0.5s ease 0.2s; } .nav-links li:nth-child(2){ transition: all 0.5s ease 0.4s; } .nav-links li:nth-child(3){ transition: all 0.5s ease 0.6s; } li.fade{ opacity: 1; } .nav-link { color: var(--clr-dark); font-size: 18px; } } @media screen and (max-width: 1024px) { .cta-select { border: 2px solid #f48024; background: transparent; color: #f48024; width: 100px; height: 35px; font-size: 12px; } .cta-add { background: #f48024; width: 100px; height: 35px; font-size: 12px; margin: 30px 0px 0px 10px; } .cover img { height: 65%; padding: 15px; /*safari*/ } .small-circle, .medium-circle, .big-circle { opacity: 0.25; } .nav-link { /* font-size:10px; */ text-decoration: none; font-weight: 600; } .logo-container { left: 2%; } .logo-container img{ max-width: 65px; max-height: 65px; } .calendar { right: 2%; visibility: hidden; } .logo-click{ display: none; visibility: hidden; } .header { top: 10; height: 20vh; width:100%; align-items: center; } .hamburger{ z-index: 2; } } @media screen and (max-width: 420px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 415px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; /*Safari*/ padding: 5px; } } @media screen and (max-width: 376px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 361px) { .hamburger{ z-index: 2; } .cover img { height: 56%; margin: 5px; padding: 5px; } } @media screen and (max-width: 321px) { .hamburger{ z-index: 2; } .cover img { height: 56%; border-radius: 50px; margin: 5px; padding: 5px; } } .scrolling{color:#fff;background:orange;height:800px;padding:30px;}
 <body> <header class="header" id="myHeader"> <script src="https://www.google.com/recaptcha/api.js"></script> <nav role="navigation"> <div class="logo-container" id="myLogo"> <a href="#"><img src="" alt="logo"/> </a> </div> <div class="hamburger" id="hamburgerID"> <div class="line"></div> <div class="line"></div> <div class="line"></div> </div> <ul class="nav-links"> <li><a class="nav-link" href="#details"> DETAILS</a></li> <li><a class="nav-link" href="#description">DESCRIPTION</a></li> <li><a class="nav-link" href="#aboutus">ABOUT US</a></li> </ul> </nav> </header> <div class="scrolling">Content Here</div> </body>

暫無
暫無

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

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