简体   繁体   English

CSS选单在iPad / iPhone上无法运作

[英]CSS Menu not working on iPad / iPhone

I have just built my first responsive website but there is one issue I am facing, the CSS dropdown menus work on android and Chrome's emulation but not in safari on iPhones or iPads. 我刚刚建立了我的第一个响应式网站,但我面临的一个问题是,CSS下拉菜单可在android和Chrome的仿真程序上运行,而在iPhone或iPad的safari中则无法运行。 Here is a link to a working copy and here is the code: 这里是工作副本的链接,下面是代码:

HTML 的HTML

<nav role="navigation" id="top-nav" class="floatRight">
    <div class="nav top-nav cf">
        <ul>
            <li class="page_item page-item-25 current_page_item"><a href="http://phily245.me.uk/gibson/">Home</a></li>
            <li class="page_item page-item-35"><a href="http://phily245.me.uk/gibson/contact/">Contact Us Now</a></li>
            <li class="page_item page-item-37"><a href="http://phily245.me.uk/gibson/services/">Services</a></li>
            <li class="page_item page-item-41"><a href="http://phily245.me.uk/gibson/gallery/">Gallery</a></li>
        </ul>
    </div>
</nav>

CSS 的CSS

#top-nav {
    margin-top: 60px;
    z-index:200;
}

#top-nav li {
    float: left;
    background-image: none;
    padding-left: 0;
}

#top-nav li a {
    font-size: 14px;
    padding: 8px 12px;
    color: #797f94;
    display: block;
}

#top-nav li:hover {
    background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    color-stop(0, #2AA6FF),
    color-stop(1, #1E8AD7)
    );
    background-image: -o-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -moz-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -webkit-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -ms-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: linear-gradient(to bottom, #2AA6FF 0%, #1E8AD7 100%);
    border-radius: 5px;
}

#top-nav li:hover a {
    color: #ffffff;
    text-decoration: none;
}

/* ==========================================================================
Tablet changes
========================================================================== */

@media screen and (min-width: 640px) and (max-width: 1024px)  {  

#top-nav {
    width: 70px;
    border-radius: 3px;
    position: absolute;
    right: 20px;
    z-index: 300;
}

#top-nav::before {
    content: "Menu";
}   

#top-nav ul {
    width: 100%;
}

#top-nav::before:hover div, #top-nav::before:focus div, #top-nav:hover div, #top-nav:focus div {
    z-index: 400;
    display: block;
}

#top-nav li {
    background-image: none;
    position: relative;
}

#top-nav li:hover {
    background-image: none;
}

}


/* ==========================================================================
Tablet & mobile changes
========================================================================== */

@media screen and (max-width: 1024px) {

#top-nav {
    padding: 8px 12px;
    background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    color-stop(0, #2AA6FF),
    color-stop(1, #1E8AD7)
    );
    background-image: -o-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -moz-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -webkit-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: -ms-linear-gradient(bottom, #2AA6FF 0%, #1E8AD7 100%);
    background-image: linear-gradient(to bottom, #2AA6FF 0%, #1E8AD7 100%);
}

#top-nav li a {
    color: #ffffff;
}

#top-nav::before {
    color: #ffffff;
    background-image: url(../images/menu.gif);
    background-position: 0% 3px;
    background-repeat: no-repeat;
    padding-left: 29px;
}

#top-nav li:hover a {
    color: #ffffff;
    text-decoration: underline;
}

#top-nav div {
    display: none;
}

#top-nav:hover div {
    /*position: absolute;*/
}

#top-nav:hover {
    height: 210px;
}

}

/* ==========================================================================
Mobile changes
========================================================================== */

@media screen and (max-width: 639px) {

#top-nav {
    margin-top: 0;
    overflow: hidden;
    float: none;
}

#top-nav::before {
    content: "J Gibson Electrical";
    font-family: segoe-script, "Comic Sans MS", cursive, sans-serif;
    font-weight: bold;
}

#top-nav ul {
    width: 100%;
}

#top-nav li {
    clear: left;
}

#top-nav:hover div {
display: block;
}

#top-nav li:hover, #top-nav li:focus  {
background-image: none;
}

}

Solution

As the accepted answer says, JavaScript was the solution. 正如公认的答案所说,JavaScript是解决方案。 Here was my solution: 这是我的解决方案:

//This goes in an onload or onresize event
if(viewportWidth <= 1024)
    $("#top-nav").click( function() {
        toggleMenu();
    });
}

function toggleMenu()
{

    menuItems = $("#top-nav div");
    if(menuItems.css("display") == "none")
    {
        menuItems.css({
            "display": "block"
        });
    }
    else
    {
        menuItems.css({
            "display": "none"
        });
        $("#top-nav").css({
            "height": "auto"
        });
    }           
}

hover states don't work on touch enabled devices. hover状态在启用触摸的设备上不起作用。 You'll have to use touch events in js or just a regular click event. 您将不得不在js使用touch events ,或者仅是常规click事件。

The issue is with the fact that you can't "hover" on an iPad, you have to use js onclick events and styling to make them drop down on click. 问题在于您无法在iPad上“悬停”,您必须使用js onclick事件和样式来使它们在点击时下降。 Here are some links that might help https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent http://www.w3schools.com/jsref/prop_style_width.asp 以下是一些可能有用的链接: https : //developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent http://www.w3schools.com/jsref/prop_style_width.asp

I was having the same issue and I decided to use a slide menu http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/ 我遇到了同样的问题,因此我决定使用幻灯片菜单http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/

Hello Phill you need to use other pseudo selector Check the following stackoverflow post :touch CSS pseudo-class or something similar? 您好菲尔,您需要使用其他伪选择器检查以下stackoverflow post :touch CSS伪类或类似的东西吗?

Seb 塞布

Found a different issue related to this topic that I thought I would post for future reference. 发现了与该主题相关的另一个问题,我认为我会发布该问题以供将来参考。

ISSUE > Dropdown's work in all browsers except Safari Mobile. 问题> Dropdown在Safari Mobile之外的所有浏览器中均有效。

Tracked the issue to the A tag not having a href element. 将问题跟踪到没有href元素的A标签。 This appears to only be an issue on Safari mobile. 这似乎只是Safari移动版上的一个问题。 Without the href tag the element is not treated as a clickable element. 如果没有href标签,则该元素不会被视为可点击元素。

Cheers! 干杯!

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

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