Last active
June 22, 2016 12:01
-
-
Save Tusko/164b1d4b0ad6a923311b81def7694f05 to your computer and use it in GitHub Desktop.
Fix for :focus on touch devices, when parent has position:fixed
This file contains 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
var supportsTouch = window.hasOwnProperty('ontouchstart') || window.navigator.msPointerEnabled ? true : false, | |
$.fn.mobileFix = function (options) { | |
var $parent = $(this), | |
$fixedElements = $(options.fixedElements); | |
$(document) | |
.on('focus', options.inputElements, function(e) { | |
$parent.addClass(options.addClass); | |
}) | |
.on('blur', options.inputElements, function(e) { | |
$parent.removeClass(options.addClass); | |
// Fix for some scenarios where you need to start scrolling | |
setTimeout(function() { | |
$(document).scrollTop($(document).scrollTop()) | |
}, 1); | |
}); | |
return this; // Allowing chaining | |
}; | |
// Only on touch devices | |
if (supportsTouch) { | |
$("body").mobileFix({ // Pass parent to apply to | |
inputElements: "input,textarea,select", // Pass activation child elements | |
addClass: "fixfixed" // Pass class name | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment