响应的侧面菜单

时间:2017-10-09 14:30:22

标签: javascript html css responsive sidenav

我正在关注一个名为“响应式HTML& amp;来自Youtube上Traversy Media的Scratch'的CSS侧面菜单(我发布的链接还没有足够的声誉)我和我的侧面导航器有一些问题。请注意before I click on the openSlideMenu() icon on the top left-hand cornerI have clicked on it时的显示方式。我已经按照老师的代码进行了操作,但界面上似乎至少有4个问题。

  1. 在3栏图标附近有一个打开的标记符号,当菜单显示时和不显示时都会保留。

  2. 侧面导航,当点击打开时,远离页面的左侧,在菜单和页面左侧之间创建一个大的空白间隙,不存在任何间隙。

  3. 侧面导航遍历标题文本“响应侧菜单”,从而阻止文本的“e菜单”部分,而标题文本应该响应并在侧导航移动时移动,如在教程中描述。

  4. side-nav中的closeMenuButton()的位置直接位于元素上方,而应位于右侧角落。

  5. 我的HTML / Javascript代码:

    function openSlideMenu() {
      document.getElementById('side-menu').style.width = '250px';
      document.getElementById('side-menu').style.marginLeft = '250px';
    }
    
    function closeSlideMenu() {
      document.getElementById('side-menu').style.width = '0px';
      document.getElementById('side-menu').style.marginLeft = '0px';
    }
    body {
      font-family: "Arial", Serif;
      background-color: #f4f4f4;
      overflow-x: hidden;
    }
    
    .navbar {
      background-color: #3b5998;
      overflow: hidden;
      height: 63px;
    }
    
    .navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    
    .navbar ul {
      margin: 8px 0 0 0;
      list-style: none;
    }
    
    .navbar a:hover {
      background-color: #ddd;
      color: #000;
    }
    
    .side-nav {
      height: 100%;
      width: 0;
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      background-color: #111;
      opacity: 0.9;
      overflow-x: hidden;
      padding-top: 60px;
      transition: 0.5s;
    }
    
    .side-nav a {
      padding: 10px 10px 10px 30px;
      text-decoration: none;
      font-size: 22px;
      color: #ccc;
      display: block;
      transition: 0.3s;
    }
    
    .side-nav a:hover {
      color: #fff;
    }
    
    .side-nav .btn-close {
      postion: absolute;
      top: 0;
      right: 22px;
      font-size: 36px;
      margin-left: 50px;
    }
    
    # main {
      transition: margin-left 0.5s;
      padding: 20px;
      overflow: hidden;
      width: 100%;
    }
    
    @media(max-width: 568px) {
      .navbar-nav {
        display: none
      }
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width-device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie-edge">
      <title>Document</title>
      <link rel="stylesheet" href="style.css" </head>
    
      <body>
        <nav class="navbar">
          <span class="open-slide">
          <a href="#" onClick="openSlideMenu()">
            <<svg width="30" height="30">
              <path d="M0,5 30,5" stroke=#fff stroke-width="5"/>
              <path d="M0,14 30,14" stroke=#fff stroke-width="5"/>
              <path d="M0,23 30,23" stroke=#fff stroke-width="5"/>
            </svg>
          </a>
        </span>
          <ul class="navbar-nav">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Service</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
    
        <div id="side-menu" class="side-nav">
          <a href="#" class="btn-close" onClick="closeSlideMenu()">&times;</a>
          <a href="#">Home</a>
          <a href="#">About</a>
          <a href="#">Services</a>
          <a href="#">Contact</a>
        </div>
    
        <div id="main">
          <h1>Responsive Side Menu</h1>
        </div>
    
    
    
      </body>
    
    </html>

    此外,老师还有他在Youtube视频描述中制作的代码,您可以通过搜索“响应式HTML&amp; amp;从Scratch'开始的CSS Side Menu。

1 个答案:

答案 0 :(得分:0)

代码充满了错误 这是工作代码 找出你的错误

&#13;
&#13;
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width-device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie-edge">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <style media="screen">
    body {
      font-family: "Arial", Serif;
      background-color: #f4f4f4;
      overflow-x: hidden;
    }

    .navbar {
      background-color: #3b5998;
      overflow: hidden;
      height: 63px;
    }

    .navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }

    .navbar ul {
      margin: 8px 0 0 0;
      list-style: none;
    }

    .navbar a:hover {
      background-color: #ddd;
      color: #000;
    }

    .side-nav {
      height: 100%;
      width: 0;
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      background-color: #111;
      opacity: 0.9;
      overflow-x: hidden;
      margin-top: 60px;
      transition: 0.5s;
    }

    .side-nav a {
      padding: 10px 10px 10px 30px;
      text-decoration: none;
      font-size: 22px;
      color: #ccc;
      display: block;
      transition: 0.3s;
    }

    .side-nav a:hover {
      color: #fff;
    }

    .side-nav .btn-close {
      position: absolute;
      top: 0;
      right: 22px;
      font-size: 36px;
      display: inline;
    }

    #main {
      transition: margin-left 0.5s;
      padding: 20px;
      overflow: hidden;
      width: 100%;
    }
    .open-slide {
      display: none;
    }

    @media(max-width: 568px) {
      .navbar-nav {
       display: none
      }
      .open-slide {
        display: inline-block;
      }
    }
    </style>
    </head>

    <body>
    <nav class="navbar">
      <span class="open-slide">
      <a href="#" onClick="openSlideMenu()">
        <<svg width="30" height="30">
          <path d="M0,5 30,5" stroke=#fff stroke-width="5"/>
          <path d="M0,14 30,14" stroke=#fff stroke-width="5"/>
          <path d="M0,23 30,23" stroke=#fff stroke-width="5"/>
        </svg>
      </a>
    </span>
      <ul class="navbar-nav">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Service</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>

    <div id="side-menu" class="side-nav">
      <a href="#" class="btn-close" onClick="closeSlideMenu()">&times;</a>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Services</a>
      <a href="#">Contact</a>
    </div>

    <div id="main">
      <h1>Responsive Side Menu</h1>
    </div>
    <script type="text/javascript">
    function openSlideMenu() {
    document.getElementById('side-menu').style.width = '250px';
    }

    function closeSlideMenu() {
    document.getElementById('side-menu').style.width = '0px';
    document.getElementById('side-menu').style.marginLeft = '0px';
    }
    </script>


    </body>

    </html>
&#13;
&#13;
&#13;