为什么对齐项目不沿横轴居中?

时间:2019-11-11 11:33:12

标签: javascript html css flexbox

在我的移动视图中,我将显示.flex和流向的.nav-links(container)设置为column。现在,我添加了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>

1 个答案:

答案 0 :(得分:2)

似乎是由li上的translateX引起的-您正在将其从中间向右100px移动。

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

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>