【发布时间】:2016-05-11 03:42:29
【问题描述】:
感谢 Gerad 的编辑。我尝试了代码,它关闭了,但仍然没有背景,链接宽度仍然占据屏幕。 我为我的网站的移动版本制作了一个 3 栏菜单,但是当点击它时,1,文本后面的菜单没有背景, 2 每页之间的间距真的很差,每页的超链接占据了屏幕的整个宽度,最后,当再次单击(页面无论如何都会越过按钮)或单击它时,它不会关闭。
如何获取背景,缩短间距和超链接区域,并在单击或随机单击附近时关闭?
我认为这是因为某些当前的 CSS 可能会干扰?
这是我的 CSS 代码:
@media screen and (max-width: 768px) {
#menu {
width:1.4em;
display: block;
background:#ddd;
font-size:1.35em;
text-align: center;
}
#nav.js {
display: none;
}
ul {
width:100%;
}
li {
width:100%;
border-right:none;
}
}
@media screen and (min-width: 768px) {
#menu {
display: none;
}
}
/* NAV */
.dropbtn {
background-color: #ffffff;
color: black;
border: none;
cursor: pointer;
display: block;
color: inherit;
text-decoration: none;
height: 3em;
line-height: 3em;
padding: 0 0.5em 0 0.5em;
outline: 0;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #ffffff;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #f1f1f1}
.show {display:block;}
JS
$("#nav").addClass("js").before('<div id="menu">☰</div>');
$("#menu").click(function(){
$("#nav").toggle();
});
$(window).resize(function(){
if(window.innerWidth > 768) {
$("#nav").removeAttr("style");
}
});
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<nav id="nav">
<ul>
<li><a href="#intro">Home</a></li>
<li><a href="#one">About</a></li>
<li><a href="#two">The Ship</a></li>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Skills</button>
<div id="myDropdown" class="dropdown-content">
<a href="/1/skills/index.html">Skills</a>
<a href="/1/skills/index.html#courses">Courses</a>
<a href="/1/skills/index.html#apprenticeships">Apprenticeships</a>
</div>
</div>
<li><a href="Journey/">Journey</a></li>
<li><a href="/1/memberships/">Join Us</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="https://www.justgiving.com/harwich-mayflower/"> <font color="purple" size="4"><strong>Donate<strong></font></a></li>
<br></br>
</ul>
</nav>
【问题讨论】:
-
你也可以分享html吗?你那里只有js和css。
-
@biseibutsu 对不起!添加了html
标签: javascript jquery html css menu