I have a fixed element (menu tab) on a page of a web app such that when the iPhone keyboard opens, the menu is repositioned on scroll to stay in the window. When the keyboard closes I want the element to return to its original position based on page load CSS.
How do I remove the scroll event I add on focus? It seems to be the problem and my code below is not working.
Additionally is there a way to keep the element in position and have the page smooth scroll behind it? My code snaps the element into place after the scroll happens.
Thanks!
MY CSS for the fixed element:
#fixed-floating-menu {
position: fixed;
right: 0;
top: 103px;
z-index: 9999;
background-color: @navy;
border: @gray;
text-align: center;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
border-color: @box-border-color;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 3px 10px 0 rgba(0, 0, 0, 0.19);
opacity: 0.7;
tr td {
padding: 10px;
}
.icon-popover-menu-side {
color: @white;
}
}
MY JS handingling the keyboard open / reposition of element:
// Adjust scrolling behaviour if iPhone
var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
if (iOS) {
$("body").mobileFix({ // Pass parent to apply to
inputElements: "input, textarea, select", // Pass activation child elements
addClass: "fixfixed" // Pass class name
});
}
// iPhone scroll fix with keyboard open
$.fn.mobileFix = function (options) {
var $parent = $(this);
$(document)
.on('focus', options.inputElements, function(e) {
$parent.addClass(options.addClass);
setTimeout(function() {
var handPosition = window.pageYOffset - 7;
$("#fixed-floating-menu").css("position", "absolute").css("top", handPosition).css("right", -10);
}, 1);
$(window).scroll(function(e) {
var handPosition = window.pageYOffset - 7;
$("#fixed-floating-menu").css("position", "absolute").css("top", handPosition).css("right", -10);
});
})
.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);
$(window).off('scroll', function(e) {
$("#fixed-floating-menu").css("position", "fixed").css("top", -7).css("right", -10);
return true;
});
});
return this; // Allowing chaining
};