JS/CSS - 函数 openNav() 不适用于主页(仅)
JS/CSS - Function openNav() not working on Homepage(Only)
出于某种原因,此元素(菜单元素)在网站 (XXXXXXXXXXXXXXXXX.com) 上不可点击,但只能在主页上点击。
该元素在其他页面和编辑器中运行良好。
我不知道是什么原因导致它在主页上 中断 。
我正在使用 Chrome 的调试器进行检查,但我仍然没有找到问题所在,更没有找到解决方法。
问:此问题的可能原因是什么?您认为什么是解决问题的良好起点?
网站:
www.somewhatmystical.com
菜单元素:
https://www.w3schools.com/code/tryit.asp?filename=GD13CSPL7EEC
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: 'Lato', sans-serif;
}
* {
box-sizing: border-box;
}
.column1 {
color: white;
float: left;
width: 50%;
padding-left: 200px;
padding-right: 100px;
padding-top: 200px;
text-align: right;
font-size: 50px;
height: 575px;
border-right: 1px solid gray;
background-color: black;
}
.column2 {
color: white;
float: right;
width: 50%;
text-align: left;
padding: 10px;
font-size: 22px;
padding-left: 100px;
padding-top: 150px;
text-decoration: none;
height: 575px;
background-color: black;
}
a:link {
color: white;
}
a:hover {
color: #C0A539;
}
.row:after {
content: "";
display: table;
clear: both;
}
.overlay {
height: 0px;
width: 100%;
position: fixed;
top: 0;
z-index: 1;
top: 0;
left: 0;
bottom: 0;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0);
overflow-x: hidden;
transition: 0.5s;
}
.overlay-content {
position: relative;
top: 25%;
width: 100%;
text-align: center;
margin-top: 30px;
color: #FFFFFF;
}
.overlay a {
padding: 8px;
text-decoration: none;
color: #FFFFFF;
display: block;
transition: 0.3s;
}
.overlay a:hover, .overlay a:focus {
color: #C0A539;
}
.overlay .closebtn {
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
}
@media screen and (max-height: 450px) {
.overlay a {
font-size: 20px
}
.overlay .closebtn {
font-size: 40px;
top: 15px;
right: 35px;
}
}
</style>
</head>
<body>
<div id="myNav" class="overlay">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<div class="overlay-content">
<div class="row">
<div class="column1">
<p>welcome to our online art journey. You can read our thoughts or you can simply write to us</p><br><br>
</div>
<div class="column2">
<a href="#" style="text-decoration: none;">blog</a>
<a href="#" style="text-decoration: none;">quotes</a>
<a href="#" style="text-decoration: none;">shop</a>
<a href="#" style="text-decoration: none;">about</a>
<a href="#" style="text-decoration: none;">contact</a>
<br><br><br><br><br>
</div>
</div>
</div>
</div>
<span style="font-size:50px;cursor:pointer;color:rgba(192,192,192,0);text-align:center;display:block;margin-top: -65px" onclick="openNav()">MENU</span>
<script>
function openNav() {
document.getElementById("myNav").style.height = "100%";
}
function closeNav() {
document.getElementById("myNav").style.height = "0";
}
</script>
</body>
</html>
可以点击。它确实在功能问题上是关于显示的。您可以更改显示高度,但您也应该尝试更改菜单中子元素的样式
<script>
function openNav() {
document.getElementById("myNav").style.display= "contents";
}
function closeNav() {
document.getElementById("myNav").style.height = "none";
}
</script>
把它放在div
中,用position: absolute
把div放在右上角,然后给它加上onclick
监听器。确保它有 width
和 height
.
要确认点击,您可以将 console.log
设置为初始事件处理函数。
问题是:
另一个元素正在使用“.overlay”class,它与菜单元素“.overlay”冲突class。
我只是将不同的 class 重命名为“.overlay1”
- 感谢 JBis 指出了这一点。
出于某种原因,此元素(菜单元素)在网站 (XXXXXXXXXXXXXXXXX.com) 上不可点击,但只能在主页上点击。
该元素在其他页面和编辑器中运行良好。
我不知道是什么原因导致它在主页上 中断 。
我正在使用 Chrome 的调试器进行检查,但我仍然没有找到问题所在,更没有找到解决方法。
问:此问题的可能原因是什么?您认为什么是解决问题的良好起点?
网站:
www.somewhatmystical.com
菜单元素: https://www.w3schools.com/code/tryit.asp?filename=GD13CSPL7EEC
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: 'Lato', sans-serif;
}
* {
box-sizing: border-box;
}
.column1 {
color: white;
float: left;
width: 50%;
padding-left: 200px;
padding-right: 100px;
padding-top: 200px;
text-align: right;
font-size: 50px;
height: 575px;
border-right: 1px solid gray;
background-color: black;
}
.column2 {
color: white;
float: right;
width: 50%;
text-align: left;
padding: 10px;
font-size: 22px;
padding-left: 100px;
padding-top: 150px;
text-decoration: none;
height: 575px;
background-color: black;
}
a:link {
color: white;
}
a:hover {
color: #C0A539;
}
.row:after {
content: "";
display: table;
clear: both;
}
.overlay {
height: 0px;
width: 100%;
position: fixed;
top: 0;
z-index: 1;
top: 0;
left: 0;
bottom: 0;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0);
overflow-x: hidden;
transition: 0.5s;
}
.overlay-content {
position: relative;
top: 25%;
width: 100%;
text-align: center;
margin-top: 30px;
color: #FFFFFF;
}
.overlay a {
padding: 8px;
text-decoration: none;
color: #FFFFFF;
display: block;
transition: 0.3s;
}
.overlay a:hover, .overlay a:focus {
color: #C0A539;
}
.overlay .closebtn {
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
}
@media screen and (max-height: 450px) {
.overlay a {
font-size: 20px
}
.overlay .closebtn {
font-size: 40px;
top: 15px;
right: 35px;
}
}
</style>
</head>
<body>
<div id="myNav" class="overlay">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<div class="overlay-content">
<div class="row">
<div class="column1">
<p>welcome to our online art journey. You can read our thoughts or you can simply write to us</p><br><br>
</div>
<div class="column2">
<a href="#" style="text-decoration: none;">blog</a>
<a href="#" style="text-decoration: none;">quotes</a>
<a href="#" style="text-decoration: none;">shop</a>
<a href="#" style="text-decoration: none;">about</a>
<a href="#" style="text-decoration: none;">contact</a>
<br><br><br><br><br>
</div>
</div>
</div>
</div>
<span style="font-size:50px;cursor:pointer;color:rgba(192,192,192,0);text-align:center;display:block;margin-top: -65px" onclick="openNav()">MENU</span>
<script>
function openNav() {
document.getElementById("myNav").style.height = "100%";
}
function closeNav() {
document.getElementById("myNav").style.height = "0";
}
</script>
</body>
</html>
可以点击。它确实在功能问题上是关于显示的。您可以更改显示高度,但您也应该尝试更改菜单中子元素的样式
<script>
function openNav() {
document.getElementById("myNav").style.display= "contents";
}
function closeNav() {
document.getElementById("myNav").style.height = "none";
}
</script>
把它放在div
中,用position: absolute
把div放在右上角,然后给它加上onclick
监听器。确保它有 width
和 height
.
要确认点击,您可以将 console.log
设置为初始事件处理函数。
问题是:
另一个元素正在使用“.overlay”class,它与菜单元素“.overlay”冲突class。
我只是将不同的 class 重命名为“.overlay1”
- 感谢 JBis 指出了这一点。