<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>a pint of javascript (and general web stuff) &#187; accessibility</title>
	<atom:link href="http://blog.gonchuki.com/category/accessibility/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gonchuki.com</link>
	<description></description>
	<lastBuildDate>Tue, 06 Jul 2010 21:45:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>usability 101: adding a global &quot;back to top&quot; to your site</title>
		<link>http://blog.gonchuki.com/archives/usability-101-adding-a-global-back-to-top-to-your-site/</link>
		<comments>http://blog.gonchuki.com/archives/usability-101-adding-a-global-back-to-top-to-your-site/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 19:26:04 +0000</pubDate>
		<dc:creator>gonchuki</dc:creator>
				<category><![CDATA[accessibility]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://blog.gonchuki.com/archives/usability-101-adding-a-global-back-to-top-to-your-site/</guid>
		<description><![CDATA[If there has to be one single usability issue I dislike from most sites, it is the &#034;back to top&#034; feature. Ugly links spread all over the place clogging the visuals of your site. It is for this reason that some months ago we developed a global unobtrusive back to top for Operator11.

identifying the problem
As [...]]]></description>
			<content:encoded><![CDATA[<p>If there has to be one single usability issue I dislike from most sites, it is the &#034;back to top&#034; feature. Ugly links spread all over the place clogging the visuals of your site. It is for this reason that some months ago we developed a global unobtrusive back to top for <a href="http://operator11.com">Operator11</a>.<br />
<span id="more-18"></span></p>
<h3 class="section">identifying the problem</h3>
<p>As we have seen before, <a href="http://poorbuthappy.com/ease/archives/2007/08/30/3804/ia-for-beginners-long-pages-work">long pages work</a>. But we always have an issue in the process: if one wants to navigate to other section of the site you have to scroll all the way up or use your other hand to hit the [Home] key.<br />
Given that the average internet user (I would say more than 90%) use their mouse to browse the web, either having to use your other hand and hunt for a key, scrolling up repetitively or clicking on that very top portion of that 16px wide scrollbar adds complexity to the task we are doing and <a href="http://www.humanized.com/weblog/2006/07/22/know_when_to_stop_designing_quantitatively/">decreases our efficiency</a>.<br />
Users love to hunt for information from page to page, and our task as user experience designers is to give them the tools to ease their stay.</p>
<h3 class="section">the wrong approach</h3>
<p>The classic solution of spreading links all over the place has great design, usability and accessibility problems.<br />
From the design point of view you either come up with a clever way to disguise the links or just leave them out there clogging the visuals. Not good either way!<br />
Usability wise, do you really know when the user wants to go back to the top of the page? Is there a need to spam the user with back to top links?<br />
And that spamming also means for decreased accessibility, screen reader or keyboard users will be constantly spammed with those meaningless links. They <strong>already</strong> know how to get back to the top of the page without the need for your &#034;shortcuts&#034;.<br />
Shouldn&#039;t there be a wiser and cleaner solution?</p>
<h3 class="section">developing a solution</h3>
<p>Fortunately, <strong>there is</strong> a fairly good solution. And armed with a bit of javascript and standards compliant <acronym title="Cascading Style Sheets">CSS</acronym> we can pull the trick.<br />
Did I just say javascript? why, yes! The back to top link is a navigational aid for mouse users and not real content in your page, think of it as an escape pod for when you want to get away from the current page.<br />
When developing this together with <a href="http://nicolassanguinetti.info">Nicolas Sanguinetti</a> for <a href="http://operator11.com">Operator11</a> our main goals were:<br />
- keep it clean<br />
- be unobtrusive<br />
- keep it usable<br />
The final solution ended as a tiny piece of code written with the aid of <a href="http://prototypejs.org">Prototype</a>.<br />
However as you may know, my personal and professional focus is into the <a href="http://mootools.net">mootools</a> library and for this reason the solution presented today will be an adaptation from the original code.</p>
<h3 class="section">methodology and code</h3>
<p><strong>Q:</strong> So when should we show the back to top link?<br />
<strong>A:</strong> Once we think the user needs it.<br />
Getting this metric can be a bit tricky and it&#039;s mostly arbitrary, but we finally settled for <strong>80% of a full page scroll</strong>. It&#039;s around this point that we need at least two mouse wheel scrolls to reach the top and hitting the back to top link becomes more efficient.<br />
the javascript:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'id'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'back-to-top'</span><span style="color: #339933;">,</span> 
    <span style="color: #3366CC;">'styles'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">0.6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'events'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
      <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> window.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'back to top'</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">injectInside</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'scroll'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> fn <span style="color: #339933;">=</span> window.<span style="color: #660066;">getScrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;addClass&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;removeClass&quot;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'back-to-top'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>fn<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visible'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>and this simple CSS:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#back-to-top</span><span style="color: #00AA00;">,</span>
<span style="color: #cc00cc;">#back-to-top</span><span style="color: #6666ff;">.visible</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
body<span style="color: #00AA00;">&gt;</span><span style="color: #cc00cc;">#back-to-top</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">17px</span> <span style="color: #933;">3px</span> <span style="color: #933;">7px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span> <span style="color: #933;">14px</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#555</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">triangle-gray-up.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
body<span style="color: #00AA00;">&gt;</span><span style="color: #cc00cc;">#back-to-top</span><span style="color: #6666ff;">.visible</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h3 class="section">pitfalls and caveats</h3>
<p>Just a few notes on usage and explanation of the methodology are needed.<br />
The code as you see purposely ignores IE6 as it has inexistent support for <code>position: fixed</code>. By using a CSS2 selector we gracefully degrade and ignore that browser and only display the link on standards compliant browsers.<br />
There is also another issue with <acronym title="Internet Explorer">IE</acronym> (6 and 7) that throws the infamous <a href="http://clientside.cnet.com/code-snippets/manipulating-the-dom/ie-and-operation-aborted/">Operation aborted</a> error and it&#039;s the reason behind using the <code>onload</code> event instead of <code>ondomready</code>.<br />
Last, but not the least, removing the hash part in the <acronym title="Uniform Resource Locator">URL</acronym> reloads the page instead of just cleaning that part of the location. This happens *at least* in Firefox and it is enough to conclude that setting <code>window.location.hash = ""</code> is not an option. For this is that we check the existence of the hash in the current location, and if not present we scroll to the top with javascript or else we set the location to the topmost element in our page.<br />
Be warned then, that if you use internal anchors in your site, you must have an element with an ID at the top of your page, preferably the body tag.<br />
For the curious, we re-set the hash part just for bookmarking purposes. If you hit the back to top link, then you are supposedly at the top and not on #someplace in the page.</p>
<h3 class="section">compatibility, download and legal bits</h3>
<p>The code is tested and works under Firefox, Opera 9, Safari 3 (both Windows and Mac) and Internet Explorer 7.<br />
As for <a href="http://mootools.net/download">mootools</a>, you just need the &#034;Element.Event&#034; and &#034;Window.Size&#034; modules, pretty much standard stuff.</p>
<p>By the way, you should have already noticed the back to top link in action in this post, feel free to try it out before leaving your comments ;)</p>
<p><a href="http://sandbox.gonchuki.com/btt/back-to-top.zip" class="download">Download back-to-top.zip</a></p>
<p><strong>License:</strong> <a href="http://creativecommons.org/licenses/by-sa/3.0/" class="cc">Creative Commons Attribution-Share Alike 3.0</a></p>
<p><strong>2009/02/27 &#8211; Update:</strong> The original code above was originally intended for mootools 1.11 (bleeding edge at that time in 2007), but given that 1.2 has been out for a long time I guess it&#039;s time for a quick update for <strong>mootools 1.2.x</strong><br />
I also took a few minutes to clean-up the <acronym title="Cascading Style Sheets">CSS</acronym> (no more CSS2 descendant rule to hide from IE6) and moved the &#034;fixed&#034; styles to the Element creation in the <acronym title="JavaScript">JS</acronym>. Now all that remains in the <acronym title="Cascading Style Sheets">CSS</acronym> is the customization on font, color, padding and background. The effect now also uses a fade in tween if it&#039;s available in your custom Mootools build.<br />
<a href="http://sandbox.gonchuki.com/btt/back-to-top_1.2.zip" class="download">Download back-to-top for mootools 1.2.x</a> [zip]</p>
<h3 class="section">addendum: original Prototype code</h3>
<p>Being that I was struck by <del datetime="2007-11-15T17:41:52+00:00">a very strange bug in mootools 1.11</del> <ins datetime="2007-11-15T17:41:52+00:00"><a href="http://dev.wp-plugins.org/ticket/723">a lame overriding of the addEvent function</a> by the <a href="http://wordpress.org/extend/plugins/stats/" rel="nofollow">Wordpress.com Stats plugin</a></ins>, I&#039;m putting here also the original Prototype-based implementation either for comparison or just for those Prototype users out there:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">BackToTop <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>
            BackToTop.<span style="color: #660066;">link</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'back-to-top'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;back to top&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">setOpacity</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0.6</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        BackToTop.<span style="color: #660066;">link</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> BackToTop.<span style="color: #660066;">onLinkClick</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;scroll&quot;</span><span style="color: #339933;">,</span> BackToTop.<span style="color: #660066;">onWindowScroll</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onLinkClick<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            window.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onWindowScroll<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">viewport</span>.<span style="color: #660066;">getScrollY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">viewport</span>.<span style="color: #660066;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            BackToTop.<span style="color: #660066;">link</span>.<span style="color: #660066;">addClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visible'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            BackToTop.<span style="color: #660066;">link</span>.<span style="color: #660066;">removeClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visible'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> BackToTop.<span style="color: #660066;">initialize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>notice that this was first written to be a <a href="http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype">LowPro</a> behaviour (and fitting more of Nicolas&#039; coding style) but given the Operation Aborted bug in <acronym title="Internet Explorer">IE</acronym> we reverted back to plain Event.observe from Prototype.<br />
<script type="text/javascript" src="http://sandbox.gonchuki.com/btt/mootools.js"></script><br />
<script type="text/javascript" src="http://sandbox.gonchuki.com/btt/back-to-top.js"></script></p>]]></content:encoded>
			<wfw:commentRss>http://blog.gonchuki.com/archives/usability-101-adding-a-global-back-to-top-to-your-site/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
