<![CDATA[Teknotit Snipet Manager tarfst: Snippets: mobile]]>http://snipet.teknotit.com/index.php <![CDATA[Implementing a Fixed Position iOS Web Application]]> Scroller = function(element) { this.element = this; this.startTouchY = 0; this.animateTo(0); element.addEventListener(‘touchstart’, this, false); element.addEventListener(‘touchmove’, this, false); element.addEventListener(‘touchend’, this, false); } Scroller.prototype.handleEvent = function(e) { switch (e.type) { case “touchstart”: this.onTouchStart(e); break; case “touchmove”: this.onTouchMove(e); break; case “touchend”: this.onTouchEnd(e); break; } } Scroller.prototype.onTouchStart = function(e) { // This will be shown in part 4. this.stopMomentum(); this.startTouchY = e.touches[0].clientY; this.contentStartOffsetY = this.contentOffsetY; } Scroller.prototype.onTouchMove = function(e) { if (this.isDragging()) { var currentY = e.touches[0].clientY; var deltaY = currentY - this.startTouchY; var newY = deltaY + this.contentStartOffsetY; this.animateTo(newY); } } Scroller.prototype.onTouchEnd = function(e) { if (this.isDragging()) { if (this.shouldStartMomentum()) { // This will be shown in part 3. this.doMomentum(); } else { this.snapToBounds(); } } } Scroller.prototype.animateTo = function(offsetY) { this.contentOffsetY = offsetY; // We use webkit-transforms with translate3d because these animations // will be hardware accelerated, and therefore significantly faster // than changing the top value. this.element.style.webkitTransform = ‘translate3d(0, ‘ + offsetY + ‘px, 0)’; } // Implementation of this method is left as an exercise for the reader. // You need to measure the current position of the scrollable content // relative to the frame. If the content is outside of the boundaries // then simply reposition it to be just within the appropriate boundary. Scroller.prototype.snapToBounds = function() { ... } // Implementation of this method is left as an exercise for the reader. // You need to consider whether their touch has moved past a certain // threshold that should be considered ‘dragging’. Scroller.prototype.isDragging = function() { ... } // Implementation of this method is left as an exercise for the reader. // You need to consider the end velocity of the drag was past the // threshold required to initiate momentum. Scroller.prototype.shouldStartMomentum = function() { ... } ]]> Tue, 25 Nov 2014 02:42:19 +0000 <![CDATA[Hide the address bar in a fullscreen iPhone or Android web app]]> var page = document.getElementById('page'), ua = navigator.userAgent, iphone = ~ua.indexOf('iPhone') || ~ua.indexOf('iPod'), ipad = ~ua.indexOf('iPad'), ios = iphone || ipad, // Detect if this is running as a fullscreen app from the homescreen fullscreen = window.navigator.standalone, android = ~ua.indexOf('Android'), lastWidth = 0; if (android) { // Android's browser adds the scroll position to the innerHeight, just to // make this really fucking difficult. Thus, once we are scrolled, the // page height value needs to be corrected in case the page is loaded // when already scrolled down. The pageYOffset is of no use, since it always // returns 0 while the address bar is displayed. window.onscroll = function() { page.style.height = window.innerHeight + 'px' } } var setupScroll = function() { // Start out by adding the height of the location bar to the width, so that // we can scroll past it if (ios) { // iOS reliably returns the innerWindow size for documentElement.clientHeight // but window.innerHeight is sometimes the wrong value after rotating // the orientation var height = document.documentElement.clientHeight; // Only add extra padding to the height on iphone / ipod, since the ipad // browser doesn't scroll off the location bar. if (iphone && !fullscreen) height += 60; page.style.height = height + 'px'; } else if (android) { // The stock Android browser has a location bar height of 56 pixels, but // this very likely could be broken in other Android browsers. page.style.height = (window.innerHeight + 56) + 'px' } // Scroll after a timeout, since iOS will scroll to the top of the page // after it fires the onload event setTimeout(scrollTo, 0, 0, 1); }; (window.onresize = function() { var pageWidth = page.offsetWidth; // Android doesn't support orientation change, so check for when the width // changes to figure out when the orientation changes if (lastWidth == pageWidth) return; lastWidth = pageWidth; setupScroll(); })();]]> Wed, 12 Nov 2014 04:34:24 +0000