Created
December 21, 2014 01:51
-
-
Save vartan/ab195e5a502a47e0c3e5 to your computer and use it in GitHub Desktop.
Converts :hover CSS to :active CSS on mobile devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts :hover CSS to :active CSS on mobile devices. | |
* Otherwise, when tapping a button on a mobile device, the button stays in | |
* the :hover state until the button is pressed. | |
* | |
* Inspired by: https://gist.github.com/rcmachado/7303143 | |
* @author Michael Vartan <[email protected]> | |
* @version 1.0 | |
* @date 2014-12-20 | |
*/ | |
function hoverTouchUnstick() { | |
// Check if the device supports touch events | |
if('ontouchstart' in document.documentElement) { | |
// Loop through each stylesheet | |
for(var sheetI = document.styleSheets.length - 1; sheetI >= 0; sheetI--) { | |
var sheet = document.styleSheets[sheetI]; | |
// Verify if cssRules exists in sheet | |
if(sheet.cssRules) { | |
// Loop through each rule in sheet | |
for(var ruleI = sheet.cssRules.length - 1; ruleI >= 0; ruleI--) { | |
var rule = sheet.cssRules[ruleI]; | |
// Verify rule has selector text | |
if(rule.selectorText) { | |
// Replace hover psuedo-class with active psuedo-class | |
rule.selectorText = rule.selectorText.replace(":hover", ":active"); | |
} | |
} | |
} | |
} | |
} | |
} |
I found I had more success using a greedy regex on line 25
rule.selectorText = rule.selectorText.replace(/\:hover/ig, ":active");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This do not work on Android for me