单击淡入div

时间:2019-03-22 10:41:43

标签: javascript jquery html css

我正在创建移动导航栏,并且目前对其进行了设置,因此,当单击/application/config/database.php时,它会显示在其上方-一切正常。但是我想为其添加一个不错的小功能,以便在单击后希望每个li淡入(或滑入)。控制台中没有错误,但是淡入功能无效。预先感谢。

menu
var menu = document.getElementById("mobile-menu-link");
var mobileNav = document.getElementById("mobile-nav");
var mobileNavContent = document.getElementById("mobile-nav-content");

$(document).ready(function() {
  menu.addEventListener("click", function() {
    if (mobileNav.style.height == "0px") {
      mobileNav.style.paddingTop = "15px";
      mobileNav.style.height = "130px";
      $("#mobile-nav li").each(function(i) {
        $(this).delay(100 * i).fadeIn(500);
      });
    } else {
      mobileNav.style.height = "0px";
    }
  });
});
html,
body {
  margin: 0;
  padding: 0;
  font-size: 21px;
  min-width: 400px;
}

div {
  display: block;
}

ul {
  list-style: none;
  list-style-image: none;
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: inherit;
}

#desktop-navbar {
  text-transform: uppercase;
  width: 100%;
  height: 90px;
  height: 13vh;
  position: fixed;
  z-index: 1;
}

#desktop-nav-wrapper {
  height: inherit;
  padding: 0 45px;
}

#desktop-nav-wrapper nav ul {
  float: right;
  padding-top: 35px;
  font-size: 66%;
}

#desktop-nav-wrapper nav li {
  position: relative;
  display: inline-block;
  padding-left: 25px;
  color: #000000;
  font-family: Questrial;
  font-weight: 700;
}

#mobile-nav {
  overflow: hidden;
  text-align: right;
  display: block;
  width: 100%;
}

#mobile-nav li {
  font-size: 95%;
  color: #000000;
  text-decoration: none;
  font-family: Questrial;
  font-weight: 700;
  margin-right: 45px;
}

#mobile-nav li:hover {
  color: #FFFFFF;
}

#desktop-logo {
  float: left;
}

.logo {
  font-size: 140%;
  margin-top: 24px;
}

#desktop-nav-wrapper nav ul {
  padding-top: 33px;
}

#desktop-navbar {
  width: 100%;
  height: 90px;
  height: 14vh;
  position: static;
}

#desktop-navbar .desktop-items {
  display: none;
}

#desktop-navbar #mobile-menu-link {
  display: block;
  font-size: 18px;
}

1 个答案:

答案 0 :(得分:0)

它不起作用,因为fadeIn表示它显示了隐藏的元素。您的li未隐藏。它们只是因为菜单的overflow-hiddenheight而没有显示。 fadeIn将用display:none覆盖display:list-item属性,并在指定间隔内将不透明度从0设置为100。

因此,您需要先隐藏它们(可以在css中使用display:none),并在显示它们之后,在隐藏菜单时再次将它们隐藏(在jQuery中可以),

var menu = document.getElementById("mobile-menu-link");
var mobileNav = document.getElementById("mobile-nav");
var mobileNavContent = document.getElementById("mobile-nav-content");

$(document).ready(function() {

  menu.addEventListener("click", function() {
    if (mobileNav.style.height == "0px") {
      mobileNav.style.paddingTop = "15px";
      mobileNav.style.height = "130px";
      $("#mobile-nav li").each(function(i) {
        $(this).delay(100 * i).fadeIn(500);
      });
    } else {
      mobileNav.style.height = "0px";
       $("#mobile-nav li").hide();
 
    }
  });
});
html,
body {
  margin: 0;
  padding: 0;
  font-size: 21px;
  min-width: 400px;
}

div {
  display: block;
}

ul {
  list-style: none;
  list-style-image: none;
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: inherit;
}

#desktop-navbar {
  text-transform: uppercase;
  width: 100%;
  height: 90px;
  height: 13vh;
  position: fixed;
  z-index: 1;
}

#desktop-nav-wrapper {
  height: inherit;
  padding: 0 45px;
}

#desktop-nav-wrapper nav ul {
  float: right;
  padding-top: 35px;
  font-size: 66%;
}

#desktop-nav-wrapper nav li {
  position: relative;
  display: inline-block;
  padding-left: 25px;
  color: #000000;
  font-family: Questrial;
  font-weight: 700;
}

#mobile-nav {
  overflow: hidden;
  text-align: right;
  text-align: right;
  display: block;
  width: 100%;
}

#mobile-nav li {
display:none;
  font-size: 95%;
  color: #000000;
  text-decoration: none;
  font-family: Questrial;
  font-weight: 700;
  margin-right: 45px;
}

#mobile-nav li:hover {
  color: #FFFFFF;
}

#desktop-logo {
  float: left;
}

.logo {
  font-size: 140%;
  margin-top: 24px;
}

#desktop-nav-wrapper nav ul {
  padding-top: 33px;
}

#desktop-navbar {
  width: 100%;
  height: 90px;
  height: 14vh;
  position: static;
}

#desktop-navbar .desktop-items {
  display: none;
}

#desktop-navbar #mobile-menu-link {
  display: block;
  font-size: 18px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mobile-nav" class="sidenav" style="height: 0;">
  <div id="mobile-nav-content">
    <nav>
      <ul>
        <li><a href="#home">Casa</a></li>
        <li><a href="#about">Sobre Mi</a></li>
        <li><a href="#services">Servicio</a></li>
        <li><a href="#gallery">Galería</a></li>
        <li><a href="#contact">Contacto</a></li>
      </ul>
    </nav>
  </div>
</div>

<div id="desktop-navbar">
  <div id="desktop-nav-wrapper">
    <h3 id="desktop-logo" class="logo"><a href="/">Logo </a></h3>
    <nav>
      <ul id="desktop-nav-content">
        <li id="mobile-menu-link"><a>Menu</a></li>
      </ul>
    </nav>
  </div>
</div>