With some HTML like this:
Some Text
Then some CSS like this:
p {
color:black;
}
p:hover {
color:red
Without device (or rather browser) specific JS I'm pretty sure you're out of luck.
Edit: thought you wanted to avoid that until i reread your question. In case of Mobile Safari you can register to get all touch events similar to what you can do with native UIView-s. Can't find the documentation right now, will try to though.
Try this:
<script>
document.addEventListener("touchstart", function(){}, true);
</script>
And in your CSS:
element:hover, element:active {
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-user-select: none;
-webkit-touch-callout: none /*only to disable context menu on long press*/
}
With this code you don't need an extra .hover class!
Easyest solution I found: I had some < span > tags with :hover css rules in them. I switched for < a href="javascript:void(0)" > and voilà. The hover styles in iOS started working.
Add this code and then set class "tapHover" to elements which you would like to work this way. First time you tap an element it will gain pseudoclass ":hover" and class "tapped". Click event will be prevented. Second time you tap the same element - click event will be fired.
// Activate only in devices with touch screen
if('ontouchstart' in window)
{
// this will make touch event add hover pseudoclass
document.addEventListener('touchstart', function(e) {}, true);
// modify click event
document.addEventListener('click', function(e) {
// get .tapHover element under cursor
var el = jQuery(e.target).hasClass('tapHover')
? jQuery(e.target)
: jQuery(e.target).closest('.tapHover');
if(!el.length)
return;
// remove tapped class from old ones
jQuery('.tapHover.tapped').each(function() {
if(this != el.get(0))
jQuery(this).removeClass('tapped');
});
if(!el.hasClass('tapped'))
{
// this is the first tap
el.addClass('tapped');
e.preventDefault();
return false;
}
else
{
// second tap
return true;
}
}, true);
}
.box {
float: left;
display: inline-block;
margin: 50px 0 0 50px;
width: 100px;
height: 100px;
overflow: hidden;
font-size: 20px;
border: solid 1px black;
}
.box.tapHover {
background: yellow;
}
.box.tapped {
border: solid 3px red;
}
.box:hover {
background: red;
}
<div class="box" onclick="this.innerHTML = Math.random().toFixed(5)"></div>
<div class="box tapHover" onclick="this.innerHTML = Math.random().toFixed(5)"></div>
<div class="box tapHover" onclick="this.innerHTML = Math.random().toFixed(5)"></div>
The mouse hover
effect cannot be implemented in touch device . When I'm appeared with same situation in safari
ios
I used :active
in css to make effect.
ie.
p:active {
color:red;
}
In my case its working .May be this is also the case that can be used with out using javascript. Just give a try.
Solved 2019 - Hover on Touch
It now seems best to avoid using hover altogether with ios or touch in general. The below code applies your css as long as touch is maintained, and without other ios flyouts. Do this;
Jquery add: $("p").on("touchstart", function(e) { $(this).focus(); e.preventDefault(); });
CSS: replace p:hover with p:focus, and add p:active
Options;
replace jquery p selector with any class etc
to have the effect remain, keep p:hover as well, and add body{cursor:ponter;} so a tap anywhere ends it
try click & mouseover events as well as touchstart in same code (but not tested)
remove e.preventDefault(); to enable users to utilise ios flyouts eg copy
Notes
only tested for text elements, ios may treat inputs etc differently
only tested on iphone XR ios 12.1.12, and ipad 3 ios 9.3.5, using Safari or Chrome.