<?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>meier-online &#187; JavaScript</title>
	<atom:link href="http://meier-online.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://meier-online.com</link>
	<description>Der Blog von Karsten Meier</description>
	<lastBuildDate>Sun, 09 Oct 2011 13:27:57 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Rotierende Reklametafel mit jQuery</title>
		<link>http://meier-online.com/2009/08/rolling-billboard-jquery/</link>
		<comments>http://meier-online.com/2009/08/rolling-billboard-jquery/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 17:15:05 +0000</pubDate>
		<dc:creator>meier</dc:creator>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Reklametafel]]></category>

		<guid isPermaLink="false">http://meier-online.com/?p=584</guid>
		<description><![CDATA[Im Jahre 1998 habe ich &#8220;Meiers Billboard&#8221; vorgestellt: Eine virtuelle Anzeigentafel, die Bilder durch Rollen austauscht. Technisch ist es ein Java Applet. Der Ruf von Java im Browser wurde immer schlechter, auch mein Applet war vor allem wegen des Bild des Rahmen verlinkt. In der Zwischenzeit ist sind die Netze leistungsfähiger, die Rechner schneller geworden [...]]]></description>
			<content:encoded><![CDATA[<p>Im Jahre 1998 habe ich &#8220;<a title="Die Vorstellung des Applets von 1998" href="/develop/java/billboardlight/index.html">Meiers Billboard</a>&#8221; vorgestellt: Eine virtuelle Anzeigentafel, die Bilder durch Rollen austauscht. Technisch ist es ein Java Applet. Der Ruf von Java im Browser wurde immer schlechter, auch mein Applet war vor allem wegen des Bild des Rahmen verlinkt.</p>
<p>In der Zwischenzeit ist sind die Netze leistungsfähiger, die Rechner schneller geworden und dank CSS gibt es neue Gestaltungsmittel. Bei der Beschäftigung mit dem JavaScript-Framework &#8220;<a title="Zur offiziellen Seite des jQuery Projektes" href="http://www.jquery.com" target="_blank">jQuery</a>&#8220;  stieß ich auf die animate()-Methode und ich fragte mich: lässt sich das Billboard nicht auch mit JavaScript realisieren?<span id="more-584"></span></p>
<p><img class="alignnone size-full wp-image-594" title="Funktionsweise: Eine Liste von Bildern wird unter einem Fenster hindurchgeschoben." src="http://meier-online.com/blog/uploads/billboardschema.png" alt="Funktionsweise: Eine Liste von Bildern wird unter einem Fenster hindurchgeschoben." width="360" height="280" /></p>
<p>Die Grundidee ist einfach: Es gibt einen Rahmen und eine Liste von Bildern. Wir manipulieren die &#8220;top&#8221;-Koordinate der Liste, um die Bewegung zu erzeugen. Die Clipping-Eigenschaft bewirkt, dass wir nur das Innere des Rahmens sehen.</p>
<p>Der HTML-Code besteht somit im wesentlichen aus einer Liste:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt; div id=&quot;billboard&quot;&gt;
&lt;ul&gt;
 &lt;li&gt;&lt;a href=&quot;link1&quot;&gt;&lt;img src=&quot;img1&quot; ... &gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href=&quot;link2&quot;&gt;&lt;img src=&quot;img2&quot; ... &gt;&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href=&quot;link3&quot;&gt;&lt;img src=&quot;img3&quot; ... &gt;&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href=&quot;link1&quot;&gt;&lt;img src=&quot;img1&quot; ...&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<p>Anders als bei einer Lösung mit Java-Applet (oder auch Flash) haben wir alle Auszeichnungsmöglichkeiten von HTML zur Verfügung. Die Bilder und Links können wir beispielsweise suchmaschinenfreundlich mit title-Attributen versehen. Die Bilder müssen alle die gleiche Größe haben und werden mit Hilfe von css in das Bild eines Rahmens positioniert:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  <span style="color: #cc00cc;">#billboard</span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">260px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span>  <span style="color: #933;">264px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">Rahmen.jpg</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#billboard</span> ul<span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">29px</span><span style="color: #00AA00;">;</span>	
     <span style="color: #000000; font-weight: bold;">clip</span><span style="color: #00AA00;">:</span> rect<span style="color: #00AA00;">&#40;</span><span style="color: #933;">0px</span>  <span style="color: #933;">200px</span> <span style="color: #933;">200px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>	
  <span style="color: #00AA00;">&#125;</span>
    ...</pre></div></div>

<h3>Verschieben</h3>
<p>Eine Methode zum Verschieben ist dank jQuery sehr einfach:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> simple_move_up<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#billboard ul li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> <span style="color: #3366CC;">'-200px'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="margin-bottom: 0cm;">Etwas komplizierter wird es dadurch, dass wir nach dem letzten Bild nahtlos wieder das erste wieder zeigen wollen. Hier gibt es einen Trick: Für den nahtlosen Übergang wiederholen wir in der HTML-Liste nach den letzten Bild noch einmal das erste. Wenn nun das neue letzte Bild erreicht ist, können wir zum ersten Bild springen, ohne das es bemerkt wird.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> picth <span style="color: #339933;">=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span>		<span style="color: #009966; font-style: italic;">/* height of a single picture */</span>
<span style="color: #003366; font-weight: bold;">var</span> nr_picts <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> curpos <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> move_up<span style="color: #009900;">&#40;</span>ydiff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	curpos <span style="color: #339933;">=</span> curpos <span style="color: #339933;">-</span> picth<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>curpos <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span> nr_picts <span style="color: #339933;">*</span> picth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		curpos <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#billboard ul li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> curpos<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		curpos <span style="color: #339933;">=</span> curpos <span style="color: #339933;">-</span> picth<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#billboard ul li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> curpos<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Endlosschleife</h3>
<p>Jetzt bleibt nur noch, unsere move_up Methode in regelmäßigen Abständen aufzurufen.</p>
<p>Dazu setzen wir eine (anonyme) Funktion, die JQuery bei abgeschlossenen Aufbau des Dokumentes aufruft. In dieser rufen wir die Funktion setInterval() aus dem JavaScript-Standard auf.</p>
<p>Bei dem Timing-Parameter müssen wir aufpassen: Der Parameter beschreibt nicht die Zeit zwischen den Animationen, sondern die Gesamtzeit eines Zyklus. Im Beispiel sind dies 8 Sekunden bzw. 8000 Millisekunden, nämlich 3 Sekunden für die Animation und 5 Sekunden Pause.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
	setInterval<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;move_up(-200)&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Damit funktioniert das Billboard dank jQuery mit erstaunlich wenigen Codezeilen.</p>
<p>Zur Demoseite mit der <a href="/develop/jquery/billboard.html" target="_blank">rotierenden Reklametafel</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://meier-online.com/2009/08/rolling-billboard-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

