Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created October 11, 2011 12:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sindresorhus/1278008 to your computer and use it in GitHub Desktop.
Save sindresorhus/1278008 to your computer and use it in GitHub Desktop.
jQuery plugin - Adds a .hover class on click on touch devices. Which will make it easier to use :hover and still support touch devices.
$.fn.touchHover = function() {
return 'ontouchstart' in document.documentElement ? this.click(function() {
$(this).toggleClass('hover');
}) : this;
};
/*
Example: $('.button').touchHover();
You also need to add .hover to your :hover CSS rules:
.button:hover -> .button:hover, .button.hover
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment