Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active June 22, 2016 12:01
Show Gist options
  • Save Tusko/164b1d4b0ad6a923311b81def7694f05 to your computer and use it in GitHub Desktop.
Save Tusko/164b1d4b0ad6a923311b81def7694f05 to your computer and use it in GitHub Desktop.
Fix for :focus on touch devices, when parent has position:fixed
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