Touch Screen Detection

Since touchscreens don’t have a “hover” state, Apple has made it so on iOS devices (iPads and iPhones) if an HTML element has :hover styling in the CSS, the first click on that item will produce the :hover state, and the second click will be an actual click. This can cause problems in certain instances.

A way around it is to add a “no-touch” class to <body>, then using Javascript detect if this is a touchscreen, and if it is then remove this class. Example:

<?php
/**
* Body Classes
*
*/
function be_body_classes( $classes ) {
$classes[] = 'no-touch';
return $classes;
}
add_filter( 'body_class', 'be_body_classes' );
view raw functions.php hosted with ❤ by GitHub
jQuery(function($){
// Touch Device Detection
var isTouchDevice = 'ontouchstart' in document.documentElement;
if( isTouchDevice ) {
$('body').removeClass('no-touch');
}
});
view raw global.js hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk