Skip to content

Commit 5875ebc

Browse files
committed
feat(scroll): disable body scroll on iOS safari
1 parent ebe134b commit 5875ebc

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

js/angular/directive/content.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ function($timeout, $controller, $ionicBind, $ionicConfig) {
132132
delegateHandle: attr.delegateHandle,
133133
startX: $scope.$eval($scope.startX) || 0,
134134
startY: $scope.$eval($scope.startY) || 0,
135-
nativeScrolling: true
135+
nativeScrolling: true,
136+
disableBodyBounce: $ionicConfig.scrolling.disableBodyBounce()
136137
};
137138

138139
} else {

js/angular/service/ionicConfig.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ IonicModule
260260
toggle: PLATFORM
261261
},
262262
scrolling: {
263-
jsScrolling: PLATFORM
263+
jsScrolling: PLATFORM,
264+
disableBodyBounce: PLATFORM
264265
},
265266
spinner: {
266267
icon: PLATFORM
@@ -309,7 +310,8 @@ IonicModule
309310
},
310311

311312
scrolling: {
312-
jsScrolling: false
313+
jsScrolling: false,
314+
disableBodyBounce: false
313315
},
314316

315317
spinner: {
@@ -324,14 +326,18 @@ IonicModule
324326
templates: {
325327
maxPrefetch: 30
326328
}
327-
328329
});
329330

330331

331332

332333
// iOS (it is the default already)
333334
// -------------------------
334-
setPlatformConfig('ios', {});
335+
setPlatformConfig('ios', {
336+
scrolling: {
337+
jsScrolling: false,
338+
disableBodyBounce: true
339+
}
340+
});
335341

336342

337343

js/views/scrollViewNative.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,26 @@
441441
self.resize();
442442
};
443443

444+
var startY = 0;
445+
var curY = 0;
446+
var height = 0;
447+
self.handleWindowTouchStart = function(e) {
448+
startY = e.touches ? e.touches[0].screenY : e.screenY;
449+
height = self.el.offsetHeight;
450+
};
451+
452+
self.handleWindowTouchMove = function(e) {
453+
curY = e.touches ? e.touches[0].screenY : e.screenY;
454+
455+
var atTop = (startY <= curY && self.el.scrollTop === 0);
456+
var atBottom = (startY >= curY && self.el.scrollHeight - self.el.scrollTop === height);
457+
458+
if(atTop || atBottom) {
459+
// Disable body bounce
460+
e.preventDefault();
461+
}
462+
};
463+
444464
container.addEventListener('scroll', self.onScroll);
445465

446466
//Broadcasted when keyboard is shown on some platforms.
@@ -453,6 +473,12 @@
453473
// Since we can only resize scroll views that are currently visible, just resize
454474
// the current scroll view when the keyboard is closed.
455475
document.addEventListener('resetScrollView', self.resetScrollView);
476+
477+
if(self.options.disableBodyBounce && !ionic.Platform.isWebView()) {
478+
window.addEventListener('touchstart', self.handleWindowTouchStart);
479+
window.addEventListener('touchmove', self.handleWindowTouchMove);
480+
}
481+
456482
},
457483

458484
__cleanup: function() {
@@ -465,6 +491,11 @@
465491
container.removeEventListener('scrollChildIntoView', self.scrollChildIntoView);
466492
container.removeEventListener('resetScrollView', self.resetScrollView);
467493

494+
if(self.options.disableBodyBounce && !ionic.Platform.isWebView()) {
495+
window.removeEventListener('touchstart', self.handleWindowTouchStart);
496+
window.removeEventListener('touchmove', self.handleWindowTouchMove);
497+
}
498+
468499
ionic.tap.removeClonedInputs(container, self);
469500

470501
delete self.__container;

0 commit comments

Comments
 (0)