<?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>nightdrops.com &#187; html</title>
	<atom:link href="http://www.nightdrops.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nightdrops.com</link>
	<description>like the color blue</description>
	<lastBuildDate>Thu, 01 Jul 2010 13:19:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Navigating through a long page: part 1</title>
		<link>http://www.nightdrops.com/2009/navigating-through-a-long-page-part-1/</link>
		<comments>http://www.nightdrops.com/2009/navigating-through-a-long-page-part-1/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 20:05:59 +0000</pubDate>
		<dc:creator>kajyr</dc:creator>
				<category><![CDATA[Blog posts]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.nightdrops.com/?p=143</guid>
		<description><![CDATA[Sometime i see around websites made just by one page. One really long page which contains all the information and offers some method to navigate up and down. There are many ways to accomplish this, flash or javascript based; i&#8217;m going to talk about the latter, because is kinda easy to learn, and permits me [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime i see around websites made just by one page. One really long page which contains all the information and offers some method to navigate up and down.</p>
<p>There are many ways to accomplish this, flash or javascript based; i&#8217;m going to talk about the latter, because is kinda easy to learn, and permits me to use non destructive javascript.</p>
<p>Non destructive javascript it is a tecnique to use javascript which don&#8217;t rely on javascript for modifying the basic content and structure of the page (so that can be viewed also fully with javascript disabled). What we do, for example, is to use js to hook the anchor links and slightly redirect their behaviour; if you turn off js they remain working anchor links.</p>
<p>I&#8217;m going to use jQuery, i really love this little library :-)</p>
<p>So let&#8217;s start planning!</p>
<h3>The planning phase</h3>
<p>What we have is a really really long html page, on which we need to move up and down. Let&#8217;s say a lot of paragraphs filled with lorem ipsum. We want to scroll up and down really smootly from one link to another, and we want a menu that follow us while moving on the page.</p>
<p>In this tutorial i&#8217;ll explain the scroll aspects, in the next i&#8217;ll talk about the moving menu.</p>
<h3>The basic html structure</h3>
<p>What we expect to have is a menu with voices that leads to some specific areas in the page. In the simplest form we can use html link that points to some anchors, in the form</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Ea.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#someId&quot;</span><a href="http://december.com/html/4/element/%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>text<span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Fa%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></a></span></div></div>
<p>that will bring you to somewhere in the page where is an emelent with that id.</p>
<p><a href="http://www.nightdrops.com/kj-includes/js-nav-tutorial/example0.html" target="_blank">This is an example of the basic html structure</a>.<br />
I&#8217;ve added some css rules to semplify what follow, but nothing special.</p>
<h3>Adding the fun</h3>
<p>But that page is boring, everybody wants a funny scroll!</p>
<p>So we introduce the jQuery library in the project. We include the jQuery javascript in the head tag, and at the bottom of the page we prepare a place for our script to go.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #3366CC;">&quot;js/jquery-1.3.1.js&quot;</span></div></div>
<p>Wait. At the bottom of the page? Yes. Just before the &lt;/body&gt; closing tab. Why? Because the <a href="http://developer.yahoo.com/performance/rules.html#js_bottom" target="_blank">Yahoo Exceptional Performance team said so</a>, to not block parallel downloads, and these are good practise to follow (also because IE6 sometimes tend to mess up with the $(document).ready() ).</p>
<p>Now googling around i foun that the html element to animate, to scroll the page, differs from firefox and explorer and safari. You&#8217;ll never guessed eh?<br />
We have to find which one is the right element. A rapid test consist to scroll down both &#8216;html&#8217; and &#8216;body&#8217; and then find out which one has moved. (Of course this goes in the $(document).ready() handler);<br />
Let&#8217;s see:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">'html, body'</span><span style="color: #3366CC;">'html, body'</span><span style="color: #3366CC;">'html'</span><span style="color: #3366CC;">'html'</span><span style="color: #3366CC;">'body'</span><span style="color: #3366CC;">'body'</span><span style="color: #3366CC;">'html, body'</span></div></div>
<p>So now we have to override the default link behaviour. But only in the case links are to id in the page. We can easily select them with the *= operator (that means &#8216;contains&#8217; ).<br />
We also need to know where to scroll:  for an &lt;a&gt; element, the &#8216;hash&#8217; property tells the references element; $(this.hash) is the jQuery wrap to that element in the page. We can easily find his y position by reading the offset() method.</p>
<p>The event.preventDefault() method inhibits the default link behaviour.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;a[href*=#]&quot;</span></div></div>
<p>Tadaaa! Every link to an id now scrolls smoothly. Notice that i&#8217;ve subtracted 200 pixel from the y position, because i don&#8217;t like the linked element on the top of the browser viewport (so i scroll 200pixel before).</p>
<p><a href="http://www.nightdrops.com/kj-includes/js-nav-tutorial/example1.html" target="_blank">This the example</a>. Soon the following part, where we will animate the menu!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nightdrops.com/2009/navigating-through-a-long-page-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
