Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS 8 position: fixed; input element workaround
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
<title>iOS 8 position: fixed; input element workaround</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
position: relative;
}
#a {
height: 2000px;
background: linear-gradient(red, blue);
}
#b {
position: fixed;
bottom: 20px;
left: 0;
width: 100%;
height: 100px;
background: rgba(0, 0, 0, 0.5);
}
textarea {
width: 80%;
height: 100%;
margin-left: 10%;
}
</style>
</head>
<body>
<div id="a"></div>
<div id="b">
<textarea></textarea>
</div>
<script>
//Get the fixed element, and the input element it contains.
var fixed_el = document.getElementById('b');
var input_el = document.querySelector('textarea');
//Listen for touchstart, focus will fire too late.
input_el.addEventListener('touchstart', function() {
//If using a non-px value, you will have to get clever, or just use 0 and live with the temporary jump.
var bottom = parseFloat(window.getComputedStyle(fixed_el).bottom);
//Switch to position absolute.
fixed_el.style.position = 'absolute';
fixed_el.style.bottom = (document.height - (window.scrollY + window.innerHeight) + bottom) + 'px';
//Switch back when focus is lost.
function blured() {
fixed_el.style.position = '';
fixed_el.style.bottom = '';
input_el.removeEventListener('blur', blured);
}
input_el.addEventListener('blur', blured);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
<title>iOS 8 position: fixed; input element workaround</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
position: relative;
}
#a {
height: 2000px;
background: linear-gradient(red, blue);
}
#b {
position: fixed;
bottom: 20px;
left: 0;
width: 100%;
height: 100px;
background: rgba(0, 0, 0, 0.5);
}
textarea {
width: 80%;
height: 100%;
margin-left: 10%;
}
</style>
</head>
<body>
<div id="a"></div>
<div id="b">
<textarea></textarea>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment