<?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>Sentido Web &#187; indice</title>
	<atom:link href="http://sentidoweb.com/tag/indice/feed" rel="self" type="application/rss+xml" />
	<link>http://sentidoweb.com</link>
	<description>Desarrollo web, HTML, CSS, Javascript, PHP, MySQL</description>
	<lastBuildDate>Tue, 10 Apr 2012 01:02:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Laboratorio: índice lateral con jQuery</title>
		<link>http://sentidoweb.com/2009/07/30/laboratorio-indice-lateral-con-jquery.php</link>
		<comments>http://sentidoweb.com/2009/07/30/laboratorio-indice-lateral-con-jquery.php#comments</comments>
		<pubDate>Fri, 31 Jul 2009 02:00:00 +0000</pubDate>
		<dc:creator>displaynone</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[indice]]></category>
		<category><![CDATA[indice lateral]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://sentidoweb.com/2009/07/30/laboratorio-indice-lateral-con-jquery.php</guid>
		<description><![CDATA[Algo que me gusta bastante de algunos editores tipo Netbeans es que cuando hay algún error en una línea aparece un enlace en el lateral en la posición relativa de la línea respecto al alto del editor. Algo parecido se me ha ocurrido hacer con jQuery, obtener los elementos cabecera (H1..H6) y crear un índice, [...]]]></description>
			<content:encoded><![CDATA[<p>Algo que me gusta bastante de algunos editores tipo Netbeans es que cuando hay algún error en una línea aparece un enlace en el lateral en la posición relativa de la línea respecto al alto del editor.</p>
<p>Algo parecido se me ha ocurrido hacer con jQuery, obtener los elementos cabecera (H1..H6) y crear un índice, y mediante estilos y jQuery repartirlos por el lateral de la ventana.</p>
<p><img alt="indice_lateral.png" src="http://sentidoweb.com/img/2009/07/indice_lateral.png" width="500" height="212" class="center" /></p>
<p>El código es sencillito, recupero los elementos h1..h6, calculo su posición X y luego ordeno el array por esta posición, creo una lista ordenada y anidada con OLs y LIs y con CSS y Javascript coloco los elementos donde corresponde.</p>
<pre><code><pre class="javascript">		  $<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> ids = <span style="color: #CC0000;">0</span>;
<span style="color: #003366; font-weight: bold;">var</span> haches = <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">1</span>; i&amp;lt;<span style="color: #CC0000;">7</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#content h'</span>+i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">id</span> = <span style="color: #3366CC;">'haches_'</span>+ids++;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #3366CC;">'none'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
haches.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">offset</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">top</span>, $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">id</span>, i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
haches.<span style="color: #006600;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a,b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>&amp;gt;b<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> ant = <span style="color: #CC0000;">1</span>;
<span style="color: #003366; font-weight: bold;">var</span> html = <span style="color: #3366CC;">'&amp;lt;ol class=&quot;indice&quot;&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i&amp;lt;haches.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span> &amp;gt; ant<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
html += <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>&amp;lt;ol&amp;gt;'</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span> &amp;lt; ant<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
html += <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>&amp;lt;/ol&amp;gt;'</span>;
<span style="color: #66cc66;">&#125;</span>
html += <span style="color: #3366CC;">'&amp;lt;li id=&quot;haches'</span>+i+<span style="color: #3366CC;">'&quot; class=&quot;indice'</span>+haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #3366CC;">'&quot;&amp;gt;&amp;lt;span&amp;gt;&amp;lt;a href=&quot;#'</span>+haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #3366CC;">'&quot;&amp;gt;'</span>+haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #3366CC;">'&amp;lt;/a&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span>;
ant = haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span>
html += <span style="color: #3366CC;">'&amp;lt;/ol&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span>;
$<span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">prepend</span><span style="color: #66cc66;">&#40;</span>html<span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#indice'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'position: absolute; top: 0px'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> alto = $<span style="color: #66cc66;">&#40;</span>window<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">height</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> max = $<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">height</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i&amp;lt;haches.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>parseInt<span style="color: #66cc66;">&#40;</span>alto*haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>/max<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#haches'</span>+i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'top'</span>, parseInt<span style="color: #66cc66;">&#40;</span>alto*haches<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>/max<span style="color: #66cc66;">&#41;</span>+<span style="color: #3366CC;">"px"</span><span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#haches'</span>+i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'left'</span>, <span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span>window<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #CC0000;">50</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #3366CC;">"px"</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></code></pre>
<p>Ni que decir tiene que le faltan cosas por hacer, como por ejemplo moverlo según se mueve el scroll, pero para hacerlo en un rato no está tampoco muy mal. Los distintos tipos de enlaces a cabecera tienen sus estilos propios (feos pero propios) y cuando te pones sobre uno de ellos aparece el título enlazando al elemento en cuestión (esto también habría que refinarlo).</p>
<p><a href="http://sentidoweb.com/lab/javascript/indice_lateral/wikipedia.html">Ejemplo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sentidoweb.com/2009/07/30/laboratorio-indice-lateral-con-jquery.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

