Home >Web Front-end >CSS Tutorial >How to Fix Fixed Navigation Elements in Mobile Safari When the Keyboard Appears?
Fixing Fixed Navigation Elements in Mobile Safari
Ensuring fixed navigation elements remain in place when the virtual keyboard opens in Mobile Safari can be challenging. This issue stems from known bugs in Mobile Safari's handling of fixed elements.
One solution proposed by dansajin involves toggling the position of fixed elements when an input field receives focus. When focus is set, the fixed elements are set to position:absolute, and when focus is lost, they revert to position:fixed.
To implement this approach, add the following CSS:
.header { position: fixed; } .footer { position: fixed; } .fixfixed .header, .fixfixed .footer { position: absolute; }
Additionally, include the following JavaScript:
if ('ontouchstart' in window) { /* cache dom references */ var $body = $('body'); /* bind events */ $(document) .on('focus', 'input', function() { $body.addClass('fixfixed'); }) .on('blur', 'input', function() { $body.removeClass('fixfixed'); }); }
The above is the detailed content of How to Fix Fixed Navigation Elements in Mobile Safari When the Keyboard Appears?. For more information, please follow other related articles on the PHP Chinese website!