gpt4 book ai didi

html - 无法对齐和增加切换导航栏的宽度

转载 作者:行者123 更新时间:2023-11-28 00:23:56 24 4
gpt4 key购买 nike

我创建了一个切换导航栏,只要单击该按钮,菜单和动画就会出现。但是,我在某些部分遇到了问题。

  1. 我的内容没有在页面右侧对齐,主要是按钮本身。

  2. 我正在努力让我的菜单背景(薰衣草色)在出现时填满页面的整个宽度和高度。

我尝试了几种方法,包括“float: right”、“width: 100vw”、“height: 100vh”等等,但没有成功。

下面是我的代码,以及指向我试图实现的外观的链接。任何帮助将不胜感激。谢谢!

示例:http://madetogether.com.au/case-study/sprout/

<!-- HTML -->
<!-- Navigation Bar -->
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul id="menu">
<li>
<a href="#page1" class="nav-link">
Services
</a>
</li>
<li class="nav-item">
<a href="#page3" class="nav-link">
About
</a>
</li>
<li class="nav-item">
<a href="#page4" class="nav-link">
Contact
</a>
</li>
</ul>

/* CSS */
/* Navigation Bar */
#menuToggle {
display: block;
position: relative;
top: 1em;
left: 1em;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}

#menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
/* top: -7px;
left: -5px; */
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}

a {
text-decoration: none;
color: black;
transition: color 0.3s ease;
}

a:hover {
color: red;
}

#menuToggle {
display: block;
position: relative;
top: 1em;
left: 1em;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}

#menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}

#menuToggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: black;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}

#menuToggle span:first-child {
transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}

#menuToggle input:checked ~ span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}

#menuToggle input:checked ~ span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}

#menuToggle input:checked ~ span:nth-last-child(2) {
transform: rotate(-45deg) translate(0, -1px);
}

#menu {
position: absolute;
width: 100%;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: lavender;
opacity: .85;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li {
padding: .5em 0;
font-size: 1.5em;
text-align: right;
}

#menuToggle input:checked ~ ul {
transform: none;
}

最佳答案

用外部 div 包裹菜单图标 span 并在其右侧添加 float ,复选框使用右侧 0

更新后的代码如下

更新

我尽我所能使用 CSS 获得 100% 高度背景,但失败了。所以我用 JavaScript 欺骗了它。

  function toggleHeight(){
var navElement = document.getElementsByTagName("nav")[0];
navElement.classList.toggle("fullHeight");

var menuElement = document.getElementById("menu");
menuElement.classList.toggle("fullHeight");

var menuToggleElement = document.getElementById("menuToggle");
menuToggleElement.classList.toggle("fullHeight");
}
#menuToggle {
display: block;
position: relative;
top: 1em;
left: 1em;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}

#menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
/* top: -7px;
left: -5px; */
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}

a {
text-decoration: none;
color: black;
transition: color 0.3s ease;
}

a:hover {
color: red;
}

#menuToggle {
display: block;
position: relative;
top: 1em;
left: 1em;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}

#menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
right: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}

#menuToggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: black;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}

#menuToggle span:first-child {
transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}

#menuToggle input:checked ~ span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}

#menuToggle input:checked ~ span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}

#menuToggle input:checked ~ span:nth-last-child(2) {
transform: rotate(-45deg) translate(0, -1px);
}

#menu {
position: absolute;
width: 100%;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: lavender;
opacity: .85;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li {
padding: .5em 0;
font-size: 1.5em;
text-align: right;
}

#menuToggle input:checked ~ ul {
transform: none;
}
.menuIcon {
float: right;
}

html, body {
height: 100%;
margin: 0px;
}

.fullHeight {
height: 100% !important;
}
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" onclick="toggleHeight()"/>
<div class="menuIcon">
<span></span>
<span></span>
<span></span>
</div>
<ul id="menu">
<li>
<a href="#page1" class="nav-link">
Services
</a>
</li>
<li class="nav-item">
<a href="#page3" class="nav-link">
About
</a>
</li>
<li class="nav-item">
<a href="#page4" class="nav-link">
Contact
</a>
</li>
</ul>

关于html - 无法对齐和增加切换导航栏的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com