Creating a Rotating Billboard System with jQuery and CSS

Currently we are in the “hey, let’s do that flash thing in jQuery”-mood and so we came up with another idea: a rotating billboard system. In this tutorial we will […]


Currently we are in the “hey, let’s do that flash thing in jQuery”-mood and so we came up with another idea: a rotating billboard system.

In this tutorial we will use some images, CSS and jQuery to create the effect of a rotating billboard with two ads. The idea is to make one set of image slices disappear while another one (the other ad) appear. We will decrease the width of each disappearing slice and increase the width of each appearing slice. This will give the effect of rotating slices, just like in a rotating billboard system.

Ok, let’s start coding!

1. The HTML

We will have quite a lot of markup for the image slices – it will be 22 slices for each ad:

<div class="container">
	<div class="ad">
		<div id="ad_1" class="ad_1">
			<img class="slice_1" src="ads/ad1_slice01.jpg"/>
			<img class="slice_2" src="ads/ad1_slice02.jpg"/>
			<img class="slice_3" src="ads/ad1_slice03.jpg"/>
			<img class="slice_4" src="ads/ad1_slice04.jpg"/>
			<img class="slice_5" src="ads/ad1_slice05.jpg"/>
			<img class="slice_6" src="ads/ad1_slice06.jpg"/>
			<img class="slice_7" src="ads/ad1_slice07.jpg"/>
			<img class="slice_8" src="ads/ad1_slice08.jpg"/>
			<img class="slice_9" src="ads/ad1_slice09.jpg"/>
			<img class="slice_10" src="ads/ad1_slice10.jpg"/>
			<img class="slice_11" src="ads/ad1_slice11.jpg"/>
			<img class="slice_12" src="ads/ad1_slice12.jpg"/>
			<img class="slice_13" src="ads/ad1_slice13.jpg"/>
			<img class="slice_14" src="ads/ad1_slice14.jpg"/>
			<img class="slice_15" src="ads/ad1_slice15.jpg"/>
			<img class="slice_16" src="ads/ad1_slice16.jpg"/>
			<img class="slice_17" src="ads/ad1_slice17.jpg"/>
			<img class="slice_18" src="ads/ad1_slice18.jpg"/>
			<img class="slice_19" src="ads/ad1_slice19.jpg"/>
			<img class="slice_20" src="ads/ad1_slice20.jpg"/>
			<img class="slice_21" src="ads/ad1_slice21.jpg"/>
			<img class="slice_22" src="ads/ad1_slice22.jpg"/>
		<div id="ad_2" class="ad_2">
			<img class="slice_1" src="ads/ad2_slice01.jpg"/>
			<img class="slice_2" src="ads/ad2_slice02.jpg"/>
			<img class="slice_3" src="ads/ad2_slice03.jpg"/>
			<img class="slice_4" src="ads/ad2_slice04.jpg"/>
			<img class="slice_5" src="ads/ad2_slice05.jpg"/>
			<img class="slice_6" src="ads/ad2_slice06.jpg"/>
			<img class="slice_7" src="ads/ad2_slice07.jpg"/>
			<img class="slice_8" src="ads/ad2_slice08.jpg"/>
			<img class="slice_9" src="ads/ad2_slice09.jpg"/>
			<img class="slice_10" src="ads/ad2_slice10.jpg"/>
			<img class="slice_11" src="ads/ad2_slice11.jpg"/>
			<img class="slice_12" src="ads/ad2_slice12.jpg"/>
			<img class="slice_13" src="ads/ad2_slice13.jpg"/>
			<img class="slice_14" src="ads/ad2_slice14.jpg"/>
			<img class="slice_15" src="ads/ad2_slice15.jpg"/>
			<img class="slice_16" src="ads/ad2_slice16.jpg"/>
			<img class="slice_17" src="ads/ad2_slice17.jpg"/>
			<img class="slice_18" src="ads/ad2_slice18.jpg"/>
			<img class="slice_19" src="ads/ad2_slice19.jpg"/>
			<img class="slice_20" src="ads/ad2_slice20.jpg"/>
			<img class="slice_21" src="ads/ad2_slice21.jpg"/>
			<img class="slice_22" src="ads/ad2_slice22.jpg"/>

<div class="billboard"></div>

For these images, 22 slices (each slice 35 pixels wide) of a 770 pixel wide and 340 pixel high image were made. One might think that this is a lot of work and that we could actually take one whole picture and create divs acting like the slices with that background image and the right background position. But then we would not be able to create the same effect of a rotating slice (at least not with the JavaScript we created for this showcase).

The billboard will be an absolute positioned div with the billboard image. Since the image has some transparent spotlights, we want to lay it over the ad container.

2. The CSS

The style for the billboard frame will be the following:

    background:transparent url(../images/billboard.png) no-repeat 0px 0px;

To position the element in the center of the page, we set the left value to 50% and the left margin negatively to half of the width of the element.

The container for the ads will have the same style like the billboard, except the background-image. We do that, because we need to position the containing elements at the same place like the billboard. We don’t want to place the ads inside of the billboard because we need the billboard to be on top of them. So, we do this trick and create another element with the same positioning:

    margin:35px 0px 0px 60px;
