带有下拉菜单的自适应CSS Topnav在samll屏幕上不起作用

时间:2019-05-23 19:48:31

标签: javascript html css dropdown responsive

我正在尝试学习如何制作响应式CCS页面,并且正在关注有关W3schools(https://www.w3schools.com/howto/howto_js_responsive_navbar_dropdown.asp)的教程

我已经编码了我的版本,并且在桌面大小上都可以正常工作,但是当我尝试使用Iphone5 / Iphone6 / Ipad大小时,在单击带有javascript的按钮后,菜单不显示。

我被困住了,并尽了最大努力找出为什么它不能像本教程中的那样工作-有人可以帮助/指导我需要进行哪些更改吗?

我的html看起来像这样:

    <!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <title>My page</title>
    <link href="https://fonts.googleapis.com/css?family=Fjalla+One|Noto+Sans:400,400i,700,700i" rel="stylesheet">
    <link href="../style/style.css" rel="stylesheet">
</head>
<body>
    <!-- topp logo start -->
    <div class="logo">
        <img src="../bilder/logo.gif" alt="Logo">
        <p>Some header text</p>
    </div><!-- topp logo slutt --><!-- Menu start -->
    <div>
        <nav>
            <div class="topnav" id="myTopnav">
                <a class="active" href="/Test.htm">Startpage</a>
                <div class="dropdown">
                    <button class="dropbtn">Group1 <i class="fa fa-caret-down"></i></button>
                    <div class="dropdown-content">
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                    </div>
                </div>
                <div class="dropdown">
                    <button class="dropbtn">Group2 <i class="fa fa-caret-down"></i></button>
                    <div class="dropdown-content">
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                    </div>
                </div>
                <div class="dropdown">
                    <button class="dropbtn">Group3 <i class="fa fa-caret-down"></i></button>
                    <div class="dropdown-content">
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                        <a href="#" target="_parent">Option1</a>
                    </div>
                </div><a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">&#9776;</a>
            </div>
        </nav>
    </div>
    <script>
    function myFunction() {    var x = document.getElementById("myTopnav");   
    if (x.className === "topnav") {      x.className += " responsive";    } 
    else {      x.className = "topnav";    } }  
    </script>
    <!-- menu end -->
    <div class="sesbanner">
        <picture>
        <source media='(min-width: 401px)' srcset='sesbanner/big.jpg'>
        <source media='(max-width: 400px)' srcset='sesbanner/tall.jpg'>
        <img src='sesbanner/small.jpg'></picture>
        <div class="sestext">
            SUBJECT<br>
            2019
        </div>
        <div class="sesliten">
            Choose option from above menu.
        </div>
    </div>
    <div class="adressbar">
        <h3>Company</h3><br>
        <br>
        Adr1<br>
        Adr2<br>
        ZIP City<br>
        <br>
        Phone: +11 11 11 11<br>
        Fax:   +22 22 22 22<br>
        <br>
        <a href="mailto:post@site.com">Email: post@site.com</a><br>
    </div><!-- Footer -->
    <div class="footer">
        &#169;All rights reserved 2019.
        <center>
            <table id="footertabell">
                <tr>
                    <td>Made by me</td>
                </tr>
            </table>
        </center>Some text.<br>
        Some more text.
    </div><!-- END -->
</body>
</html>

和css样式文件如下:




h1{
  font-size: 2.25em;
   line-height: 2.25em;
  font-family: 'Fjalla One', sans-serif;
  margin-top: 10px;
  margin-bottom: 10px;
}
h2{
  font-size: 1.875em;
   line-height: 2em;
  font-family: 'Fjalla One', sans-serif;
}

h3{
  font-size: 1.25em;
   line-height: 1.375em;
  font-family: 'Fjalla One', sans-serif;
}
h4{
  font-size: 1em;
   line-height: 1.125em;
  font-family: 'Fjalla One', sans-serif;
}
h5{
  font-size: 0.875em;
   line-height: 1em;
  font-family: 'Fjalla One', sans-serif;
}
h6{
  font-size: 0.75em;
   line-height: 0.875em;
  font-family: 'Fjalla One', sans-serif;
}
.paragraph {
  font-size: 0.875em;
  line-height: 1em;
  font-family: 'Noto Sans', sans-serif;
}
.medium {
  font-size: 0.875em;
  line-height: 1em;
  font-family: 'Noto Sans', sans-serif;
text-align: center;
color: darkgray;
}

.liten {
  font-size: 0.5625em;
   line-height: 0.625em;
  font-family: 'Noto Sans', sans-serif;
}
p.bold {
  font-weight: 600;
}


.logo {

    max-width: 100%;
    max-height: 100%;
    text-align: center;
    background-color: black;
    font-family:'Fjalla One', sans-serif;
    text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.50);
    font-size: 2.00em;
    color: white;
}


.vgrtema{
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
 justify-content: space-evenly;
 align-items: center;
  max-width: 100%;
  max-height: 100%;



  background-color: #e7e7e7;

}

.vgrtema.img {
    max-width: 100%;
    max-height: 100%;

}
.vgrtextheader {


font-family:'Fjalla One', sans-serif;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.50);
font-size: 1.75em;
color: black;
text-align: center;
vertical-align: middle;
line-height: 1.5em;
}



.sesbanner {
position: relative;
text-align: center;
color: white;
background-color: black;
}


.sestext {
 font-family:'Fjalla One', sans-serif;
text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.50);
font-size: 3em;
text-transform: uppercase;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
    }

.sesliten {
font-family: 'Noto Sans', sans-serif;
text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.50);
font-size: 1em;
text-transform: uppercase;
position: absolute;
top: 85%;
left: 50%;
transform: translate(-50%, -50%);
    }

/*Menu start */

nav {margin:0;
  font-family:'Fjalla One', sans-serif;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.50);
  font-size: 1.00em;
 background-color: black;
}
/* Add a black background color to the top navigation */
.topnav {
  overflow: hidden;
  background-color: darkgray;
}
/* Style the links inside the navigation bar */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
/* Add an active class to highlight the current page */
.active {
  background-color: darkgray;
  color: white;
}
/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
  display: none;
}
/* Dropdown container - needed to position the dropdown content */
.dropdown {
  float: left;
  overflow: hidden;
}
/* Style the dropdown button to fit inside the topnav */
.dropdown .dropbtn {
  font-size: 17px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}
/* Style the dropdown content (hidden by default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}
/* Style the links inside the dropdown */
.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}
/* Add a dark background on topnav links and the dropdown button on hover */
.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: white;
}
/* Add a grey background to dropdown links on hover */
.dropdown-content a:hover {
  background-color: #ddd;
  color: black;
}
/* Show the dropdown menu when the user moves the mouse over the dropdown button */
.dropdown:hover .dropdown-content {
  display: block;
}

/* When the screen is less than 600 pixels wide, hide all links, except for the first one 
("Home"). Show the link that contains should open and close the topnav (.icon) */

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}


/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. 
This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

/*Menu end  */


0 个答案:

没有答案