try to make it a bit smoother

This commit is contained in:
Sam 2015-03-25 15:23:26 +11:00
parent eec87b5513
commit 692433f0cf
1 changed files with 44 additions and 31 deletions

View File

@ -16,38 +16,10 @@ function positioningWorkaround($fixedElement) {
const fixedElement = $fixedElement[0];
var positioningHack = function(evt){
const self = this;
var done = false;
// allow for keyboard in iPad portrait
var iPadOffset = 0;
if (window.innerHeight > window.innerWidth && navigator.userAgent.match(/iPad/)) {
// there is no way to get virtual keyboard height
iPadOffset = 640 - $(fixedElement).height();
}
var positionElement = _.debounce(function(){
if (done) {
return;
}
fixedElement.style.top = window.scrollY + iPadOffset + 'px';
}, 400);
if (fixedElement.style.position !== 'absolute') {
evt.preventDefault();
fixedElement.style.position = 'absolute';
// get out of the way while opening keyboard
fixedElement.style.top = '0px';
}
$(window).on('scroll', positionElement);
var blurred = function() {
if (_.include($(document.activeElement).parents(), fixedElement)) {
var blurredNow = function(evt) {
if (!done && _.include($(document.activeElement).parents(), fixedElement)) {
// something in focus so skip
return;
}
@ -55,17 +27,57 @@ function positioningWorkaround($fixedElement) {
done = true;
fixedElement.style.position = '';
fixedElement.style.top = '';
self.removeEventListener('blur', blurred);
$(window).off('scroll', positionElement);
if (evt) {
evt.target.removeEventListener('blur', blurred);
}
$(window).off('scroll.position-hack');
};
blurred = _.debounce(blurred, 250);
var blurred = _.debounce(blurredNow, 250);
var positioningHack = function(evt){
const self = this;
done = false;
// we need this, otherwise changing focus means we never clear
self.addEventListener('blur', blurred);
if (fixedElement.style.position === 'absolute') {
if (this !== document.activeElement) {
evt.preventDefault();
self.focus();
}
return;
}
self.addEventListener('blur', blurred);
fixedElement.style.position = 'absolute';
// get out of the way while opening keyboard
fixedElement.style.top = '0px';
var iPadOffset = 0;
if (window.innerHeight > window.innerWidth && navigator.userAgent.match(/iPad/)) {
// there is no way to get virtual keyboard height
iPadOffset = 640 - $(fixedElement).height();
}
var oldScrollY = 0;
var positionElement = _.debounce(function(){
if (done) {
return;
}
if (Math.abs(oldScrollY - window.scrollY) < 20) {
return;
}
oldScrollY = window.scrollY;
fixedElement.style.top = window.scrollY + iPadOffset + 'px';
}, 400);
$(window).on('scroll.position-hack', positionElement);
evt.preventDefault();
self.focus();
};
function attachTouchStart(elem, fn) {
@ -78,6 +90,7 @@ function positioningWorkaround($fixedElement) {
const checkForInputs = _.debounce(function(){
$fixedElement.find('button,a').each(function(){
attachTouchStart(this, function(evt){
done = true;
evt.preventDefault();
$(this).click();
});