简体   繁体   English

jQuery 的移动导航问题

[英]Mobile navigation issues with jQuery

I created a simple navigation, but it doesn't work on mobile.我创建了一个简单的导航,但它不适用于移动设备。 When you click on the plus symbol or "Book 1 name" the list of links are suppose to appear, but for some reason it doesn't work, and when you click the plus symbol the ex symbol is suppose to appear.当您单击加号或“书 1 名称”时,应该会出现链接列表,但由于某种原因它不起作用,并且当您单击加号时,应该会出现 ex 符号。 The book title should link should work on desktop, but not on mobile.书名应该链接应该在桌面上工作,但不能在移动设备上工作。 Thanks!谢谢!

 $(document).ready(function () { $(window).resize(function() { if($(window).width() <= 550) { $('.bookName').click(function(e){ e.preventDefault(); $('.bookNavigation ul li ul').toggleClass('toggleNav') $(this).toggleClass('changeIcon'); }); } else { $('.bookName').click(function(e){ return true; }); } }); });
 .bookNavigation ul li ul{ display: flex; }.toggleNav{ display: block; } @media only screen and (max-width: 550px) {.bookNavigation ul li ul{ display: none; }.bookName::before{ content: '+'; padding-right: 10px; }.bookName.changeIcon::before{ content: '-'; padding-right: 10px; } } a{ text-decoration: none; } li{ list-style: none; }.bookNavigation{ margin-bottom: 100px; }.bookName{ text-transform: uppercase; font-size: 30px; } li{ margin-right: 30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav class="bookNavigation"> <ul> <li> <a href="#" class="bookName">Book 1 Name</a> <ul> <li><a href="#">Book 1 Chapter 1</a></li> <li><a href="#">Book 1 Chapter 2</a></li> <li><a href="#">Book 1 Chapter 3</a></li> </ul> </li> </ul> </nav> <p>you are in Book 1 Name cover page</p>

You can do this check $(window).width() <= 550 inside the click event rather than attaching the event based on screen-size.您可以在单击事件中执行此检查$(window).width() <= 550而不是根据屏幕大小附加事件。 Please find the corrected code.请找到更正后的代码。

 $('.bookName').click(function(e) { if ($(window).width() <= 550) { e.preventDefault(); $('.bookNavigation ul li ul').toggleClass('toggleNav') $(this).toggleClass('changeIcon'); } });
 .bookNavigation ul li ul { display: flex; }.bookNavigation ul li ul.toggleNav { display: block; } @media only screen and (max-width: 550px) {.bookNavigation ul li ul { display: none; }.bookName::before { content: '+'; padding-right: 10px; }.bookName.changeIcon::before { content: '-'; padding-right: 10px; } } a { text-decoration: none; } li { list-style: none; }.bookNavigation { margin-bottom: 100px; }.bookName { text-transform: uppercase; font-size: 30px; } li { margin-right: 30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav class="bookNavigation"> <ul> <li> <a href="#" class="bookName">Book 1 Name</a> <ul> <li><a href="#">Book 1 Chapter 1</a></li> <li><a href="#">Book 1 Chapter 2</a></li> <li><a href="#">Book 1 Chapter 3</a></li> </ul> </li> </ul> </nav> <p>you are in Book 1 Name cover page</p>

Instead of checking on resize, check the window with inside buttonClickHandler like below而不是检查调整大小,检查 window 与内部 buttonClickHandler 如下所示

$(button).on('click', function(){
   if($(window).width() <= 550){
      // Code to toggle list
   }
});

Another problem was the specificity of toggleNav css class.另一个问题是 toggleNav css class 的特异性。

.bookNavigation ul li ul {
   display: none;
}

Above code is more specific than上面的代码比

.toggleNav {
   display: block;
}

So the toggling toggleNav won't work because the 1st one will override this one.所以切换 toggleNav 将不起作用,因为第一个将覆盖这个。 For fixing it create a class like the one below and toggle it on click.为了修复它,创建一个 class 如下图所示,并在单击时切换它。 So that it will be hidden initially and will be removed on click.这样它最初会被隐藏并在点击时被删除。

.navHidden {
  display: none
}

Check this pin for working example: https://codesandbox.io/s/bold-currying-39w11检查此引脚以获取工作示例: https://codesandbox.io/s/bold-currying-39w11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM