Archive for category jQuery
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>
Getting Browser Information using JQuery
Posted by vivek in JavaScript, jQuery on June 23rd, 2009
One day every browser will act in same way and support same web standards. However, that’s not today. In a sufficiently complicated web application, it’s important to know which browser user is using so we can know which JavaScript functions are available and which CSS properties it supports. Here, I am showing how you know about the users browser using JQuery. We can find lot of information about users browser like its name, version number etc through jQuery.
At the highest level, JQuery offers an object called browser that contains some simple flags to determine which one of the major browsers is currently being used – Safari, Opera, IE, or Mozilla.
$(document).ready(function(){ var browser; if($.browser.mozilla) browser = "Firefox"; else if($.msie) browser = "Internet Explorer"; else if($.browser.opera) browser = "Opera"; else if($.browser.safari) browser = "Safari"; else browser = "Unknown"; $('#browserName').append(browser); });
Create a DIV so in your page so you can see the output of this JavaScript.
<div id="browserName">Your Browser: </div>
When you execute the code, the output should look something like below. If you are using Safari.
Your Browser: Safari
How use AJAX in DRUPAL

How use AJAX in DRUPAL
We can easily use AJAX in DRUPAL framework. Drupal provide the jQuery javascript library so we can use jQuery for our AJAX implementation. First we write a module in which we are going to implement the server side logic. Suppose our module name is product and we will check the given product name is exist or not in our product table.
<?php /*product.module*/ function product_menu() { $items = array(); $items['product'] = array( 'page callback' => 'drupal_get_form', 'page arguments' => array('product'), 'access arguments' => TRUE, 'type' => MENU_CALLBACK, ); $items['product/check_name'] = array( 'page callback' => 'check_name', 'access arguments' => TRUE, 'type' => MENU_CALLBACK, ); return $items; } function product() { $path = drupal_get_path('module', 'product'); drupal_add_js($path . '/product.js', 'module'); $form['product_name'] = array( '#title' => t('Product Name'), '#type' => 'textfield', '#required' => TRUE, '#size' => 30, '#description' => t('Please enter product name.'), ); $form['check_name'] = array( '#type' => 'markup', '#value' => "<a href='#' id='check_name'>" . t('Check Product Name') . "</a><br/>", ); $form['status'] = array( '#type' => 'markup', '#value' => "<span id='status'></span><br/>", ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); $form['cancel'] = array( '#type' => 'markup', '#value' => l(t('Cancel'), 'product_mgmt'), ); return $form; } function check_name() { $name = strtolower($_GET['name']); $query = "SELECT COUNT(*) AS total FROM {product} WHERE LOWER(product_name) LIKE ('%s')"; $rs = db_query($query, $name); $info = db_fetch_object($rs); $total = $info->total; if ($total) { echo "$('#status').html('This product is available.');"; } else { echo "$('#status').html('This product is not available.');"; } } ?>
Don’t return anything in “check_name()” function otherwise it will return the whole page when we access “product/check_name” path through AJAX.
/*product.js*/ // JavaScript Document $(document).ready(function() { $('#check_name').attr('href', 'javascript:void(0);'); $('#edit-product-name').keydown(function(event){ $('#status').html(''); }); $('#check_name').click(function() { var name = $.trim($('#edit-product-name').val()); if (name == '') { $('#status').html('Please enter product name.'); return; } $.ajax({ type: "GET", url: "/product/check_name", data: "name=" + encodeURI(name), success: function(msg){ eval(msg); } }); }); });

