gpt4 book ai didi

javascript - 为什么对齐元素不沿交叉轴居中?

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:41 25 4
gpt4 key购买 nike

在我的移动 View 中,我有 .nav-links(container) 显示 flex 和 flow-direction 设置为列。现在,我添加了 align-items 以将横轴设置为中心,但它没有在容器中居中显示。我尝试在 li 元素周围放置一个边框并将宽度设置为 100%,但它似乎在左侧显示了一个边距。我已将 margin 设置为 0 并检查以确保但无济于事。到目前为止,我只在 chrome 上测试过这个。任何想法都会非常感谢。

const navSlide = () => {
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");

burger.addEventListener("click", ()=>{
// Toggle Nav
nav.classList.toggle("nav-active");

// Animate Links
navLinks.forEach((link, index) => {
if(link.style.animation){
link.style.animation = ""
}
else {
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + .5}s`;
}
});

//burger Animation
burger.classList.toggle("toggle");


});


}

navSlide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

nav{
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: rgb(83, 54, 110);
font-family: 'Raleway', sans-serif;
}

.logo{
color: rgba(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}

.nav-links{
display: flex;
justify-content: space-around;
width: 30%;
}

.nav-links li{
list-style: none;
}

.nav-links a{
color: rgba(226, 226, 226);
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
}
.burger{
display: none;
}
.burger div{
width: 25px;
height: 3px;;
background-color: rgba(226, 226, 226);
margin: 5px;
transition: all 0.3s ease;
}

@media screen and (max-width:1024px){
.nav-links{
width: 60%;
}
}

@media screen and (max-width:768px){
body{
overflow-x: hidden;
}

.nav-links{
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
background-color: rgb(83, 54, 110);
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}

.nav-links li{
opacity: 0;
}


.burger{
display: block;
cursor: pointer;
}
}

.nav-active{
transform: translateX(0%);
}

@keyframes navLinkFade{
from{
opacity: 0;
transform: translateX(50px);
}
to{
opacity: 1;
transform: translateX(100px);
}
}

.toggle .line1{
transform: rotate(-45deg) translate(-5px, 6px);
}
.toggle .line2{
opacity: 0;
}
.toggle .line3{
transform: rotate(45deg) translate(-5px, -6px);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="normalize.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Sushi Gen</title>
</head>
<body>
<nav>
<div class="logo">
<h4>Sushi Gen</h4>
</div>
<ul class="nav-links">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>

<script src="sushiGen.js"></script>
</body>
</html>

最佳答案

似乎是由 li 上的 translateX 引起的 - 您将它从中间向右移动 100 像素。

如果您删除该动画,那么您的链接将居中:

const navSlide = () => {
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");

burger.addEventListener("click", ()=>{
// Toggle Nav
nav.classList.toggle("nav-active");

// Animate Links
navLinks.forEach((link, index) => {
if(link.style.animation){
link.style.animation = ""
}
else {
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + .5}s`;
}
});

//burger Animation
burger.classList.toggle("toggle");


});


}

navSlide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

nav{
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: rgb(83, 54, 110);
font-family: 'Raleway', sans-serif;
}

.logo{
color: rgba(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}

.nav-links{
display: flex;
justify-content: space-around;
width: 30%;
}

.nav-links li{
list-style: none;
}

.nav-links a{
color: rgba(226, 226, 226);
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
}
.burger{
display: none;
}
.burger div{
width: 25px;
height: 3px;;
background-color: rgba(226, 226, 226);
margin: 5px;
transition: all 0.3s ease;
}

@media screen and (max-width:1024px){
.nav-links{
width: 60%;
}
}

@media screen and (max-width:768px){
body{
overflow-x: hidden;
}

.nav-links{
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
background-color: rgb(83, 54, 110);
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}

.nav-links li{
opacity: 0;
}


.burger{
display: block;
cursor: pointer;
}
}

.nav-active{
transform: translateX(0%);
}

@keyframes navLinkFade{
from{
opacity: 0;
}
to{
opacity: 1;
}
}

.toggle .line1{
transform: rotate(-45deg) translate(-5px, 6px);
}
.toggle .line2{
opacity: 0;
}
.toggle .line3{
transform: rotate(45deg) translate(-5px, -6px);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="normalize.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Sushi Gen</title>
</head>
<body>
<nav>
<div class="logo">
<h4>Sushi Gen</h4>
</div>
<ul class="nav-links">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Home</a>
</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>

<script src="sushiGen.js"></script>
</body>
</html>

关于javascript - 为什么对齐元素不沿交叉轴居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58800547/

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