【问题标题】:iOS click is activating parent anchor of dropdown menuiOS点击正在激活下拉菜单的父锚
【发布时间】:2013-04-27 00:21:17
【问题描述】:

我已经用尽了我能想到或找到的解决方案。我正在处理一个固定在页面顶部的问题。左侧有一个锚点,可将您带到页面顶部,如果将鼠标悬停在它上面将显示其他外部链接。右侧是带有锚点的页面部分列表,您可以滚动到它们。

这一切在桌面上都可以正常工作,因为悬停和单击是单独的事件,但在 ipad 上它们是相同的。在 iPad 上,您应该能够触摸“产品列表”列表项并显示下拉菜单。如果再次触摸,它将带您回到顶部。现在,当您触摸它时,它将带您回到顶部并显示悬停。

这是一个重新创建代码和问题的 jsfiddle。 http://jsfiddle.net/hyaTV/5/

HTML

<ul class="one">
<li><a class="intrapage" href="#stage">Product Title</a>
    <ul>
        <li><a href="other product">other product</a></li> <!-- link that goes to different page -->
        <li><a href="other product">other product</a></li> <!-- link that goes to different page -->
    </ul>
</li>
</ul>
<ul class="two">
    <li><a class="intrapage" href="#section_one">birds</a></li> <!-- goes to Birds section -->
    <li><a class="intrapage" href="#section_two">bees</a></li> <!-- goes to bees section -->
</ul>

CSS

ul.one{float:left;list-style:none;}
ul.one ul{display:none;}
ul.one > li:hover ul{display:block;}

/* styling for right side nav */
ul.two{float:right;list-style:none;}
ul.two li{display:inline-block;}

JS

// scrolls window to element id with easing
$(".intrapage").click(function(event) {
    event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 850);
return false;
});

【问题讨论】:

  • theverge.com 似乎已经解决了这个问题,但我不满意他们的导航代码有多复杂,只是看起来不是一个非常优雅的解决方案。

标签: javascript jquery ios css


【解决方案1】:

您可以使用iOS :hover issue 来发挥您的优势。我相信通过将您的 CSS 更改为

ul.one > li ul { display: none; }
ul.one > li:hover ul { display: block; }

也就是说,上述问题在其他移动操作系统上不存在。在 iOS 上无法将鼠标移出。

最好是detect if user is on mobile device 并用javascript 将.mobile 添加到&lt;body&gt; 然后在click 事件上切换子菜单。

CSS

ul.one &gt; li:hover ul, ul.one &gt; li.hover ul { display: block; }

JS

$('body.mobile ul.one > li').click(function(e) {
  $(this).toggleClass('hover');
});

【讨论】:

  • 在你的 css 中,li:hover 不会覆盖 ipad 上的 li.hover,因为它会在触摸事件结束后应用 :hover?
  • 你说得对,它应该是body.mobile ul.one &gt; li.hover ul {愚蠢的剪切和粘贴。
【解决方案2】:

我想出了一个似乎可行的解决方案。它将要求您的页面包含 Modernizr JS 以检查是否支持触摸。

JS

if (document.addEventListener) {
document.addEventListener("touchstart", function(){}, true);
}

if($('html').hasClass('touch')){

    $('.one > li').click(function(e){
        if($(this).hasClass('hover')){
    //      $(this).removeClass('hover');
        } else {
            $(this).toggleClass('hover');
        }
    });

    $('html').bind('touchstart', function(){
        if($('.one > li').is(':hover')){
                    // do nothing
        } else {
            $('.one > li.hover').removeClass('hover');
        }
    });

    $('.one a.intrapage').click(function(event){
        if($('.one > li').hasClass('hover')){
            event.preventDefault();
            $('html,body').animate({scrollTop:$(this.hash).offset().top}, 850);
            return false;               
        } else {
            event.preventDefault();
        }
    });

    $(".two .intrapage").click(function(event) {
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top}, 850);
        return false;
    }); 

} else {
    $(".intrapage").click(function(event) {
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top - 50}, 850);
        return false;
    }); 
}

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多