How to stop mobile safari from setting fixed positions to absolute on input focus?
Safari has supported position: fixed since at least version 9.2, but if you're seeing difficult issues, you can fully create the fixed position effect by making the document element and body full screen and then using absolute positioning. Scrolling then occurs in some main container element rather than the body. Your "fixed" elements can exist anywhere in the markup using this method.
jsfiddle here
html,
body,
.mainContainer {
height: 100%;
width: 100%;
overflow: hidden;
margin: 0;
}
.mainContainer {
overflow: auto;
}
.fixed {
position: absolute;
bottom: 20px;
left: 20px;
}
How to make fixed-content go above iOS keyboard?
We can use VisualViewport to calculate keyboard height. So we can set fixed-content pos correct.
Small demo: https://whatwg6.github.io/pos-above-keyboard/index.html
Code snippet:
const button = document.getElementById("button");
const input = document.getElementById("input");
const height = window.visualViewport.height;
const viewport = window.visualViewport;
window.addEventListener("scroll", () => input.blur());
window.visualViewport.addEventListener("resize", resizeHandler);
function resizeHandler() {
if (!/iPhone|iPad|iPod/.test(window.navigator.userAgent)) {
height = viewport.height;
}
button.style.bottom = `${height - viewport.height + 10}px`;
}
function blurHandler() {
button.style.bottom = "10px";
}
html,
body {
margin: 0;
padding: 0;
}
#button {
position: fixed;
width: 100%;
bottom: 10px;
background-color: rebeccapurple;
line-height: 40px;
text-align: center;
}
<input type="text" inputmode="decimal" value="0.99" id="input" onblur="blurHandler()" />
<div id="button">Button</div>
Safari in ios8 is scrolling screen when fixed elements get focus
This is now fixed in iOS 10.3!
Hacks should no longer be needed.
Prevent iOS from handling focus event on an input
This has been quite a hack, but it seems to work:
body, html {
margin: 0;
overflow: auto;
height: 100%;
-webkit-overflow-scrolling: touch;
}
Just add this to your CSS. I needed to remove the margin, not sure if that will affect your design. If so, you might try to add padding instead, but I haven't tried.
The idea is to prevent the html element to scroll, tricking Safari as it seems that it is only attempting to do it in the outer most element.
If the html doesn't scroll, I need to add the scroll back, so I apply it to the body. That makes the scroll to lose the momentum, and to add it back you need that -webkit-overflow-scrolling: touch
thing.
So, to be more clear of what is going on, the code should be like this - but I haven't tried it:
html {
margin: 0;
overflow: hidden;
height: 100%
}
body {
margin: 0;
overflow: auto;
height: 100%;
-webkit-overflow-scrolling: touch;
}
iOS 10 Safari: Prevent scrolling behind a fixed overlay and maintain scroll position
Add -webkit-overflow-scrolling: touch;
to the #overlay
element.
Then add this JavaScript code at the end of the body tag:
(function () {
var _overlay = document.getElementById('overlay');
var _clientY = null; // remember Y position on touch start
_overlay.addEventListener('touchstart', function (event) {
if (event.targetTouches.length === 1) {
// detect single touch
_clientY = event.targetTouches[0].clientY;
}
}, false);
_overlay.addEventListener('touchmove', function (event) {
if (event.targetTouches.length === 1) {
// detect single touch
disableRubberBand(event);
}
}, false);
function disableRubberBand(event) {
var clientY = event.targetTouches[0].clientY - _clientY;
if (_overlay.scrollTop === 0 && clientY > 0) {
// element is at the top of its scroll
event.preventDefault();
}
if (isOverlayTotallyScrolled() && clientY < 0) {
//element is at the top of its scroll
event.preventDefault();
}
}
function isOverlayTotallyScrolled() {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
return _overlay.scrollHeight - _overlay.scrollTop <= _overlay.clientHeight;
}
}())
Related Topics
How to Extend a Class from a CSS File in SASS
Are Margin and Padding Most Disbalanced Thing Among All Browser
Bootswatch Theme in Shiny Flexdashboard R
Vertically Center HTML Element Within a Div of Dynamic Height
Css3 Filter: Drop-Shadow Spread Property Alternatives
Do You Plan to Support Ie 6 on Your Future Projects
How Do Browsers Deal with "Tofu" Characters
Equal Height Bootstrap Cards Within Slick Carousel
How to Get Perfect Border Radius on Images in All Browsers
What Does a Space Preceding :Nth-Child(N) Do
Set the Width of Children to Fill the Parent
Stop Animation from Replaying When Parent Switches from Display:None to Block
Horizontal and Vertical Center Text in HTML