简体   繁体   English

响应式网站上的CSS下拉菜单导致移动电话出现问题

[英]CSS Drop Down Menus on Responsive Site are Causing Problems on Mobile Phones

I have been tasked with taking a site that uses CSS drop down menus and making it responsive. 我的任务是使用一个使用CSS下拉菜单并使其响应的网站。 Since mobile phones don't have hover events I am using Modernizr to add classes to the menus to show/hide them. 由于手机没有悬停事件,我使用Modernizr在菜单中添加类来显示/隐藏它们。 This works fine. 这很好用。 The problem is that touching the parent element still results in the phone navigating to the parent element so unless you are super fast, you can't click on the sub items that appear in the menu. 问题是触摸父元素仍会导致手机导航到父元素,因此除非您超快,否则您无法单击菜单中显示的子项。 Return false and preventDefault both work on my iphone simulator but fail on real devices (android and iphone). 返回false和preventDefault都在我的iphone模拟器上工作但在真实设备(android和iphone)上失败。

//make sure main nav links don't take you anywhere on mobile

$('#a-popular-main-nav').on('touchend', function(event) {
    event.preventDefault();
    return false;
});

$('#a-profile-main-nav').on('touchend', function(event) {
    event.preventDefault();
    return false;
});


if (Modernizr.touch) {
    $('.menu').each(function () {
        var $lis = $(this).find('li');
        $lis.on('touchend', function(event) {

            var $this = $(this);
            if ($this.hasClass('activated')) {
                $this.removeClass('activated');
            }
            else {
                $lis.removeClass('activated');
                $this.addClass('activated');
            }
            event.stopPropagation();

       });

        //close menus if touched outside the menu
       $('body').on('touchend', function() {
               $lis.removeClass('activated');
       });


    });

};

I've tried every combination of stopPropagation, preventDefault, and return false. 我已经尝试了stopPropagation,preventDefault和return false的每个组合。 Has anyone run into this before? 有没有人遇到过这个?

So the answer lies in the first bit of code. 所以答案就在于第一部分代码。 The trouble is the touchend event. 问题在于touchend事件。 The default that I am trying to prevent actually belongs to the click event so here is how I altered the code. 我试图阻止的默认实际上属于click事件,所以这里是我改变代码的方式。 Pretty easy fix. 很容易解决。

$('#a-popular-main-nav').on('click', function(event) {
    event.preventDefault();
});

$('#a-profile-main-nav').on('click', function(event) {
    event.preventDefault();
});

I've done a fair amount of responsive design and have found the best solution in this is Chris Coyier's method of having a select menu for mobile. 我已经完成了大量的响应式设计,并找到了最佳解决方案,这是Chris Coyier为移动设备选择菜单的方法。 That way you can take advantage of each individual phone's native handling of the select menu, which will in most cases function much better (and more importantly predictably ) than any jQuery hacks. 这样你就可以利用每个手机对选择菜单的本机处理,这在大多数情况下比任何jQuery黑客都要好得多(更重要的是可预测 )。

http://css-tricks.com/convert-menu-to-dropdown/ http://css-tricks.com/convert-menu-to-dropdown/

Hope that helps 希望有所帮助

I've been looking into a responsive css drop down menu lately. 我最近一直在寻找响应式css下拉菜单。 This drop down seems to work fine on my iPhone: http://matthewjamestaylor.com/blog/centered-dropdown-menus# 这个下拉似乎在我的iPhone上正常工作: http//matthewjamestaylor.com/blog/centered-dropdown-menus#

But needs some styling work and a bug taken care of (on of the lis seems to move when you "hover"/click on a certain li Good luck. 但需要一些造型工作和一个错误照顾(当你“悬停”/点击某个李时,lis似乎会移动。祝你好运。

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

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