Skip to main content

Image Carousel with jQuery

jQuery is a wonderful cross browser javascript library. The effects that can be created using it are simply superb and uber cool. And that without using flash. The two major advantages of not using flash is that you can save on bandwidth as embedded flash objects are not bandwidth friendly. Besides flash is not supported on iPads, meaning which on an iPad the webpage will be a complete piece of junk and nothing else with missing components.
The polaroid effect is not covered in this article. Some other time!

Now there are many jQuery based carousels on the internet. So why need a new one? well, actually you don't. But for example you wish to integrate a carousel in Blogger. The choices become severely restricted and then the customization is often difficult to configure. Most of the carousels available for blogger are simple ones with image transitions and basically nothing more. So I present to you a new form of jQuery carousel which is optimized for blogger.

Before you embark on the journey you can the check out the Demo.
Demo
What will you need?

  • A CSS file for declaring the style of the carousel
  • A javascript file to code its behavior
  • Pictures to be shown
First let's invite the javascript. Since blogger does not allow hosting javascripts, you need to host it yourself.
For this to work you need two javascripts. One is the jQuery library. get the latest one from here.

http://code.jquery.com/jquery-1.6.4.min.js

Embed it before </head> in your template.

<script src='http://code.jquery.com/jquery-latest.js' type='text/javascript'/>

