iPad/iPhone hover problem causes the user to double click a link

后端 未结 26 1709
太阳男子
太阳男子 2020-11-27 09:18

I have some websites I built times ago, that use jquery mouse events...I just got an ipad and i noticed that all the mouse over events are translated in clicks...so for inst

26条回答
  •  孤城傲影
    2020-11-27 09:50

    What worked for me is what others here have already said:

    Don't show/hide elements on hover or mousemove (which is the event in my case).

    Here's what Apple says (https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html):

    A clickable element is a link, form element, image map area, or any other element with mousemove, mousedown, mouseup, or onclick handlers

    If the user taps a clickable element, events arrive in this order: mouseover, mousemove, mousedown, mouseup, and click. Also, if the contents of the page changes on the mousemove event, no subsequent events in the sequence are sent. This behavior allows the user to tap in the new content.

    So, you could use @woop's solution: detect the userAgent, check if it's and iOS device and then bind the event. I ended up using this technique because it suits my needs and it makes more sense do not bind hover events when you don't want it.

    But... if you don't wanna mess with userAgents and still hide/show elements on hover/mousemove, i found out you can do so by using native javascript, like this:

    $("body").on("mouseover", function() {
           document.getElementsByTagName("my-class")[0].style.display = 'block'; //show element
           document.querySelector(".my-selector div").style.display = 'none'; // hide element
    });
    

    This will work on the Desktop version and will do nothing on the mobile version.

    And for a little more compatibility...

    $("body").on("mouseover", function() {
       if (document.getElementsByTagName && document.querySelector) { // check compatibility
           document.getElementsByTagName("my-class")[0].style.display = 'block'; //show element
           document.querySelector(".my-selector div").style.display = 'none'; // hide element
        } else {
            $(".my-class").show();
            $(".my-selector div").hide();
        }
    });
    

提交回复
热议问题