{"id":584,"date":"2009-08-21T18:15:05","date_gmt":"2009-08-21T17:15:05","guid":{"rendered":"http:\/\/meier-online.com\/?p=584"},"modified":"2015-02-18T23:32:33","modified_gmt":"2015-02-18T22:32:33","slug":"rolling-billboard-jquery","status":"publish","type":"post","link":"https:\/\/meier-online.com\/en\/2009\/08\/rolling-billboard-jquery\/","title":{"rendered":"Rotating Billboard with jQuery"},"content":{"rendered":"<p><a href=\"\/develop\/jquery\/billboard.html\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1336 alignright\" title=\"To example page with a billboard with rotating images\" alt=\"Billboard with changing pictures\" src=\"http:\/\/meier-online.com\/blog\/uploads\/billboard-with-arrow-150.png\" width=\"150\" height=\"150\" \/><\/a>In 1998, I have presented &#8220;Meier&#8217;s billboard&#8221;, A virtual display panel with changing images. Technically it is a Java-applet. Unfortunately, the reputation of Java-applets in the browser was bad and there was not much interest in my Java-applets.<\/p>\n<p>In the meantime, the networks became more powerful, the computers became faster, and thanks to CSS, we got new design elements. One day I was dealing with the JavaScript framework &#8220;jQuery&#8221;. I came across the <code>animate()<\/code>-method, and I wondered: \u201cis it possible to realize the billboard only with JavaScript?\u201d<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-594\" alt=\"how it works: a list witt pictures is moved under a window\" src=\"http:\/\/meier-online.com\/blog\/uploads\/billboardschema.png\" width=\"360\" height=\"280\" srcset=\"https:\/\/meier-online.com\/blog\/uploads\/billboardschema.png 360w, https:\/\/meier-online.com\/blog\/uploads\/billboardschema-300x233.png 300w\" sizes=\"(max-width: 360px) 100vw, 360px\" \/><\/p>\n<p>The basic idea is simple: There is a frame and a list of images. The meaning of frame here is that of a picture-frame, not that of a HTML-frame.<\/p>\n<p>We manipulate the &#8220;top&#8221; coordinate of the list to create the motion. The clipping property is responsible to let us see only the inside of the frame.<\/p>\n<p>So the HTML code is just a div and a list of images:<\/p>\n<pre lang=\"html\">\r\n< div id=\"billboard\">\r\n\t< li>< a href=\"link1\">< img alt=\"\" src=\"img1\" \/>< \/a>< \/li>\r\n\t< li>< a href=\"link2\">< img alt=\"\" src=\"img2\" \/>< \/a>< \/li>\r\n\t< li>< a href=\"link3\">< img alt=\"\" src=\"img3\" \/>< \/a>< \/li>\r\n\t< li>< a href=\"link1\">< img alt=\"\" src=\"img1\" \/>< \/a>< \/li>\r\n< \/div>\r\n<\/pre>\n<p>In contrast to a solution with a Java applet (or Flash), we can use the full power of HTML. For example, the pictures can have links and search engine friendly title attributes.<\/p>\n<p>The images must all have the same size, otherwise it will not look good. We use CSS to position the images inside our billboard-frame.<\/p>\n<pre lang=\"css\">#billboard{\r\n  width: 260px;\r\n  height: 264px;\r\n  background-image: url(Rahmen.jpg);\r\n  position: relative;\r\n  padding: 0px;\r\n}\r\n#billboard ul{\r\n  list-style: none;\r\n  padding: 0px;\r\n  margin: 0px;\r\n  position: absolute;\r\n  top: 30px;\r\n  left: 29px;\r\n  clip: rect(0px 200px 200px 0px);\r\n}<\/pre>\n<h3>Move<\/h3>\n<p>Moving the images is very simple due to jQuery:<\/p>\n<pre lang=\"javascript\">function simple_move_up(){ \r\n  $('#billboard ul li').animate({top: '-200px'}, 3000); \r\n}<\/pre>\n<p>It is somewhat more complicated to show the last picture and then seamlessly move to the first picture. But there is a trick: we just repeat the first picture after the last picture in the HTML list. When this new \u201clast\u201d picture is reached, we can just jump to the first image and nobody will notice the jump, because both pictures are the same.<\/p>\n<pre lang=\"javascript\" escaped=\"true\">\r\nvar picth = 200; \/* height of a single picture *\/ \r\nvar nr_picts = 3; \r\nvar curpos = 0; \r\nfunction move_up(ydiff){ \r\n  curpos = curpos - picth; \r\n  if (curpos < (- nr_picts * picth){ \r\n    curpos = 0; \r\n    $('#billboard ul li').css({top: curpos}); \r\n    curpos = curpos - picth; \r\n  }\r\n  $('#billboard ul li').animate({top: curpos}, 3000); \r\n}<\/pre>\n<h3>Loop<\/h3>\n<p>What is missing is to start and loop the movement. We need to call our <code>move_up<\/code> method in regular intervals.<\/p>\n<p>For this we define a (anonymous) function, which JQuery calls after the document construction is finished. In this function we call the <code>setInterval()<\/code> function from the JavaScript standard.<\/p>\n<p>We need to be careful with the timing parameter: The parameter does not describe the time between the animations, instead it describes the total time of one cycle. In the example this is 8 seconds or 8000 milliseconds, ie. 3 seconds for the animation, and 5 seconds pause.<\/p>\n<pre lang=\"javascript\">$(document).ready(function(){ \r\n    setInterval(\"move_up(-200)\", 8000); \r\n})<\/pre>\n<p>And this was all, the billboard works. We needed surprisingly few lines of code thanks jQuery .<\/p>\n<p>Watch the demo page with the <a title=\"Rotating Billboard demo page\" href=\"http:\/\/meier-online.com\/develop\/jquery\/billboard.html\">rotating billboard<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Sorry, this entry is only available in Deutsch.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[150,148,152],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-develop","tag-javascript","tag-jquery","tag-reklametafel","entry"],"_links":{"self":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/584"}],"collection":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":30,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":2034,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/584\/revisions\/2034"}],"wp:attachment":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}