Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottbarrow/32cdd64028e785166ae5d834faaea168 to your computer and use it in GitHub Desktop.
Save scottbarrow/32cdd64028e785166ae5d834faaea168 to your computer and use it in GitHub Desktop.
# 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.
#
if 'ontouchstart' of document.documentElement
sheetI = document.styleSheets.length - 1
while sheetI >= 0
sheet = document.styleSheets[sheetI]
# Verify if cssRules exists in sheet and they are same origin
# (Chrome throws an exception for cross-origin requests on stylesheets)
try
cssRules = sheet.cssRules
catch e
if e.name != 'SecurityError'
throw e
if cssRules
# Loop through each rule in sheet
ruleI = cssRules.length - 1
while ruleI >= 0
rule = 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')
ruleI--
sheetI--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment