Skip to content

Commit 29a33d7

Browse files
committed
fix(keyboard): better scroll to. #4849 #4645
1 parent 9780c40 commit 29a33d7

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

Diff for: js/utils/dom.js

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
};
113113
},
114114

115+
getOffsetTop: function(el) {
116+
var curtop = 0;
117+
if (el.offsetParent) {
118+
do {
119+
curtop += el.offsetTop;
120+
} while (el = el.offsetParent);
121+
return curtop;
122+
}
123+
},
124+
115125
/**
116126
* @ngdoc method
117127
* @name ionic.DomUtil#ready

Diff for: js/views/scrollViewNative.js

+56-8
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,17 @@
340340
* into view.
341341
*/
342342
self.scrollChildIntoView = function(e) {
343+
var rect = container.getBoundingClientRect();
344+
if(!self.__originalContainerHeight) {
345+
self.__originalContainerHeight = rect.height;
346+
}
347+
343348
// D
344-
var scrollBottomOffsetToTop = container.getBoundingClientRect().bottom;
349+
var scrollBottomOffsetToTop = rect.bottom;
345350
// D - A
346-
scrollViewOffsetHeight = container.offsetHeight;
351+
scrollViewOffsetHeight = self.__originalContainerHeight;
352+
//console.log('Scroll view offset height', scrollViewOffsetHeight);
353+
//console.dir(container);
347354
var alreadyShrunk = self.isShrunkForKeyboard;
348355

349356
var isModal = container.parentNode.classList.contains('modal');
@@ -378,14 +385,25 @@
378385
var scrollBottomOffsetToBottom = e.detail.viewportHeight - scrollBottomOffsetToTop;
379386

380387
// 0 or D - B if D > B E - B E - D
381-
var keyboardOffset = e.detail.keyboardHeight - scrollBottomOffsetToBottom;
388+
//var keyboardOffset = e.detail.keyboardHeight - scrollBottomOffsetToBottom;
382389

383390
ionic.requestAnimationFrame(function(){
384391
// D - A or B - A if D > B D - A max(0, D - B)
385-
scrollViewOffsetHeight = keyboardOffset >= 0 ? scrollViewOffsetHeight + keyboardOffset : scrollViewOffsetHeight - keyboardOffset;
392+
scrollViewOffsetHeight = Math.max(0, Math.min(self.__originalContainerHeight, self.__originalContainerHeight - (e.detail.keyboardHeight - 43)));//keyboardOffset >= 0 ? scrollViewOffsetHeight - keyboardOffset : scrollViewOffsetHeight + keyboardOffset;
393+
394+
//console.log('Old container height', self.__originalContainerHeight, 'New container height', scrollViewOffsetHeight, 'Keyboard height', e.detail.keyboardHeight);
386395

387396
container.style.height = scrollViewOffsetHeight + "px";
388397

398+
/*
399+
if (ionic.Platform.isIOS()) {
400+
// Force redraw to avoid disappearing content
401+
var disp = container.style.display;
402+
container.style.display = 'none';
403+
var trick = container.offsetHeight;
404+
container.style.display = disp;
405+
}
406+
*/
389407
container.classList.add('keyboard-up');
390408
//update scroll view
391409
self.resize();
@@ -413,26 +431,42 @@
413431
if (e.detail.isElementUnderKeyboard) {
414432

415433
ionic.requestAnimationFrame(function(){
434+
var pos = ionic.DomUtil.getOffsetTop(e.detail.target);
435+
setTimeout(function() {
436+
if (ionic.Platform.isIOS()) {
437+
ionic.tap.cloneFocusedInput(container, self);
438+
}
439+
// Scroll the input into view, with a 100px buffer
440+
self.scrollTo(0, pos - (rect.top + 100), true);
441+
self.onScroll();
442+
}, 32);
443+
444+
/*
416445
// update D if we shrunk
417446
if (self.isShrunkForKeyboard && !alreadyShrunk) {
418447
scrollBottomOffsetToTop = container.getBoundingClientRect().bottom;
448+
console.log('Scroll bottom', scrollBottomOffsetToTop);
419449
}
420450
421451
// middle of the scrollview, this is where we want to scroll to
422452
// (D - A) / 2
423453
var scrollMidpointOffset = scrollViewOffsetHeight * 0.5;
454+
console.log('Midpoint', scrollMidpointOffset);
424455
//console.log("container.offsetHeight: " + scrollViewOffsetHeight);
425456
426457
// middle of the input we want to scroll into view
427458
// C
428459
var inputMidpoint = ((e.detail.elementBottom + e.detail.elementTop) / 2);
460+
console.log('Input midpoint');
429461
430462
// distance from middle of input to the bottom of the scroll view
431463
// C - D C D
432464
var inputMidpointOffsetToScrollBottom = inputMidpoint - scrollBottomOffsetToTop;
465+
console.log('Input midpoint offset', inputMidpointOffsetToScrollBottom);
433466
434467
//C - D + (D - A)/2 C - D (D - A)/ 2
435468
var scrollTop = inputMidpointOffsetToScrollBottom + scrollMidpointOffset;
469+
console.log('Scroll top', scrollTop);
436470
437471
if ( scrollTop > 0) {
438472
if (ionic.Platform.isIOS()) {
@@ -447,6 +481,7 @@
447481
self.onScroll();
448482
}
449483
}
484+
*/
450485
});
451486
}
452487

@@ -461,10 +496,23 @@
461496
self.isShrunkForKeyboard = false;
462497
container.style.height = "";
463498

464-
// Read after setting this to avoid rendering issues like white boxes.
465-
ionic.requestAnimationFrame(function() {
466-
container.classList.remove('keyboard-up');
467-
});
499+
/*
500+
if (ionic.Platform.isIOS()) {
501+
// Force redraw to avoid disappearing content
502+
var disp = container.style.display;
503+
container.style.display = 'none';
504+
var trick = container.offsetHeight;
505+
container.style.display = disp;
506+
}
507+
*/
508+
509+
self.__originalContainerHeight = container.getBoundingClientRect().height;
510+
511+
if (ionic.Platform.isIOS()) {
512+
ionic.requestAnimationFrame(function() {
513+
container.classList.remove('keyboard-up');
514+
});
515+
}
468516

469517
}
470518
self.resize();

Diff for: scss/_scaffolding.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ ion-infinite-scroll {
210210
-webkit-transform: translate3d(0, 0, 0); // fix iOS bug where relative children of scroller disapear while scrolling. see: http://stackoverflow.com/questions/9807620/ipad-safari-scrolling-causes-html-elements-to-disappear-and-reappear-with-a-dela
211211
}
212212

213-
&.keyboard-up {
213+
&.keyboard-up:not(.keyboard-up-confirm) {
214214
overflow: hidden;
215215
}
216216
}

0 commit comments

Comments
 (0)