javascript - div 中的内容重叠

标签 javascript jquery html css

请点击打开,然后点击关闭。关闭位于 SLIDER div 中,但是当 slider div 的 width0 时,close 文本仍然出现。

function openNav() {
  document.getElementById("main").style.marginLeft = "320px";
  document.getElementById("slider").style.width = "320px";
}

function closeNav() {
  document.getElementById("main").style.marginLeft = "0px";
  document.getElementById("slider").style.width = "0px";
}
body {
  margin: 0;
  padding: 0;
  background: #f1f1f1
}
#main {
  margin-left: 0px;
  transition: all 0.5s
}
.header {
  width: 100%;
  min-width: 320px;
  height: 60px;
  background: #154760;
}
.slider {
  width: 0;
  height: 669px;
  background: #066;
  position: fixed;
  top: 0;
  left: 0;
  transition: all 0.5s;
  z-index: 1000;
  display: inline-block;
}
a {
  color: #fff
}
<div class="slider" id="slider">
  <div style="width:inherit;display:inline-block"> <a href="JavaScript:void()" onclick="closeNav()" style="display:inline-block"> CLOSE</a> 
  </div>
</div>
<div id="main">
  <div class="header"> <a href="JavaScript:void()" onclick="openNav()" style="margin-left:50px"> OPEN</a>
  </div>
</div>

最佳答案

▶ 1st 选项:

尝试将 overflow: hidden 添加到 .slider 中,如以下代码片段所示:

function openNav() {
  document.getElementById("main").style.marginLeft = "320px";
  document.getElementById("slider").style.width = "320px";
}

function closeNav() {
  document.getElementById("main").style.marginLeft = "0px";
  document.getElementById("slider").style.width = "0px";
}
body {
  margin: 0;
  padding: 0;
  background: #f1f1f1
}

#main {
  margin-left: 0px;
  transition: all 0.5s
}

.header {
  width: 100%;
  min-width: 320px;
  height: 60px;
  background: #154760;
}

.slider {
  width: 0;
  height: 669px;
  background: #066;
  position: fixed;
  top: 0;
  left: 0;
  transition: all 0.5s;
  z-index: 1000;
  display: inline-block;
  overflow: hidden;
}

a {
  color: #fff
}
<div class="slider" id="slider">
  <div style="width:inherit;display:inline-block"> <a href="JavaScript:void()" onclick="closeNav()" style="display:inline-block"> CLOSE</a> 
  </div>
</div>

<div id="main">
  <div class="header"> <a href="JavaScript:void()" onclick="openNav()" style="margin-left:50px"> OPEN</a>
  </div>
</div>


▶ 第二个nd选项:

或者,您可以尝试一些更聪明的方法,而不是使用 overflow: hidden。在此解决方案中,我删除了一个链接,并根据是否显示 slider 使另一个链接打开关闭

function openNav() {
  document.getElementById("main").style.marginLeft = "320px";
  document.getElementById("slider").style.width = "320px";
  document.getElementById("open-close").innerHTML = "CLOSE";
}

function closeNav() {
  document.getElementById("main").style.marginLeft = "0px";
  document.getElementById("slider").style.width = "0px";
  document.getElementById("open-close").innerHTML = "OPEN";
}

var opened = false;

document.getElementById("open-close").onclick = function() {
  opened = !opened;
  return (opened) ? openNav() : closeNav();
}
body {
  margin: 0;
  padding: 0;
  background: #f1f1f1
}

#main {
  margin-left: 0px;
  transition: all 0.5s
}

.header {
  width: 100%;
  min-width: 320px;
  height: 60px;
  background: #154760;
}

.slider {
  width: 0;
  height: 669px;
  background: #066;
  transition: all 0.5s;
  display: inline-block;
}

.slider,
#open-close {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
}

#open-close {
  color: #fff
}
<div class="slider" id="slider"></div>

<div id="main">
  <div class="header">
    <a id = "open-close" href="JavaScript:void()"> OPEN</a>
  </div>
</div>

关于javascript - div 中的内容重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765888/

相关文章:

jquery - 如何使用 JQuery 更改模型的值?

jquery - 立即引发完全火灾

javascript - 如何使用动态 ID 隐藏动态创建的 html 元素

javascript - 从 Vue.Js 中的下拉列表中获取项目属性

css - HTML 输入,没有焦点的闪烁插入符?

html - 'ag-grid' 表格单元格中的自定义迷你图内的 D3 工具提示在表格单元格边界外不可见

javascript - Jasmine 模拟 ajax 调用在 IE 中不起作用

javascript - Contenteditable - 从插入符号提取文本到元素末尾

javascript - 使用 jQuery/JS 确定星期几

javascript - 动态访问 knockoutjs 可观察数组的属性