Currently having an issue where my navigation and dropdowns aren't working on iOS devices, specifically iPhone.
When the user clicks a nav item, that has children, I toggle a class on the parent container, which displays the dropdown.
Below is the HTML:
<div class="menu-main-menu-container">
<ul id="menu-main-menu" class="menu">
<li id="menu-item-365" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-365 nav-open"><a href="#">About</a>
<ul class="sub-menu">
<li id="menu-item-44" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-44"><a href="http://localhost:8888/beulah/about/im-new/">I’m New</a></li>
<li id="menu-item-166" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-166"><a href="http://localhost:8888/beulah/about/membership/">Membership</a></li>
<li id="menu-item-43" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-43"><a href="http://localhost:8888/beulah/about/core-values/">Core Values</a></li>
<li id="menu-item-45" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-45"><a href="http://localhost:8888/beulah/about/our-history/">Our History</a></li>
<li id="menu-item-46" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-46"><a href="http://localhost:8888/beulah/about/staff-directory/">Staff Directory</a></li>
</ul>
</li>
</ul>
</div>
CSS:
.sub-menu {
opacity: 0;
max-height: 0;
visibility: hidden;
transition: all 0.3s ease-in-out;
-webkit-transition: all 0.3s ease-in-out;
}
.nav-open .sub-menu {
visibility: visible;
position: relative;
opacity: 1;
padding-top: 25px;
padding-bottom: 25px;
height: inherit;
max-height: 300px;
}
JS:
$(".menu-item > a").click(function(e){
e.preventDefault();
$(this).parent().toggleClass("nav-open");
});
As of right now, the class is applied, when clicked, but on iPhone the dropdown does not open up.
The problem is, iPad/iPhone click event is not triggered the way it is on your desktop.
You need to pick any of these events:
touchstart
touchmove
touchend
touchcancel
so, here :
$(".menu-item > a").bind( "touchstart" , function(e){
e.preventDefault();
$(this).parent().toggleClass("nav-open");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.