Skip to content

Commit

Permalink
feat(scroll): disable body scroll on iOS safari
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Dec 15, 2015
1 parent ebe134b commit 5875ebc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion js/angular/directive/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ function($timeout, $controller, $ionicBind, $ionicConfig) {
delegateHandle: attr.delegateHandle,
startX: $scope.$eval($scope.startX) || 0,
startY: $scope.$eval($scope.startY) || 0,
nativeScrolling: true
nativeScrolling: true,
disableBodyBounce: $ionicConfig.scrolling.disableBodyBounce()
};

} else {
Expand Down
14 changes: 10 additions & 4 deletions js/angular/service/ionicConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ IonicModule
toggle: PLATFORM
},
scrolling: {
jsScrolling: PLATFORM
jsScrolling: PLATFORM,
disableBodyBounce: PLATFORM
},
spinner: {
icon: PLATFORM
Expand Down Expand Up @@ -309,7 +310,8 @@ IonicModule
},

scrolling: {
jsScrolling: false
jsScrolling: false,
disableBodyBounce: false
},

spinner: {
Expand All @@ -324,14 +326,18 @@ IonicModule
templates: {
maxPrefetch: 30
}

});



// iOS (it is the default already)
// -------------------------
setPlatformConfig('ios', {});
setPlatformConfig('ios', {
scrolling: {
jsScrolling: false,
disableBodyBounce: true
}
});



Expand Down
31 changes: 31 additions & 0 deletions js/views/scrollViewNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,26 @@
self.resize();
};

var startY = 0;
var curY = 0;
var height = 0;
self.handleWindowTouchStart = function(e) {
startY = e.touches ? e.touches[0].screenY : e.screenY;
height = self.el.offsetHeight;
};

self.handleWindowTouchMove = function(e) {
curY = e.touches ? e.touches[0].screenY : e.screenY;

var atTop = (startY <= curY && self.el.scrollTop === 0);
var atBottom = (startY >= curY && self.el.scrollHeight - self.el.scrollTop === height);

if(atTop || atBottom) {
// Disable body bounce
e.preventDefault();
}
};

container.addEventListener('scroll', self.onScroll);

//Broadcasted when keyboard is shown on some platforms.
Expand All @@ -453,6 +473,12 @@
// Since we can only resize scroll views that are currently visible, just resize
// the current scroll view when the keyboard is closed.
document.addEventListener('resetScrollView', self.resetScrollView);

if(self.options.disableBodyBounce && !ionic.Platform.isWebView()) {
window.addEventListener('touchstart', self.handleWindowTouchStart);

This comment has been minimized.

Copy link
@mlynch

mlynch Dec 15, 2015

Author Contributor

Need to test with concurrent scroll views

window.addEventListener('touchmove', self.handleWindowTouchMove);
}

},

__cleanup: function() {
Expand All @@ -465,6 +491,11 @@
container.removeEventListener('scrollChildIntoView', self.scrollChildIntoView);
container.removeEventListener('resetScrollView', self.resetScrollView);

if(self.options.disableBodyBounce && !ionic.Platform.isWebView()) {
window.removeEventListener('touchstart', self.handleWindowTouchStart);
window.removeEventListener('touchmove', self.handleWindowTouchMove);
}

ionic.tap.removeClonedInputs(container, self);

delete self.__container;
Expand Down

0 comments on commit 5875ebc

Please sign in to comment.