为什么媒体查询在通过 javascript 更改后不更新值?
Why isn't media query updating values after they are changed via javascript?
我有一个非常标准的导航栏,其中包含一个包含指向我的子站点的链接的列表。我在网站上添加了一个汉堡菜单图标,它应该出现在小屏幕和移动屏幕上。此外,我通过 css 媒体查询将它们的字体大小设置为零来隐藏链接。如果单击菜单图标,我会触发一个 javascript 函数,这将相应地 increase/decrease 链接的字体大小。
一切都很好,只有一个问题。在脚本更改了链接的字体大小后,一旦我调整了浏览器的大小,这些值就会被保留,并且由于某种原因不会被媒体查询更新。因此,根据移动菜单是打开还是关闭,字体大小要么非常大,要么为零。为什么在将浏览器调整回全屏时这些值没有更新?
包含复制所需代码的代码片段:
var open = false;
function openmobilemenu() {
var nav = document.getElementsByTagName("nav");
var links = nav[0].getElementsByTagName("li");
if (!open) {
for (var i = 0; i < links.length; i++) {
links[i].style.transition = "0.5s";
links[i].style.fontSize = "10vw";
}
open = true;
}
else {
for (var i = 0; i < links.length; i++) {
links[i].style.fontSize = "0";
}
open = false;
}
}
header {
position: relative;
width: 100%;
height: 200px;
background-image: url("../img/header.png");
background-repeat: no-repeat;
background-position: right;
background-size: auto 100%;
background-color: #CDCCCA;
}
header img {
position: absolute;
width: 500px;
padding: 0 15%;
bottom: 10px;
}
.mobilemenu {
display: none;
}
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: 100px;
}
nav ul {
position: absolute;
bottom: 0;
width: 70%;
list-style: none;
padding: 0 15%;
display: flex;
margin: 0;
}
nav li {
width: 125px;
text-align: center;
transition: none;
}
.navunderline {
width: 125px;
height: 0;
margin: 5px 0 0 0;
background-color: #DAD9D7;
transition: 500ms;
}
nav a {
color: #DAD9D7;
}
nav a:hover {
text-decoration: none;
}
nav li:hover .navunderline {
height: 5px;
margin: 0;
}
@media screen and (max-width: 800px), (hover:none) {
.mobilemenu {
display: flex;
color: #61625B;
font-size: 100px;
margin: auto 5%;
}
.mobilemenu a, a:hover, a:active {
text-decoration: none;
}
nav {
position: relative;
background-color: #61625B;
width: 100%;
min-height: 10px;
height: auto;
overflow: visible;
padding: 0;
margin: 0;
}
nav ul {
position: relative;
height: auto;
bottom: 0;
width: 100%;
list-style: none;
flex-direction: column;
padding: 0;
}
nav li {
width: 100%;
text-align: center;
margin: 0;
padding: 0;
font-size: 0;
height: auto;
}
nav li:hover {
background-color: #8b131f;
}
.navunderline {
display: none;
}
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<header>
<a href="index.html"><img src="img/logo.png" alt="some alt" /></a>
<a href="javascript:void(0);" class="mobilemenu" onclick="openmobilemenu()">
<i class="fa fa-bars"></i>
</a>
</header>
<nav>
<ul>
<li><a href="content/unternehmen.html">Unternehmen</a><div class="navunderline"></div></li>
<li><a href="content/leistungen.html">Leistungen</a><div class="navunderline"></div></li>
<li><a href="content/referenzen.html">Referenzen</a><div class="navunderline"></div></li>
<li><a href="content/news.html">News</a><div class="navunderline"></div></li>
<li><a href="content/kontakt.html">Kontakt</a><div class="navunderline"></div></li>
</ul>
</nav>
这是因为您的 JS 正在您的元素上设置内联样式,而内联样式总是比样式表中的任何内容都更具体。
解决这个问题的方法有以下三种:
- 在 window 调整大小时使用 JS 以删除这些样式。
- 不内联样式,但 add/remove 类 在调整大小的那些元素上。使用您的样式表来控制这些元素的样式。
- 在您的样式表中将字体样式设置为
!important
(绕过特殊性的唯一方法 - 不推荐)
我有一个非常标准的导航栏,其中包含一个包含指向我的子站点的链接的列表。我在网站上添加了一个汉堡菜单图标,它应该出现在小屏幕和移动屏幕上。此外,我通过 css 媒体查询将它们的字体大小设置为零来隐藏链接。如果单击菜单图标,我会触发一个 javascript 函数,这将相应地 increase/decrease 链接的字体大小。
一切都很好,只有一个问题。在脚本更改了链接的字体大小后,一旦我调整了浏览器的大小,这些值就会被保留,并且由于某种原因不会被媒体查询更新。因此,根据移动菜单是打开还是关闭,字体大小要么非常大,要么为零。为什么在将浏览器调整回全屏时这些值没有更新?
包含复制所需代码的代码片段:
var open = false;
function openmobilemenu() {
var nav = document.getElementsByTagName("nav");
var links = nav[0].getElementsByTagName("li");
if (!open) {
for (var i = 0; i < links.length; i++) {
links[i].style.transition = "0.5s";
links[i].style.fontSize = "10vw";
}
open = true;
}
else {
for (var i = 0; i < links.length; i++) {
links[i].style.fontSize = "0";
}
open = false;
}
}
header {
position: relative;
width: 100%;
height: 200px;
background-image: url("../img/header.png");
background-repeat: no-repeat;
background-position: right;
background-size: auto 100%;
background-color: #CDCCCA;
}
header img {
position: absolute;
width: 500px;
padding: 0 15%;
bottom: 10px;
}
.mobilemenu {
display: none;
}
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: 100px;
}
nav ul {
position: absolute;
bottom: 0;
width: 70%;
list-style: none;
padding: 0 15%;
display: flex;
margin: 0;
}
nav li {
width: 125px;
text-align: center;
transition: none;
}
.navunderline {
width: 125px;
height: 0;
margin: 5px 0 0 0;
background-color: #DAD9D7;
transition: 500ms;
}
nav a {
color: #DAD9D7;
}
nav a:hover {
text-decoration: none;
}
nav li:hover .navunderline {
height: 5px;
margin: 0;
}
@media screen and (max-width: 800px), (hover:none) {
.mobilemenu {
display: flex;
color: #61625B;
font-size: 100px;
margin: auto 5%;
}
.mobilemenu a, a:hover, a:active {
text-decoration: none;
}
nav {
position: relative;
background-color: #61625B;
width: 100%;
min-height: 10px;
height: auto;
overflow: visible;
padding: 0;
margin: 0;
}
nav ul {
position: relative;
height: auto;
bottom: 0;
width: 100%;
list-style: none;
flex-direction: column;
padding: 0;
}
nav li {
width: 100%;
text-align: center;
margin: 0;
padding: 0;
font-size: 0;
height: auto;
}
nav li:hover {
background-color: #8b131f;
}
.navunderline {
display: none;
}
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<header>
<a href="index.html"><img src="img/logo.png" alt="some alt" /></a>
<a href="javascript:void(0);" class="mobilemenu" onclick="openmobilemenu()">
<i class="fa fa-bars"></i>
</a>
</header>
<nav>
<ul>
<li><a href="content/unternehmen.html">Unternehmen</a><div class="navunderline"></div></li>
<li><a href="content/leistungen.html">Leistungen</a><div class="navunderline"></div></li>
<li><a href="content/referenzen.html">Referenzen</a><div class="navunderline"></div></li>
<li><a href="content/news.html">News</a><div class="navunderline"></div></li>
<li><a href="content/kontakt.html">Kontakt</a><div class="navunderline"></div></li>
</ul>
</nav>
这是因为您的 JS 正在您的元素上设置内联样式,而内联样式总是比样式表中的任何内容都更具体。
解决这个问题的方法有以下三种:
- 在 window 调整大小时使用 JS 以删除这些样式。
- 不内联样式,但 add/remove 类 在调整大小的那些元素上。使用您的样式表来控制这些元素的样式。
- 在您的样式表中将字体样式设置为
!important
(绕过特殊性的唯一方法 - 不推荐)