Tap highlight in Safari Mobile and jQuery on() function produce huge highlight

大城市里の小女人 提交于 2020-01-05 07:16:10

问题


I am using jQuery's on() function on iOS' Safari Mobile Browser like this:

$("#content").on(".element", click(function(){ do stuff});

When clicking on .element Safari highlights the whole #content area instead of just the area .element takes.

When using

$(".element").click(function(){do stuff});

instead the right area is highlighted. I guess this is the expected behavior but it's not intuitive for the user at all, so I'm looking for a workaround.

I know I can get rid of the highlighting completely by using -webkit-tap-highlight-color: rgba(0,0,0,0); but I think the highlight is actually pretty useful generally since CSS' hover isn't useful on mobile devices already.

Is there a way with jQuery's on function and Safari Mobile to get the tap highlight of the .element?


回答1:


I solved it the following way:

  1. Put document.addEventListener("touchstart", function(){}, true); in document ready function enabling the css :active pseude class
  2. Disabling the tap highlight completely: -webkit-tap-highlight-color: rgba(0,0,0,0);
  3. Adding .element:active{background: #cccccc;}

This way the clicked (active) element gets a nice gray background when touched.



来源:https://stackoverflow.com/questions/12674857/tap-highlight-in-safari-mobile-and-jquery-on-function-produce-huge-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!