Miss Part 1? Go give it a read then.

So now that you have your custom scrollees, how about we fix those arrow keys, mmkay? First lets take a look at why the keys aren't working.

The key here is you must focus

The main issue here is one of focus, or rather lack of focus. When your page loads the wrapper div needs to have focus, or be activated for it to register the the up and down arrow keys. I am not sure what the default behavior for a web browser is, but I think the document window itself has focus on load. Once you interact with the page you transfer focus to the element(s) in question.

Anywhoo, we need to make sure that the wrapper div has focus so it can register the use of up and down arrow keys. Luckily this is pretty simple to do.

Time for a little javascript

I had originally spec'ed out activating the use of the arrow keys, as well as adding some smooth scrolling to the site, but after some initial tests decided that although it was great to look at, it introduced some unwanted usability issues. In the end I decided to just restore normal user expectations and leave it at that.

For this example I will be using a smidge of jQuery, and a smidge of straight javascript. I already include jQuery in all my themes for various eye candy interactions so it was handy to pull it in for this.

If you prefer another framework, as long as it has a method for checking with the DOM is ready, you are good to go. Here is the javascript, in its entirety:

<script>
    $(document).ready(function() {
          document.links[0].focus();
     });
</script>

Badda bing, badda boom. So, here's the breakdown. We wait for the DOM to be ready for us to munge with it, and when it is ready, we munge. Brother do we munge.

Basically we are telling the page to give focus to the first link it finds in the page. Since this link is inside our wrapper div, wrapper now focus as well which means that our arrow keys will now work as expected.

Not to difficult really. Oh, and I mentioned in the previous installment that we would see why the following CSS was important:

a:focus { outline:0px;}

When a link has focus you get the dotted outline applied to it which is less than desireable. This CSS removes that outline. Now, there is an argument that we are impeding usability by removing the outline, and there is a great deal of merit to that argument. I don't believe I would do this on a website meant for commercial consumption, but for a personal site meant for learning, sharing and exploration it sits pretty well I think.

Well that is it for now. Hopefully you have found this micro-series helpful. As always comments and questions are welcome.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Part 2 of this tutorial is now up, give it a gander why don't ya?

I have, as others around the interwebs, added a bit of awesomeness to the Sillyness by leveraging the scrollbar specific CSS that is in the newer builds of WebKit.

The two downfalls of using these rules are that they are only supported by WebKit based browsers (no love from the Fox), and that it kills your up and down arrow keys. This is a fairly serious usability issue. Can you imagine if you couldn't play online poker on certain browsers? It would be frustrating and would make you think twice about using it. Last night I set about working out how to fix this sticky wicket. The results are todays tutorial.

Targeting ftw

The simple answer here is to do some user agent sniffing, and only load the files needed for this tom foolery if a webkit enabled browser is found. A quick snippet of PHP and we are on our way:


   
   

At this point we should only have the CSS and JS to power this little diddy loaded when a browser that can use it is accessing our site. Rawk. Now lets take a look at the CSS. It is pretty lengthy, so lets look at it in chunks.

/* http://almaer.com/scrollbar/debug.html */
#wrapper {
	overflow: hidden;
	background: #090f13 url('images/background.jpg') repeat top left;
	background-attachment: fixed;
	font-family: helvetica;
	margin: 0px;
	padding: 0px;
	line-height: 1.5em;
	color: #fff;
	text-shadow: 1px 1px 2px #000;
}

Okay, so here we are setting up the whole shebang. Basically we are wrapping the entire site in a div (#wrapper), and setting the overflow to hidden. You might be wondering why we aren't just applying this to html or body. I will get into why we aren't doing this later, but just trust me, it doesn't work properly.

The rest of the CSS there is just setting up 2010 my theme so lets skip on to the good stuff. Setting up the custom scrollbar.

::-webkit-scrollbar {
	width: 6px;
}

::-webkit-scrollbar-track-piece {
	background-color: #333;
	-webkit-border-radius: 3px;
	margin-top: 10px;
	margin-bottom: 10px;
}
::-webkit-scrollbar-thumb:vertical {
	height: 50px;
	background-color: #666;
	-webkit-border-radius: 3px;
}

This isn't really all that complicated either. A scrollbar is made up of a specific set of UI elements, but we are only going to focus on a few of them. The full list can be found on the Surfin Safari Blog.

So lets look at the CSS we have to play with. Right now we are just concerned with the scrollbar thumb and the track. The above CSS says to create a vertical scrollbar that is 6 pixels wide, that has a background color of #333 and should have a top and bottom margin of 10px. Round it off with some rounded corner action and you are set. Should be just like creating the CSS for any div.

Next we setup the scrollbar thumb, you know the bit you click on and drag to scroll. This should be pretty straightforward as well. Make the thumb piece 50px at the smallest, color it #666 and give it some rounded corners to make it purty. At this point we should now have a functioning scrollbar.

The next section of code adds in exceptions for Mobile Safari.

@media only screen and (max-device-width:480px) {
#wrapper {
	overflow: auto;
}
}
	
#wrapper {
	position: absolute;
	top: 0;
	left: 0;
	bottom: 0;
	right: 10px;
	overflow-y: scroll;
	overflow-x: hidden;
}

@media only screen and (max-device-width:480px) {
	#wrapper {
		position: relative;
		top: auto;
		right: auto;
		bottom: auto;
		left: auto;
		overflow: auto;
	}
}

Lastly we need to add in a rule that won't make much sense now, but will be absolutely necessary as we get into part two of this tutorial.

a:focus { outline:0px;}

Alright that is it for part 1. By this time you should have a nifty scrollbar, but dead arrow keys. Tomorrow I will go through how to reactivate the arrow keys ensuring a great experience on your site, no matter the means an individual employs to access it.

As always, questions and comments are welcome.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

This is Sillyness Spelled Wrong Intentionally. Going strong for 9 years, 8 months and 3 weeks