.ad_1 img{
.ad_2 img{

The slices of the ad images will be 35 pixels wide. The slices of the second ad will have the same width, but initially we need to set it to 0. We also need to set the left margin of the second slices to 18 pixels since we want to create the rotating effect. I will explain more about this value after we define the CSS for the slices.

The single slices need to be positioned:


By positioning the elements one more pixel to the left than they are actually wide, we create a little gap between the them. Now, the left margin has a value of 18 pixels because it is half of a slice plus its gap. We set this because we want the slices to appear (or disappear) from (or to) their center and not just from the side. If we simply set the width of a slice to 0 pixels the image will not seem to be rotating.

And that is all the CSS. For the additional fancy background images you can check out the downloadable version below.

Let’s create the rotating effect with jQuery.

3. The JavaScript

We will now create a function for rotating the slices. The function will make the first ad slices disappear by making their width 0 pixels. To achieve the rotating effect, we add a left margin of 18 pixels.

While the first ad slices disappear we make the others appear by removing the left margin of 18 pixels (that we initially set in the CSS) and giving them a width of 35 pixels. We call the rotate function like this:

 $('#ad_1 > img').each(function(i,e){

The whole script that we call will look like this:

$(function() {            
    $('#ad_1 > img').each(function(i,e){
    function rotate(elem1,speed,timeout,i){
            var other;
            if(elem1.parent().attr('id') == 'ad_1')
                other = $('#ad_2').children('img').eq(i);
                other = $('#ad_1').children('img').eq(i);
                var f = function() { rotate(other,speed,timeout,i) };

So, the rotate function performs the hiding of the current element (that it was called upon) and then identifies which element it’s currently dealing with, so that another call of the rotate function can be performed on the other ad slices.
The two times mentioned in the rotate function stand for the speed of the rotation effect (speed) and the duration between the swapping of the ads (timeout) in milliseconds.

And that’s it!
I hope you like and enjoy it!

Message from TestkingWhether you want to get ccnp certification or interested in ccna certification, using ccna wireless prep resources you will pass real exam on first attempt.

Tagged with:

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

👾 Hey! Looking for the latest in frontend? Twice a week, we'll deliver the freshest frontend news, website inspo, cool code demos, videos and UI animations right to your inbox.

Zero fluff, all quality, to make your Mondays and Thursdays more creative!

Feedback 70

Comments are closed.
  1. i cant add 3. img..i added source code by img_2 and style css code by img_2 but i chanced from all 2 to 3…pls can you give litle info..thanx for all from now..

  2. Hi Mary Lou.
    Can you please give the JavaScript if I were to add two more images to the billboard.

    Thank You!!

  3. I put it online with the idea of selling adverts and getting people to tweet social it etc

    Just got to get a few things in place but overall this looks like a great little concept to have a play about with

    I used the name TwitTed as I have many Double T Ted’s

    They are Teddy Bear Characters and TwitTed will appear on your Billboard demo on son Folks

    So Look Back Soon Folks 🙂

    Nobody will notice that Loony Toons ref there will they?

  4. it would be nice if one could add a couple or more pix!(4 or 6 or 8)
    please supply the right working code
    to do this.thanx :))
    i altered the speed like this on my personal webpage intro:


    so the panels turn more in harmony even when the animation runs long time.

  5. i managed to have the effect working with seven (7) pictures (800 X 336) !
    to see it click on my HEXJUMPER name on this comment please.
    (attention the previous comment,dd.24 august 2011,with the HEXJUMPER name is an alternative website with alternative effect too!)

  6. hi

    it looks really impressive.
    probably the strange part part is lights on a billboard frame – mostly they are located on the top 😉

    otherwise – I love it 🙂

  7. hey guys its great but is there a way to add a url link to the pictures on billboard thats the whole point advertising but i cant seem to do it say u click on pic and it goes to url address …bla…a website ??????????
    anyone thanks !

  8. Is there a way of setting it up so that it has more than 2 slides, maybe 4 or 5…

    I would like to use it on the news page on my site…

  9. Ahhhh…. Just lovely. I’m a Jquery addict so I’ll be adding this to my collection. Many thanks!

  10. So if you need more than one two pictures in the rotator here it is the procedure:

    in style.css, replace the line “.ad_2 img{” with “.ad_2 img, .ad_3 img, .ad_4 img{” if you need you can go even more further… 😉

    in index.html replace script part with:

    $(function() {
    $(‘#ad_1 > img’).each(function(i,e){
    function rotate(elem1,speed,timeout,i){
    var other;
    if(elem1.parent().attr(‘id’) == ‘ad_1’)
    other = $(‘#ad_2’).children(‘img’).eq(i);
    else if(elem1.parent().attr(‘id’) == ‘ad_2’)
    other = $(‘#ad_3’).children(‘img’).eq(i);
    else if(elem1.parent().attr(‘id’) == ‘ad_3’)
    other = $(‘#ad_4’).children(‘img’).eq(i);
    other = $(‘#ad_1’).children(‘img’).eq(i);
    var f = function() { rotate(other,speed,timeout,i) };

    if you need you can add more else if loops, and just copy whole div with class ad_1 change his class and id to ad_3 and change img src, do the same thing for ad_4 😉

    so i hope i helped someone 🙂

    btw. posto bi reko da smo susjedi puno hvala manoela na izvrsnoj ideji 🙂

  11. It is unique and awesome.
    Some beautiful head you have which houses beautiful ideas.
    Keep up the good things.
    And, thanks for sharing.