The other one is the one we need to create to make the carousel work.
So here it goes,

 <script type='text/javascript'>  
 $(document).ready(function() {  
   //Set Default State of each portfolio piece  
   $( & quot;.paging & quot;).show();  
   $( & quot;.paging a: first & quot;).addClass( & quot; active & quot;);  
   //Get size of images, how many there are, then determin the size of the image reel.  
   var imageWidth = $( & quot;.window & quot;).width();  
   var imageSum = $( & quot;.image_reel img & quot;).size();  
   var imageReelWidth = imageWidth * imageSum;  
   //Adjust the image reel to its new size  
   $( & quot;.image_reel & quot;).css({ & #39;width&# 39;: imageReelWidth  
   });  
   //Paging + Slider Function  
   rotate = function() {  
     var triggerID = $active.attr( & quot; rel & quot;) - 1; //Get number of times to slide  
     var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide  
     $( & quot;.paging a & quot;).removeClass( & #39;active&# 39;); //Remove all active class  
     $active.addClass( & #39;active&# 39;); //Add active class (the $active is declared in the rotateSwitch function)  
     //Slider Animation  
     $( & quot;.image_reel & quot;).animate({  
       left: -image_reelPosition  
     }, 500);  
   };  
   //Rotation + Timing Event  
   rotateSwitch = function() {  
     play = setInterval(function() { //Set timer - this will repeat itself every 3 seconds  
       $active = $( & #39;.paging a.active&# 39;).next();  
       if ($active.length === 0) { //If paging reaches the end...  
         $active = $( & #39;.paging a:first&# 39;); //go back to first  
       }  
       rotate(); //Trigger the paging and slider function  
     }, 7000); //Timer speed in milliseconds (3 seconds)  
   };  
   rotateSwitch(); //Run function on launch  
   //On Hover  
   $( & quot;.image_reel a & quot;).hover(function() {  
     clearInterval(play); //Stop the rotation  
   }, function() {  
     rotateSwitch(); //Resume rotation  
   });  
   //On Click  
   $( & quot;.paging a & quot;).click(function() {  
     $active = $(this); //Activate the clicked paging  
     //Reset Timer  
     clearInterval(play); //Stop the rotation  
     rotate(); //Trigger rotation immediately  
     rotateSwitch(); // Resume rotation  
     return false; //Prevent browser jump to link anchor  
   });  
 });  
 </script>  


You can embed it in the template in the following ways :


  • Copy Paste Method : Copy paste the entire code before </head>  (easy method)
  • Src Method : Create a javascript file and call it using the src attribute. (Recommended) Downside is that quick editing will be difficult but pages will load much faster as the browser caches those elements. My suggestion is embed the script first (Copy paste method) and once it works perfectly, delete it from there, create a js file and upload it somewhere. then put this code before </head> in your template.
    <script src='link of file.js' type='text/javascript'/>
  • Following the first method(the copy paste method) the screenshot should look somewhat like this,


    and the rest of the code should follow(I have shown only a snippet to show exactly where the lines should be.)
Now let's see the CSS file
Here also you can either do the copy paste method or the src one. Blogger allows inserting CSS from Template > Advanced > Add CSS, so I will do that for now. Once all errors are removed, I can create the src line and remove embedded code.
 /*--Main Container--*/  
 .main_view {  
 float: left;  
 position: relative;  
 }  
 /*--Window/Masking Styles--*/  
 .window {  
 height:300px; width: 900px;  
 overflow: hidden; /*--Hides anything outside of the set width/height--*/  
 position: relative;  
 }  
 .image_reel {  
 position: absolute;  
 top: 0; left: 0;  
 }  
 .image_reel img {float: left;}  
 /*--Paging Styles--*/  
 .paging {  
 position: absolute;  
 bottom: -5px; right: 0px;  
 width: 900px; height: 0px;  
 z-index: 100; /*--Assures the paging stays on the top layer--*/  
 text-align: center;  
 line-height: 0px;  
 display: none; /*--Hidden by default, will be later shown with jQuery--*/  
 }  
 .paging a {  
 padding: 5px;  
 text-decoration: none;  
 color: #transparent;  
 }  
 .paging a.active {  
 font-weight: bold;  
 -moz-border-radius: 10px;  
 -khtml-border-radius: 10px;  
 -webkit-border-radius: 10px;  
 }  
 .paging a:hover {font-weight: bold;}  

So that's the CSS file.

I will now explain the workings. If you want you can skip this paragraph.
main container - outer container containing the images as well as the slider buttons below
window - container for the img reel(which actually contains the images), the height and the width should be the one you want. Change them to suit your style.
img_reel - this has got the images, changing values here is funny, try them out :D
image_reel sets the image attributes, you may wish it to float in the direction of your choice.
paging - choose the paging styles here, height and width are all self explanatory, a high z-index ensures that the element remains on top of other elements as no element should have a higher index than 100.
paging a, active, hover - gives the hyperlink styles, if you have text in the HTML in the sliders(I don't) they will be rendered accordingly.

Now comes the HTML file itself.
You can 
  • either create a Post and embed it in the HTML mode OR
  • create a custom HTML/Javascript gadget and insert the code there. I chose this.
 <div class="container">  
 <div class="folio_block">  
 <div class="main_view">  
 <div class="window">  
 <div class="image_reel">  
 <a class="img" href="http://i.imgur.com/MsDjc.jpg" ><img alt="" src="http://i.imgur.com/MsDjc.jpg" /></a>  
 <a class="img" href="http://i.imgur.com/Cn548.jpg" ><img alt="" src="http://i.imgur.com/Cn548.jpg" /></a>  
 <a class="img" href="http://i.imgur.com/GChA9.jpg" ><img alt="" src="http://i.imgur.com/GChA9.jpg" /></a>  
 <a class="img" href="http://i.imgur.com/mmojx.jpg" ><img alt="" src="http://i.imgur.com/mmojx.jpg" /></a>  
 <a class="img" href="http://i.imgur.com/u8nwS.jpg" ><img alt="" src="http://i.imgur.com/u8nwS.jpg" /></a>  
 <a class="img" href="http://i.imgur.com/67L84.jpg" ><img alt="" src="http://i.imgur.com/67L84.jpg" /></a>  
 </div>  
 </div>  
 </div>  
 <div class="paging">  
 <a href="#" rel="1"><img src="http://i.imgur.com/8Y9jH.png"/></a>  
 <a href="#" rel="2"><img src="http://i.imgur.com/8Y9jH.png"/></a>  
 <a href="#" rel="3"><img src="http://i.imgur.com/8Y9jH.png"/></a>  
 <a href="#" rel="4"><img src="http://i.imgur.com/8Y9jH.png"/></a>  
 <a href="#" rel="5"><img src="http://i.imgur.com/8Y9jH.png"/></a>  
 </div>  
 </div>  
 </div>  

As you can see, the code here is very simple.
The first series of images are the actual image files with the small image as the link and the large full size image as the src.
The second one is the image for a bullet, you can change the image and even put text just before the closing anchor tag that just before </a>

For example
<a href="#" rel="5"><img src="http://i.imgur.com/8Y9jH.png"/>Blah Blah</a>


Save it and you are done. Most probably your carousel will work in a somewhat ugly way. You need to adjust the height and width of the elements to make it align correctly. As for the Polaroid style effect that you see in the demo, that's another story and I will cover it some other day. As for now that's how you make a simple image carousel using jQuery in Blogger.

Comments

Popular posts from this blog

Operational Situational Summary - Ukraine 2 March

From a glance at the map and the news reports, it appears that the Russian advance has been very slow or even checked. However, a deeper look shows the situation has worsened significantly for Ukraine. That this happened as per my expectation in the past few posts makes me feel worse. In the North, the Russian forces have already encircled Chernihiv in the past couple of days. This is not yet fully updated on this map. The two strong armored spearheads from Konotop (which had also fallen) are currently holding in Pryluky and Nizhyn. The Chernihiv garrison probably has a day or two before the Russians reach Kozelets. Then any breakout attempt to Kyiv will become extremely difficult and will likely result in very heavy losses. It is to be expected that this city too, will fall soon without affecting the battle of Kyiv. This is bad for Ukraine. Now that the International Community has rallied for Ukraine, the previous fears of Russia dealing with breakaway republics in the East should be ...

Saraswati Puja and Valentine's Day Coincide

I used to hear this a lot - for Bengalis (Bengali Hindus), Saraswati Pujo is equivalent to the Valentine's Day . Girls would wear yellow sarees and braid their hair, while guys will be wearing yellow punjabees and white dhoti or pajama.  This time on Feb 14, 2024, they fell on the same day. Woke up pretty early in the morning, showered, wore the dhuti and uttiyo and did Saraswati Puja. Most of the mantras I have got by heart now. After the pushpanjali , I was done and we (my mother and I) could then break our fast with the Mahaprasad . Took a day off for the day, but still joined for a quick catchup call.  This is how she looks like. There is a bit of a history here. Usually, Hindus get the vigraha from the shop every year, worship and then do visarjan (immersion) in a river or pond. However, she is with us since I was in class IX. When I was in Standard X, my grandmother had died. As part of the souch (relatively inauspicious greiving period), we could not get a new vigra...

Growing up

Time affects everything and everyone We all grow up. After college, friends go places, to different states and far away countries. You get less and less time. Some go for higher studies, again, in other states. Parents get older. Then friends start getting married. First it’s usually the girls. One by one, your crushes tie the knot. You dismiss it, you are still young. Then your guy friends start getting married too. Your circle shrinks. In the end you look around. All you are left with are ideological nutjobs, immature imbeciles, idealistic crazies, bohemian hippies, or other misfits. Do you really belong here? Okay, that was too harsh. But you think. You say to yourself, ‘ dil to baccha hai jee ’.  Okay, fair enough. And then... Then, you see a growing pot belly. Something you always despised. Your dad had one (he still does). After taking a shower, you look at the mirror. You are losing hair. You search on Amazon for hair growing products - it is too much of a hassle. Hair trans...