【问题标题】:Cant get drop 3 bar menu to have a background or close无法获得下拉 3 栏菜单以获得背景或关闭
【发布时间】: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">&#9776;</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


【解决方案1】:

所以,你有一些小问题,我不知道这是否只是在你复制代码时发生的,但这里是:

首先(再次,对不起,如果你已经有了这个,但是因为你复制的不是我会假设这是问题之一)你需要 $(document).ready,你需要把你的js代码在里面,否则它不会运行。

JS

    $(document).ready(function() {
  $("#nav").addClass("js").before('<div id="menu">&#9776;</div>');
  //I prefer to have the onclick like this but I haven't deleted the myfunction you have, so you can still use that giving the function name from the html
  $('.dropbtn').click(function() {
    document.getElementById("myDropdown").classList.toggle("show");

  });
  $("#menu").click(function() {
    $("#nav").toggle();
  });
  $(window).resize(function() {
    if (window.innerWidth > 768) {
      $("#nav").removeAttr("style");
    }
  });



  // 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');
        }
      }
    }
  }


});

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

并且在 css 中你没有任何东西可以使背景着色,所以它什么也不做。最后,我认为如果您将width 作为其他内容而不是将100% 设置为ul,则不会占用整个页面。 CSS

  ul {
    width: 30%;
    background-color: aquamarine;
  }

这是JSFIDDLE

希望这会有所帮助!

【讨论】:

  • 太棒了!但是,当单击菜单时,三个栏被整个菜单覆盖,当您在菜单之外单击时,它不会关闭。这是为什么呢?
  • 好吧,这个还是这样吗,因为我不确定我是否理解正确:JSFIDDLE-LINK
  • 是的,还是一样。我假设它与我现有的 CSS 干扰有关?
  • 我认为也有几个 js 错误,尝试从浏览器上的开发者控制台跟踪它们
  • @biseilbutsu 它抓住了一个。它说“Uncaught ReferenceError: $ is not defined”代码是:$(document).ready(function() {
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2013-06-09
  • 2013-02-27
  • 2019-12-08
  • 1970-01-01
相关资源
最近更新 更多