叠加导航不透明度更改

时间:2016-12-21 14:38:52

标签: javascript jquery css navigation

我正在尝试将我的菜单显示在叠加层中,但是使用不透明度更改效果而不是高度更改。我在这里找到代码http://www.w3schools.com/howto/howto_js_fullscreen_overlay.asp 但是我不能按照我的意愿去做这件事。 我想摆脱高度0% - > 100%的变化而不是我想改变不透明度0-> 1来创造淡化效果。

function openNav() {
document.getElementById("myNav").style.height = "100%";
}

function closeNav() {
document.getElementById("myNav").style.height = "0%";
}

当我把它更改为:

function openNav() {
document.getElementById("myNav").style.opacity = "1";
}

function closeNav() {
document.getElementById("myNav").style.opacity = "0";
}

我的css对此:

.overlay {
height: 100%;
width: 100%;
position: fixed;
z-index: 999;
top: 0;
left: 0;
background-color: rgba(0,0,0, 0.9);
overflow-y: hidden;
transition: 0.5s;
}

覆盖页面加载时显示的菜单显示(不是按钮onclick)但是当我关闭它时我无法重新打开它。

这是我发现的小提琴http://jsfiddle.net/rubixx/ct3ve7pb/1/ 这就是我使用的基本相同的代码,但我想摆脱高度变化,只是不透明度fadein叠加。

我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

没有太多要改变的地方:

我们使用height: 0%来隐藏叠加层,而不是最初的display: none

而不是改变高度,我们使用fadeIn / fadeOut。

更新了小提琴:

http://jsfiddle.net/ct3ve7pb/4/



function openNav() {
    $("#myNav").fadeIn();
    $("#open-nav").hide();
}

function closeNav() {
    $("#myNav").fadeOut();
    $("#open-nav").show();
}

body {
    margin: 0;
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 100%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 1);
    overflow-y: hidden;
    display: none;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px !important;
}
#open-nav {
  background-color: transparent;
  border: none;
  position: absolute;
  top: 34px;
  right: 45px;
  font-size: 38px !important;
  transition-delay: 0.2s;
}

@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .closebtn {
    font-size: 40px !important;
    top: 15px;
    right: 35px;
  }
}

.open {
  height: 100%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>

<button id="open-nav" style="font-size:30px;cursor:pointer" onclick="openNav()">☰</button>
&#13;
&#13;
&#13;

相关问题