From c9b384c8a85ded5da956cc2cc5603a902981661b Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Wed, 27 Nov 2013 14:54:48 -0600 Subject: [PATCH] Slightly easier scroll API --- dist/js/ionic.js | 58 ++++++++++++++- js/ext/angular/test/zyng.html | 2 +- js/views/scrollZyng.js | 58 ++++++++++++++- test/scroll_z.html | 134 ++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 7 deletions(-) create mode 100644 test/scroll_z.html diff --git a/dist/js/ionic.js b/dist/js/ionic.js index 699cfcd4a8..e9310fa613 100644 --- a/dist/js/ionic.js +++ b/dist/js/ionic.js @@ -3112,14 +3112,15 @@ var Scroller; }; - /** * A pure logic 'component' for 'virtual' scrolling/zooming. */ ionic.views.Scroller = ionic.views.View.inherit({ - initialize: function(callback, options) { + initialize: function(options) { - this.__callback = callback; + this.__content = options.content; + + this.__callback = this.getRenderFn(); this.options = { @@ -3344,6 +3345,57 @@ var Scroller; --------------------------------------------------------------------------- */ + getRenderFn: function() { + var content = this.__content; + + var docStyle = document.documentElement.style; + + var engine; + if ('MozAppearance' in docStyle) { + engine = 'gecko'; + } else if ('WebkitAppearance' in docStyle) { + engine = 'webkit'; + } else if (typeof navigator.cpuClass === 'string') { + engine = 'trident'; + } + + var vendorPrefix = { + trident: 'ms', + gecko: 'Moz', + webkit: 'Webkit', + presto: 'O' + }[engine]; + + var helperElem = document.createElement("div"); + var undef; + + var perspectiveProperty = vendorPrefix + "Perspective"; + var transformProperty = vendorPrefix + "Transform"; + + if (helperElem.style[perspectiveProperty] !== undef) { + + return function(left, top, zoom) { + content.style[transformProperty] = 'translate3d(' + (-left) + 'px,' + (-top) + 'px,0) scale(' + zoom + ')'; + }; + + } else if (helperElem.style[transformProperty] !== undef) { + + return function(left, top, zoom) { + content.style[transformProperty] = 'translate(' + (-left) + 'px,' + (-top) + 'px) scale(' + zoom + ')'; + }; + + } else { + + return function(left, top, zoom) { + content.style.marginLeft = left ? (-left/zoom) + 'px' : ''; + content.style.marginTop = top ? (-top/zoom) + 'px' : ''; + content.style.zoom = zoom || ''; + }; + + } + }, + + /** * Configures the dimensions of the client (outer) and content (inner) elements. * Requires the available space for the outer element and the outer size of the inner element. diff --git a/js/ext/angular/test/zyng.html b/js/ext/angular/test/zyng.html index b25b7cb624..567d736b6e 100644 --- a/js/ext/angular/test/zyng.html +++ b/js/ext/angular/test/zyng.html @@ -65,7 +65,7 @@ var container = document.getElementById('c'); // Initialize Scroller - var scroller = new ionic.views.Scroller(render, { + var scroller = new ionic.views.Scroller({ scrollingX: false }); diff --git a/js/views/scrollZyng.js b/js/views/scrollZyng.js index 885e50d3cc..c9baa8e2d6 100644 --- a/js/views/scrollZyng.js +++ b/js/views/scrollZyng.js @@ -278,14 +278,15 @@ var Scroller; }; - /** * A pure logic 'component' for 'virtual' scrolling/zooming. */ ionic.views.Scroller = ionic.views.View.inherit({ - initialize: function(callback, options) { + initialize: function(options) { - this.__callback = callback; + this.__content = options.content; + + this.__callback = this.getRenderFn(); this.options = { @@ -510,6 +511,57 @@ var Scroller; --------------------------------------------------------------------------- */ + getRenderFn: function() { + var content = this.__content; + + var docStyle = document.documentElement.style; + + var engine; + if ('MozAppearance' in docStyle) { + engine = 'gecko'; + } else if ('WebkitAppearance' in docStyle) { + engine = 'webkit'; + } else if (typeof navigator.cpuClass === 'string') { + engine = 'trident'; + } + + var vendorPrefix = { + trident: 'ms', + gecko: 'Moz', + webkit: 'Webkit', + presto: 'O' + }[engine]; + + var helperElem = document.createElement("div"); + var undef; + + var perspectiveProperty = vendorPrefix + "Perspective"; + var transformProperty = vendorPrefix + "Transform"; + + if (helperElem.style[perspectiveProperty] !== undef) { + + return function(left, top, zoom) { + content.style[transformProperty] = 'translate3d(' + (-left) + 'px,' + (-top) + 'px,0) scale(' + zoom + ')'; + }; + + } else if (helperElem.style[transformProperty] !== undef) { + + return function(left, top, zoom) { + content.style[transformProperty] = 'translate(' + (-left) + 'px,' + (-top) + 'px) scale(' + zoom + ')'; + }; + + } else { + + return function(left, top, zoom) { + content.style.marginLeft = left ? (-left/zoom) + 'px' : ''; + content.style.marginTop = top ? (-top/zoom) + 'px' : ''; + content.style.zoom = zoom || ''; + }; + + } + }, + + /** * Configures the dimensions of the client (outer) and content (inner) elements. * Requires the available space for the outer element and the outer size of the inner element. diff --git a/test/scroll_z.html b/test/scroll_z.html new file mode 100644 index 0000000000..ecf5d88672 --- /dev/null +++ b/test/scroll_z.html @@ -0,0 +1,134 @@ + + + + Content + + + + + + + + + +
+
+
+
+
+ + + + +