Scrolling a overflow:auto; element on a touch screen device

Monday, May 03, 2010

In mobile Safari on the iPhone, iPod Touch, and iPad (as well as the webkit based browser on Android phones) it's not immediately obvious how to scroll a div that has overflow:auto; set on it. If this were a desktop browser you would see scrollbars and be able to manipulate those or even use your mouse wheel. No such concepts exist on a touch screen device!

To scroll the entire page you just touch it and move your finger. But when you touch the element that would normally scroll, the entire page scrolls instead. This is a little bit broken in my opinion since there's no visual indicator that you aren't seeing all the content. However, if you are on a site and you know there's a scrollable div there is a simple (but not obvious) workaround. Simple use two fingers at the same time and scroll them in the same direction.

This works OK but like I said it's not obvious, there's still no indicator that the content is scrollable, and when you use more than one finger you might accidentally trigger some other gesture like scaling the page. I recently ran into this exact issue at work and came up with a pretty solid solution in javascript. I broke this down into two simple functions, but the last one is where the magic happens.

This first function simply attempts to create a new touch event. Only touch screen browsers like mobile Safari have these events, so if it doesn't throw an error then we are using a touch screen device. Otherwise it's probably a desktop browser.

function isTouchDevice(){
	try{
		document.createEvent("TouchEvent");
		return true;
	}catch(e){
		return false;
	}
}

Next, this function calls the isTouchDevice() function, and if it succeeds we attach some special touch events to the page. First when a touch event begins (the touchstart event) we get the current scroll position and prevent the default browser behavior, which in this case would be to scroll the page. Next when you move your finger the touchmove event is called. Here we just subtract the position of your finger from the scroll position we saved earlier and again prevent the page from scrolling.

function touchScroll(id){
	if(isTouchDevice){ //if touch events exist...
		var el=document.getElementById(id);
		var scrollStartPos=0;

		document.getElementById(id).addEventListener("touchstart", function(event) {
			scrollStartPos=this.scrollTop+event.touches[0].pageY;
			event.preventDefault();
		},false);

		document.getElementById(id).addEventListener("touchmove", function(event) {
			this.scrollTop=scrollStartPos-event.touches[0].pageY;
			event.preventDefault();
		},false);
	}
}

So that's it, just include these functions on your page and just call it by passing in the ID of the element you want to scroll. Like so: touchScroll("MyElement");. You can see a working demo here: http://chris-barr.com/files/touchScroll.htm. I feel like this is a better way of doing things because it's more intuitive since you're just using one finger, and it's potentially more obvious. Even if you don't immediately know there's hidden content, you might accidentally touch this while scrolling the page and realize there's more to see in this div.

Posted by Chris Barr on 05/03 at 11:48 PM
Filed under Web, Code, Javascript, CSS5 Comments

Typographical Javasctipt Clock

I was bored, so I made one.  Check it out here: http://chris-barr.com/files/text%20clock/

Posted by Chris Barr on 05/03 at 11:41 PM
Filed under Projects, Web, Code, Javascript0 Comments

Hot Chip - I Feel Better

Wednesday, March 17, 2010

Posted by Chris Barr on 03/17 at 09:38 PM
Filed under Music, Video/Motion Graphics, Comedy0 Comments

Kid Cudi (feat. MGMT & Ratatat) - Pursuit Of Happiness

Thursday, March 11, 2010

Posted by Chris Barr on 03/11 at 12:20 AM
Filed under Music, Video/Motion Graphics0 Comments

Broken Bells - The High Road

Posted by Chris Barr on 03/11 at 12:12 AM
Filed under Music, Video/Motion Graphics0 Comments

New Years Resolutions

Friday, January 01, 2010

It’s a new year, and a new decade. I know 2010 will be a great year for me, and I’m going to do my best to make sure of that by setting a few new years resolutions for myself.

  • Take 1 photo a day and blog it to force myself to get more creative - I’m keeping it in a tumblr blog you can follow here: http://2010photos.tumblr.com
  • Lose 10 pounds
  • Make a short film about my trip to Togo, Africa in July
  • Try to get a photo published somewhere or in a local gallery

So most of these are photography related, but I think these all revolve around my trip to Africa later this year. This is more than likely a once in a lifetime trip, and I really want to make sure that I take some amazing photos and videos to remember it by.

Posted by Chris Barr on 01/01 at 05:01 PM
Filed under General, Photography, Projects3 Comments

So, Photoshop crashed on me.

Thursday, December 31, 2009

I hope this helps the nice people over at Adobe.

Posted by Chris Barr on 12/31 at 07:13 PM
Filed under Design, Macintosh, Comedy, Tech0 Comments

