Custom scrollbars in Safari part 1
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.
Enjoyed this article? Follow me on Twitter.