Archive for category JavaScript
Create Simple Carousel using jQuery
Posted by admin in CSS, HTML, JavaScript, jQuery on February 15th, 2010
You can see the working demo here.
Carousel is a way to present content in the form of slideshow on web page. Here I’m showing how easily you can create a horizontal carousel using jQuery. You can play with the given code and easily create vertical carousel, circular carousel, auto-scrolling carousel etc.
I’m using jQuery for animation. So, you need jQuery JavaScript library for it. You can download latest jQuery JavaScript library from jquery.com.
So, how it work.
Your basic structure of HTML for Carousel should be like this:-
<div id="my_carousel">
<ul>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
.....
</ul>
</div>What you need to do before applying “my_carousel” ID to your Carousel DIV.
1. Put any HTML content which you want to display in your LI’s.
2. Create CSS for your Carousel DIV.
3. You also need two images, or button to scroll your content in left and right direction.
4. Assign “btnprev” class to your left button or image.
5. Assign “btnnext” class to your right button or image.
6. Assign “my_carousel” ID to you Carousel DIV.
How JavaScript Code work:-
So, when browser load the page JavaScript look for “my_carousel” ID in HTML and apply all the require attributes and events to your DIV, UL, and LI.
Important JavaScript variables and their purpose:-
step – How many LI’s you want to scroll on click of left and right button.
current – Index or Number of left most visible LI in carousel.
maximum – Total number of LI in carousel.
visible – Number of visible LI’s.
speed – Animation speed.
liSize – Width of one LI in pixels.
liSize – Width of one LI in pixels.
carousel_height – Carousel DIV height.
ulSize – Width of UL. You can calculate it like this liSize * maximum.
divSize – Width of CArousel DIV. You can calculate it like this liSize * visible.
Assign require attributes to DIV, UL, and LI.
$('#my_carousel').css("width", divSize+"px").css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative"); $('#my_carousel ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute");
Here we changed the width, height, visibility, overflow, and position of Carousel DIV. The Carousel DIV overflow = hidden property is very important here, through it we are just showing the require(visible) LI’s which we want to show at a time. We just hide the remaining LI’s.
We also changes the width, left and position of UL.
Assign Click event to left and right buttons/images
Left button/image
$('.btnprev').click(function() { if(current - step < 0 || current - step > maximum - visible) {return; } else { current = current - step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; });
So, we have assigned a click event on previous button. So, we are first checking the index of current LI. If the current LI is the first LI we will not do anything. We will simply return from the function. If the current LI is not the first LI then scroll the content of UL to left hand side by using the jQuery “animate” function. For more information about animate function click here. And we also changed the current to current left most most visible LI.
Right button/image
$('.btnnext').click(function() { if(current + step < 0 || current + step > maximum - visible) {return; } else { current = current + step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; });
Similarly we assigned the clicked event on next button and followed the same logic what we used for previous click. We just changed it slightly to check last LI when we click on next button. If the current is last LI we will do nothing.
Complete JavaScript Code:-
<script language="javascript"> $(function() { var step = 2; var current = 0; var maximum = $('#my_carousel ul li').size(); var visible = 2; var speed = 200; var liSize = 331; var carousel_height = 161; var ulSize = liSize * maximum; var divSize = liSize * visible; $('#my_carousel ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute"); $('#my_carousel').css("width", divSize+"px").css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative"); $('.btnnext').click(function() { if(current + step < 0 || current + step > maximum - visible) {return; } else { current = current + step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); $('.btnprev').click(function() { if(current - step < 0 || current - step > maximum - visible) {return; } else { current = current - step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); }); </script>
Round Number upto 2 decimal places in JavaScript
Posted by admin in JavaScript on January 17th, 2010
JavaScript’s Math.round() function doesn’t provide facility to round off a number up to 2 or more decimal places. So, here I’m showing a simple JavaScript code using which you can round off numbers up to 2 decimal places. You can change this for more decimal places.
var retailPrice = 9.99; var percentage = 0.2525; /* 25.25% */ var decimalPlace = 100; /* 100 for 2 decimal place. 1000 for 3 decimal place.*/ var wholeSalePrice = Math.round(percentage * retailPrice * decimalPlace) / decimalPlace; alert(wholeSalePrice); /* This will show 2.52 */
Initialize LightBox using JSON string and Run it from Flash
Posted by admin in Flash, JavaScript, Prototype on August 25th, 2009
Lightbox is the most popular way of showing large image on click on thumbnail over the web. Lot of websites are using the LightBox. Normally, We use lightbox using the “rel” attribute of anchor tag. We use lightbox this way:-
<a href="images/image-1.jpg" rel="lightbox" title="thumb"><img src="images/thumb-1.jpg" width="100" height="40" alt="" /></a>Here, we provided the “rel” attribute as “lightbox” in hyperlink. What lightbox script do is when page load completed its check all the hyperlinks of page and if rel=”lightbox” is exists them it push this link into an array.
Lightbox extract the “href” and “title” attributes of each link having rel=”lightbox” and push all of them into array. “href” is normally the URL of larger image. Lingtbox also add “click” event on all of these hyperlinks and when user click on any particular thumbnail the lightbox starts.
Here, I’m showing a way in which you can initialize the lightbox by using the JSON string. Basically, this JSON string contains the “href” and “title” information which we want to show. We can create this string dynamically using any server side scripting language. Here is one of sample JSON string.
<script language="javascript"> var img_json = [['pics/photo-80.jpg', 'Title 01'],['pics/photo-128.jpg', 'Title 02'],['pics/photo-160.jpg', 'Title 03'],['pics/photo-165.jpg', 'Title 04'],['pics/photo-205.jpg', 'Title 05'],['pics/photo-206.jpg', 'Title 06']]; </script>
You can learn more about JSON here.
We also need to do some small changes in lightbox javascript file so it use our JSON string for image array instead of create array by checking the “rel” attribute in whole page.
You can download the customized lightbox javascript file from here.
You can see the working demo here.
1. Remove the “imageLink” parameter from start function because now we not starting LightBox by not clicking any thumbnail.
start: function(imageLink)
function to
start: function()
2. Now we are initializing the “imageArray” using JSON string.
this.imageArray = [];
to
this.imageArray = img_json;
3. Comment these line of code inside start function bacause we not want to check whole page links for lightbox.
/*if ((imageLink.rel == 'lightbox')){
// if image is NOT part of a set, add single image to imageArray
this.imageArray.push([imageLink.href, imageLink.title]);
} else {
// if image is part of a set..
this.imageArray =
$$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
collect(function(anchor){ return [anchor.href, anchor.title]; }).
uniq();
while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
}*/4. Add function startLightBox() at the end of Lightbox class. This function start the lightbox when we click on the link having id=”start”.
startLightBox: function() { var th = this; Event.observe($('start'), 'click', function () { th.start(); }); }
5. Add this line in initialize() function to call “startLightBox()” function which create the click event on link having id=”start”.
this.startLightBox();
After completing all the five changes in lightbox.js you need to create a link with id = “start” in your HTML file. When you click on this link its starts the LightBox. Here is the code for HTML file.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Initialize LightBox using JSON string and Run it from Flash</title> <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" /> <script src="js/prototype.js" type="text/javascript"></script> <script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script> <script src="js/lightbox.js" type="text/javascript"></script> </head> <body> <a href="#" id="start">Start LightBox</a> </body> <script language="javascript"> var img_json = [['pics/photo-80.jpg', 'Title 01'],['pics/photo-128.jpg', 'Title 02'],['pics/photo-160.jpg', 'Title 03'],['pics/photo-165.jpg', 'Title 04'],['pics/photo-205.jpg', 'Title 05'],['pics/photo-206.jpg', 'Title 06']]; </script> </html>

