【问题标题】:Responsive mobile navbar (navigation) jquery/javascript响应式移动导航栏(导航)jquery/javascript
【发布时间】:2015-10-24 07:39:20
【问题描述】:

我正在做一个响应式网页设计,但我的 jquery 遇到了这个问题。每当我尝试以移动设备大小打开菜单时,它只会打开一秒钟,然后在我点击菜单按钮之前就快速关闭。当我将 .css('display', '') 更改为 .css('display', '500') 时,菜单可以工作,但问题是当我关闭移动菜单时,我的完整桌面网站导航菜单不会节目。如何解决此问题,以便我的导航栏在移动设备以及桌面和平板电脑视图中工作?

我单独保存了我的手机/平板电脑/dektop .css 文件,而不是制作@media,如果可能的话,我更愿意保持这种方式。 (如果您对代码有任何疑问,请随时提出,并提前感谢您抽出时间帮助我。)

jQuery(document).ready(function() {
    
    jQuery(".menubutton").click(function() {
        
        jQuery(".nav-menu").slideToggle(400, function() {
            jQuery(this).toggleClass("nav-expanded").css('display', '')
        //the number next to 'display' '' can be turned into 1 but then the menu of desktop doesn't show
        });
        
    });
    
});
@charset "utf-8";

/* cssphone */

.menubutton {
    display: block;
    position: relative;
    background-color: orange;
    text-align: center;
    padding: 15px;
    font-size: 20pt;
    font-size: 6vw;
    cursor: pointer;
}


/* Navigation java*/
.nav-expanded {
    display: block;
}

/* Navigation java end*/

.nav-menu {
    display: none;
}

nav ul {
    display: block;
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;
    position: relative;
    background-color: #680000;
}

nav ul li {
    display: inline-block;
    box-sizing: border-box;
    width: 100%;
    border-bottom: 1px black dashed;
}

nav ul li:hover {
    background-color: #ff0000;
}

nav ul li:active {
    border-bottom: 3px #fff dashed;
    background-color: #ff0000;
}

.currentnav {
    border-bottom: 3px #fff dashed;
    background-color: #ff0000;
}

nav ul li a {
    display: block;
    padding: 15px;
    color: white;
    text-decoration: none;
    box-sizing: border-box;
    font-size: 20pt;
    font-size: 6vw;
    font-weight: bold;
}

nav ul li:last-child {
    border-bottom: none;
}

nav ul li:hover > ul {
    display: block;
}

nav ul ul {
    display: block;
    position: relative;
    background-color: #680000;
    width: 100%;
    text-align: left;
    list-style: none;
}

nav ul ul li {
    display: block;
    text-align: center;
}

nav ul ul li:hover {
    color: white;
    background-color: #ff0000;
}

/* css phone ends */


/* css desktop */

@charset "utf-8";

.menubutton {
    display: none;
    width: 100%;
    box-sizing: border-box;
}

nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;
    position: relative;
    background-color: #680000;
}

nav ul li {
    display: inline-block;
}

nav ul li:hover {
    background-color: #ff0000;
}

nav ul li:active {
    border-bottom: 3px #fff solid;
    background-color: #ff0000;
}

.currentnav {
    border-bottom: 3px #fff solid;
    background-color: #ff0000;
}

nav ul li a {
    display: block;
    padding: 15px;
    color: white;
    text-decoration: none;
    font-size: 20pt;
    font-weight: bold;
}

nav ul li:hover > ul {
    display: block;
}

nav ul ul {
    display: none;
    position: absolute;
    background-color: #680000;
    min-width: 150px;
    text-align: left;
    border-top: 0;
    margin: 0 0 0 -2px;
}

nav ul ul li {
    display: block;
    border: 1px solid black;
    text-align: center;
}

nav ul ul li:hover {
    color: white;
    background-color: #ff0000;
}

/* css desktop ends */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYLE html>
<html class="html" lang="en">


<head>
  <meta name="viewport" content="width=device-width, initial-scale=1"
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="cssdesktop.css">
  <link rel="stylesheet" type="text/css" media="only screen and (min-width: 1px) and (max-width: 599px)" href="cssphone.css">
  <link rel="stylesheet" type="text/css" media="only screen and (min-width: 600px) and (max-width: 960px)" href="csstablet.css">
  <link rel="icon" type="image/png" href="images/logo%2016x16.png" alt="16x16logo">
  <link rel="shortcut icon" type="image/png" href="images/logo%2016x16.png">

  <script type="text/javascript" src="jsfiles/jquery-2.1.4.min.js"></script>
  <script type="text/javascript" src="jsfiles/script.js"></script>
</head>

<body class="homepage" link="orange" vlink="blue" alink="yellow">
 

  <div class="menubutton">MENU</div>
  <nav class="nav-menu"> 
    <ul>
	  <li class="currentnav"><a href="index.html">Home</a></li><li>
	  <a href="Gamin'_News.html">News</a>
        <ul>
          <li><a href="Latest_Gamin'_News.html">LGN</a></li>
          <li><a href="Games_Coming_Out_requirements.html">Games Coming out/Requirements</a></li>
          <li><a href="Technology_News.html">Technology News</a></li>
        </ul>
      </li><li>
	  <a href="Gameplay.html">Gameplay</a>
        <ul>
          <li><a href="Counter_Strike_Global_Offensive.html">CS</a></li>
          <li><a href="The_Elder_Scrolls_Online_Tamriel_Unlimited.html">TESO</a></li>
          <li><a href="Other_Games.html">Other Games</a></li>
        </ul>
      </li><li>
	  <a href="F.A.Q..html">F.A.Q.</a>
        <ul>
          <li><a href="Cookies.html">Cookies</a></li>  
        </ul>
      </li><li>
	  <a href="About.html">About</a>
        <ul>
          <li><a href="Who_am_I.html">Who am I?</a></li>
          <li><a href="Facebook.html">Facebook</a></li>
          <li><a href="Twitch.html">Twitch</a></li>
          <li><a href="Youtube.html">Youtube</a></li>
        </ul>  
      </li><li>
	  <a href="Contact.html">Contact</a></li>
	</ul>
  </nav>
  </body>
  </html>

【问题讨论】:

    标签: javascript jquery css mobile responsive-design


    【解决方案1】:

    我在 jsFiddle 中重新创建了您的代码:https://jsfiddle.net/k4zL1rap/ 我不得不更改一些 CSS 属性来显示块,这样我才能看到导航,但问题在于下面的代码:

    .css('display', '') 您已经在使用类切换显示块,因此额外的显示将删除显示值,因为您在 js 中有一个空字符串。此外,该行的末尾应该有一个分号,以供将来参考。

    【讨论】:

    • 这段代码很好,除了桌面视图外,一切正常。当然,我可以通过单击菜单按钮进入菜单,但我真正想要的是菜单按钮仅以移动形式显示,而不以桌面形式显示。所以本质上我希望(视图=桌面)导航在没有菜单按钮的情况下始终可见(菜单按钮现在可见),并且在(视图=移动)中我希望菜单按钮可见但导航仅在单击时可见。
    • 您只需要像这样@media all and (max-width: 500px) .menubutton { display: block;} 围绕显示属性进行媒体查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2018-01-27
    • 2021-04-18
    相关资源
    最近更新 更多