我是HTML5,CSS和Javascript的新学员。如果我的问题非常基本,那么我很抱歉。我正在开发一个响应式导航菜单。当浏览器的宽度大于500时,存在垂直导航栏。一旦屏幕页面的大小小于500(例如在移动电话上),应隐藏水平导航栏,而是显示汉堡包图标。点击汉堡包菜单后,汉堡包图标下方会显示一个垂直导航栏。 但问题是当我在导航上调用setAttribute(" visibility"," visible")或setAttribute(" visibility"," hidden")时吧,它不起作用。我不知道为什么。 setAttribute被称为屏幕加载时或单击汉堡包图标时。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./css/normalize.css">
<link rel="stylesheet" href="./css/page-main.css">
<link rel="stylesheet" href="./css/master.css">
<link rel="stylesheet" href="./css/footer.css">
<!-- fonts -->
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans:300" rel="stylesheet">
<!-- fonts -->
<title>Home Page</title>
</head>
<body onload="hideORShow()">
<header>
<nav id="navigation-links" class="navbar navigation-box">
<div class="col-1of6 nav-item">
<a class="main-nav-item-page-index" href="./index.html">Home</a>
</div>
<div class="col-1of6 nav-item" >
<a class="main-nav-item-page-index" href="./spring-summer.html">Fall</a>
</div>
<div class="col-1of6 nav-item">
<a class="main-nav-item-page-index" href="./fall-winter.html">Winter</a>
</div>
<div class="col-1of6 nav-item">
<a class="main-nav-item-page-index" href="./leather.html">Leather</a>
</div>
</nav>
<div class="open-slide">
<a href="#">
<svg id="hamburgur-icon" onClick="openSlideMenu()">
<path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
<path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
<path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
</svg>
</a>
</div>
<div id="side-menu" class="side-nav" >
<a href="#" class="btn-close" onClick="closeSlideMenu()">×</a>
<a class="main-nav-item-page-index" href="./index.html">Home</a>
<a class="main-nav-item-page-index" href="./spring-summer.html">Fall</a>
<a class="main-nav-item-page-index" href="./fall-winter.html">Winter</a>
<a class="main-nav-item-page-index" href="./leather.html">Leather</a>
</div>
</header>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
这是我的javascript文件:
var hamburgerMenu = document.getElementById('hamburgur-icon');
var slidingMenu = document.getElementById('side-menu');
var navigationBox = document.getElementById('navigation-links');
function hideORShow(){
if(width > 500){
console.log("Inside of hideOrShow function - Window width is greater than 500px, hiding hamburger");
hamburgerMenu.setAttribute("visibility","hidden");
slidingMenu.setAttribute("visibility","hidden");
}
else{
console.log("Inside of hideOrShow function - Window width is less than 500px, hiding hamburger");
navigationBox.setAttribute("visibility","hidden");
hamburgerMenu.setAttribute("visibility","visible");
}
}
function openSlideMenu(){
console.log("hamburger btn clicked");
slidingMenu.setAttribute("visibility","visible");
slidingMenu.setAttribute("background-color","red");
}
function closeSlideMenu(){
console.log("close btn clicked");
slidingMenu.setAttribute("visibility","hidden");
}
我在chrome调试器中看到在触发事件后可见性属性发生了变化,但我仍然没有在浏览器中看到视觉上的变化。例如,当我单击关闭按钮时,汉堡图标下方的垂直导航栏仍然存在。 我会很感激任何提示。
答案 0 :(得分:2)
但问题是当我调用setAttribute(&#34; visibility&#34;,&#34; visible&#34;)或setAttribute(&#34; visibility&#34;,&#34; hidden&#34; )...
可见性是CSS样式,而不是元素属性。这就是为什么它无法正常工作的原因。尝试替换任何看起来像这样的行:
navigationBox.setAttribute("visibility","hidden");
hamburgerMenu.setAttribute("visibility","visible");
使用:
navigationBox.style.visibility = "hidden";
hamburgerMenu.style.visibility = "visible";
后者实际上为元素设置了visibility
CSS样式,而前者在元素上设置了一个属性(作为属性的可见性对它没有任何作用)。
答案 1 :(得分:1)
您尝试更改的是CSS样式,而不是HTML属性。有问题的属性是style
属性,但在JS中有一个简单的方法:
例如,
slidingMenu.style.visibility = "visible";
slidingMenu.style.backgroundColor = "red";