javascript - 将网站分为 3 个部分 -> 1 个水平部分和 2 个垂直部分

标签 javascript jquery html css

我在屏幕左侧有一个边栏。我可以通过按一个按钮来切换它。右边是内容。

我想把按钮放在顶部的水平条上。侧边栏似乎盖住了这个栏,所以我看不到按钮。

这是我当前的代码:

HTML 文件:

    <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>
</title>
</head>

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>

<script src="MainController.js"></script>
<link rel="stylesheet" type="text/css" href="MainStyle.css">

<body onload="InitDocument()">

<div id="topBar">
  <button id="btnNavToggle" type="button" onclick="ToggleNavbar()">Menu</button>
</div>

<div id="container">
<div id="sideNav">
<button type="button" onclick="NewEntry()">+</button>
<p>test</p>
</div>
<div id="mainArea">
<p>Title:</p>
<input id="titleInputField" type="text">
<p>Text:</p>
<textarea id="textArea"> </textarea>
<p></p>
<button type="button" onclick="SaveEntry()">Save</button>
</div>
</div>

</body>
</html>

CSS 文件:

    body{
  background-color: #EEEEEE;
  color: #000000;
}

* {
    margin: 0;
    padding: 0;
}

#sideNav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  overflow-x: hidden;
  background-color: #333333;
  color: #EEEEEE;
}

Js 文件:

var navIsOpen = true;

function InitDocument(){ // Initialization
  ToggleNavbar();
}

function ToggleNavbar(){ // show - hide the navbar
  var sideNavWidth = "0px";
  var mainAreaWidth = "0px";
  if (navIsOpen)
  {
    sideNavWidth = "200px";
    mainAreaWidth = "200px";
  }
  $("#sideNav").width(sideNavWidth);
  $("#mainArea").css('margin-left',mainAreaWidth);
  navIsOpen = !navIsOpen;
}

function SaveEntry(){ // save the entry
  var txtTitle = $("#titleInputField").val();
  var txtField = $("#textArea").val();
  alert(txtTitle + "#" + txtField);
}

function NewEntry() { // create a new entry
  alert("neuer Eintrag");
}

这就是我想要实现的目标

enter image description here

看来我只需要修复 CSS 即可完成。

最佳答案

我将 margin-top:0; 添加到你的 topBar 并从你的 sideNav 中删除了 top: 0;

试试这个:

var navIsOpen = true;

function InitDocument(){ // Initialization
  ToggleNavbar();
}

function ToggleNavbar(){ // show - hide the navbar
  var sideNavWidth = "0px";
  var mainAreaWidth = "0px";
  if (navIsOpen)
  {
    sideNavWidth = "200px";
    mainAreaWidth = "200px";
  }
  $("#sideNav").width(sideNavWidth);
  $("#mainArea").css('margin-left',mainAreaWidth);
  navIsOpen = !navIsOpen;
}

function SaveEntry(){ // save the entry
  var txtTitle = $("#titleInputField").val();
  var txtField = $("#textArea").val();
  alert(txtTitle + "#" + txtField);
}

function NewEntry() { // create a new entry
  alert("neuer Eintrag");
}
body{
  background-color: #EEEEEE;
  color: #000000;
}

*{
  margin: 0;
  padding: 0;
}

#topBar {
  margin-top:0;
  background-color: navy;
}

#sideNav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  left: 0;
  overflow-x: hidden;
  background-color: #333333;
  color: #EEEEEE;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>
    </title>
</head>

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>

<script src="MainController.js"></script>
<link rel="stylesheet" type="text/css" href="MainStyle.css">

<body onload="InitDocument()">

    <div id="topBar">
        <button id="btnNavToggle" type="button" onclick="ToggleNavbar()">Menu</button>
    </div>

    <div id="container">
        <div id="sideNav">
            <button type="button" onclick="NewEntry()">+</button>
            <p>test</p>
        </div>
        <div id="mainArea">
            <p>Title:</p>
            <input id="titleInputField" type="text">
            <p>Text:</p>
            <textarea id="textArea"> </textarea>
            <p></p>
            <button type="button" onclick="SaveEntry()">Save</button>
        </div>
    </div>

</body>

</html>

关于javascript - 将网站分为 3 个部分 -> 1 个水平部分和 2 个垂直部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593990/

相关文章:

html - 在 HTML 中将文本与标题标签 (h2) 对齐

html - 相对定位的元素不占用其子元素的高度

c# - WebSocket 服务器未触发握手

javascript - 除非在一对双引号之间,否则将字符串拆分为带有空格的单词

javascript - TinyMCE 提示文本

javascript - 使用ajax调用填充数据表

javascript - 将下拉列表转换为输入按钮的javascript事件监听器

javascript - 计算 div 容器中样式标签的数量

javascript - 使用 JavaScript 更改特定 div 中特定字符的 CSS

javascript - 如何在 X 秒后应用 window.close()?