意外的 CSS 溢出故障



我一直在使用CSS溢出时遇到一个问题。

我有一个页面,我打算响应。导航菜单是导致我的问题的原因。

特别是"溢出-x:隐藏"声明...

起初我认为这个问题可能与 devtools 有关(或者更确切地说是我对它的滥用(,但我已经在两部手机上测试了该网站,那里是一样的......

有趣的是,在出现点击事件之前,溢出是如何不隐藏的,我认为解决方案与这一事实有关......也许哈哈

https://www.youtube.com/watch?v=_hsCahD-1Ig

我真的很感激任何人看一下视频并给我一些建议。谢谢。

更新:正如所指出的...我也应该添加代码...

HTML (index.html(:

<!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=Poppins" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<title>Responsive Sliding Nav</title>

</head>    
<body>
<nav>
<div class="logo">
<h4>NAV BAR</h4>
</div>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Work</a></li>
<li><a href="#">Projects</a></li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>

</nav>
<script src="app.js"></script>
</body>
</html>

CSS (style.css(:

*{
margin: 0px;
padding: 0px;
box-sizing: border-box;

}
html {
background: url(tst.jpg);
//background-size: auto 100vh;
background-size: cover;
background-repeat: no-repeat
}
nav{
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
// background-color: #5D4954;
background-color: rgba(93, 73, 84, 0.79);
font-family: 'Poppins', sans-serif;
}
.logo{
color: rgb(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links{
display: flex;
justify-content: space-around;
width: 50%;
}
.nav-links li{
list-style: none;
}
.nav-links a{
color: rgb(226, 226, 226);
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
}
.burger{
display: none;
cursor: pointer;
}
.burger div{
width: 25px;
height: 3px;
background-color: rgb(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: #5D4954;
background-color: rgba(93, 73, 84, 0.7);

display: flex;
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;
}
}
.nav-active {
transform: translateX(0%);
}
@keyframes navLinkFade {
from{
opacity: 0;
transform: translateX(50px);
}
to{
opacity: 1;
transform: translateX(0px);
}
}

JS(应用程序.js(:

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 + 0.5}s`;
}
});
});
}
navSlide();

由于您没有提供有关如何创建所有内容(HTML/CSS/Javascript(的任何信息,因此仅通过观看视频我唯一能帮助您的是您可能从未设置过html或正文宽度/高度。因此,请在CSS文件中添加以下行:

html, body {
width: 100%;
height: 100%
}

但下次,请务必提供更多细节和代码

你可以尝试添加!important属性,如下所示:

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

如果这不起作用,您应该尝试将 Overflow-X:Hidden 也添加到 HTML 中。尝试带和不带!重要。 希望这能解决您的问题。

尝试将显示属性从 .nav-links 移动到 .nav-active 类并设置 .nav-links display:none 像这样的东西

.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
/* background-color: #5D4954; */
background-color: rgba(93, 73, 84, 0.7);
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
display: none;
}
.nav-active {
transform: translateX(0%);
display: flex;
flex-direction: column;
align-items: center;
}

希望这对:)有所帮助 干杯

最新更新