Fantastic Album Art

Friday, November 27, 2009

I wanted to show off some amazing album art from my music library, so I've picked 24 of my absolute favorites (arranged alphabetically). My rules for selecting these was to purely judge them based on the artwork, not on the actual music itself.

Some just have amazing photography, others just focus on great use of typography. All in all, I love each piece of album art (and the music.)



Ben Folds - Ben Folds Live

[iTunes] | [Amazon]


Chemical Brothers - Push The Button

[iTunes] | [Amazon]


Death Cab For Cutie - Transatlanticism

[iTunes] | [Amazon]


Derek Webb - Stockholm Syndrome

[iTunes] | [Amazon]


Feist - The Reminder

[iTunes] | [Amazon]


Frou Frou - Details

[iTunes] | [Amazon]


Iron and Wine - Our Endless Numbered Days

[iTunes] | [Amazon]


John Mayer Trio- Try!

[iTunes] | [Amazon]


John Reuben - The Boy Vs. the Cynic

[iTunes] | [Amazon]


Johnny Cash - The Legend of Johnny Cash

[Amazon]


José González - In Our Nature

[iTunes] | [Amazon]


The Mars Volta - The Bedlam in Goliath

[iTunes] | [Amazon]


Modest Mouse - Good News for People Who Love Bad News

[iTunes] | [Amazon]


Norah Jones - Not Too Late

[iTunes] | [Amazon]


Radiohead - Hail to the Theif

[iTunes] | [Amazon]


Regina Spektor - Begin to Hope

[iTunes] | [Amazon]


Sarah McLachlan - Wintersong

[iTunes] | [Amazon]


Shawn McDonald - Simply Nothing

[iTunes] | [Amazon]


Sufjan Stevens - Illinoise

[iTunes] | [Amazon]


The Flaming Lips - At War With The Mystics

[iTunes] | [Amazon]


The Shins - Chutes Too Narrow

[iTunes] | [Amazon]


UNKLE - War Stories

[iTunes] | [Amazon]


Wilco - Yankee Hotel Foxtrot

[iTunes] | [Amazon]


William Shatner - Has Been

[iTunes] | [Amazon]
Posted by Chris Barr on 11/27 at 07:15 PM
Filed under Photography, Design, Music3 Comments

Ramona Falls - I Say Fever

Saturday, November 07, 2009

Posted by Chris Barr on 11/07 at 10:56 AM
Filed under Music1 Comments

Detect browser support for CSS properties

Tuesday, September 22, 2009

Adding shadows or rounded corners to any element is way more trouble than it should be, especially if you're tried out some of the newer CSS3 properties. A simple two lines of CSS can get you these amazingly gorgeous effects, but as expected they aren't supported in every browser (I'm looking at you Internet Explorer!)

Some older versions of browsers that do support these features don't use the official W3C method and you have you use a vendor specific CSS property,like so:

#coolElement{
    box-shadow:#000 3px 3px 8px;
    -moz-box-shadow:#000 3px 3px 8px;
    -webkit-box-shadow:#000 3px 3px 8px;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
}

So, that of course won't work in IE, and if you want to have rounded corners or shadows on IE you have to make some custom PNG images and align everything properly. It's a lot of work, but it must be done if you want it to look consistent across all browsers. I realized today that ideally I want to do both, meaning that I want to use the pure CSS method when it's available in the browser, and the old clunky way otherwise.

Sure I could just see what works in each browser and only allow certain versions of browsers, but I'd always have to come back and update that list as future browsers add support for these things. Instead, why not test and see if the browser supports these features first, and then decide what to do.

function supportsBoxShadow(){
  var s=document.body.style;
  return s.WebkitBoxShadow!== undefined||s.MozBoxShadow!==undefined||s.BoxShadow!==undefined;
}
function supportsBoderRadius(){
  var s=document.body.style;
  return s.WebkitBorderRadius!== undefined||s.MozBorderRadius!==undefined||s.BorderRadius!==undefined;
}

Those functions will return true if the CSS property is supported in the browser, and not just the W3C property, but the vendor specific property as well. If there's some other property you want to test, follow the same pattern and just use CamelCase and remove the hyphens.

So putting this into practice is pretty simple, assuming we use the CSS property defined above.

if(supportsBoxShadow()){
    //Woo! We don't have to do anything! CSS does all the work!
}else{
    //Write your complex code here to position your PNG shadows.... :(
}
Posted by Chris Barr on 09/22 at 09:43 AM
Filed under Web, Code, Javascript, CSS1 Comments
Page 1 of 8 pages  1 2 3 >  Last »