Fix CSS hover on iPhone/iPad/iPod

前端 未结 16 639
星月不相逢
星月不相逢 2020-12-02 14:53

I want to fix the hover effect on iOS ( change to touch event ) but I dont have any idea . Let me explain this . You have a text in your page :

16条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 15:11

    I know it's an old post, already answered, but I found another solution without adding css classes or doing too much javascript than really needed, and I want to share it, hoping can help someone.

    I found that to enable :hover effect on every kind of elements on a Touch enabled browser, you need to tell him that your elements are clickable. To do so you can simply add an empty handler to the click function with jQuery or javascript.

    $('.need-hover').on('click', function(){ });
    

    It's better if you do so only on Mobile enabled browsers with this snippet:

    // check for css :hover supports and save in a variable
    var supportsTouch = (typeof Touch == "object");
    
    if(supportsTouch){ 
        // not supports :hover
        $('.need-hover').on('click', function(){ });
    }
    

提交回复
热议问题