The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
Learn how to create a Customizable and Flexible jQuery Carousel Rotator.
Recently while working on a client I ran into a major jQuery crisis: I needed a carousel rotator which had a menu and for each menu item an image and description. Said image could not have dimensional restraints and the description needed to be able to contain HTML if necessary. The menu could not be restricted to a single format (not just list items) and it needed to hold default content to start the rotation. Lastly, it needed to be able to adapt to multiple environments and not be restricted to a single layout or style or require a number to be set as to how many list items would be included.
Solution: mix, mash, and create my own code because all of the scripts I found failed to properly handle the content, were restrictive to a certain styling, or required a specific number to know how many items it was rotating through.
You can view a live example of this script without any styling and only basic imagery. (Remember, styling is up to you and your design and any image will fit within the script.)
/*
* jQuery Carousel Compilation by Ryan Barr
* http://spookyismy.name
* v1.0.0
*/
$(document).ready(function() {
/* Show default content within .carousel-content */
$(".carousel-content").show();
$(".carousel-content .carousel-content-image").animate({ opacity: 0.85 }, 1 );
/* Set the first menu item as active. */
$("#carousel-menu li:first").addClass('selected');
/* If menu item is clicked, switch content properly. */
$("#carousel-menu li.menu-item").click(function(){
var altText = $(this).find('.carousel-content-image img').attr("alt");
var imgSrc = $(this).find('a.carousel').attr("href");
var imgDesc = $(this).find('.info').html();
if ($(this).is(".selected")) {
return false;
} else {
$(".carousel-content").animate({ opacity: 0 }, 250 , function() {
$(".carousel-content-info").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 400 );
$(".carousel-content-image img").attr({ src: imgSrc , alt: altText }).animate({ opacity: 1 }, 400 );
$(".carousel-content").animate({ opacity: 1 }, 250 );
});
}
$("#carousel-menu li").removeClass('selected');
$(this).addClass('selected');
return false;
});
/* Pause Rotation if Hovering #carousel-menu Item */
carouselNext = false;
$("#carousel-menu li").hover(
function () {
carouselNext = true;
},
function () {
carouselNext = false;
}
);
/* Rotate Carousel, Move from :last to :first if necessary. */
var carouselNextClick = function(){
if(!carouselNext) {
var $nextElement = $(".selected").next("li.menu-item");
if($("li.selected").hasClass("menu-last") ){
$("#carousel-menu li:first").trigger("click");
} else {
$nextElement.trigger("click");
}
}
};
/* Pause between rotations in milliseconds. (3500 is 3.5 seconds) */
setInterval(carouselNextClick, 3500);
});
<ul id="carousel-menu">
<li class="menu-item"><a class="carousel" href="images/sample1.png">Sample 1</a>
<div class="info">Slug about the first sample.</div></li>
<li class="menu-item"><a class="carousel" href="images/sample2.png">Sample 2</a>
<div class="info">Slug about the second sample.</div></li>
<li class="menu-item menu-last"><a class="carousel" href="images/sample3.png">Sample 3</a>
<div class="info">Slug about the third sample.</div></li>
</ul>
<div class="carousel-content">
<div class="carousel-content-image"><img src="images/sample1.png" alt="Sample Image" /></div>
<div class="carousel-content-info">Slug about the first sample.</div>
</div>
#carousel-menu li { cursor: pointer; }
#carousel-menu li.selected { background: #eee; }
.info { display: none; }
To make this easy we will break this down into each section: jQuery, HTML, and CSS.
Please remember you must be properly calling both the jQuery library and the JavaScript code above into your document in order for this to function properly. For example:
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="js/jquery-carousel.1.0.0.js" type="text/javascript"></script>
Placing the above code between your head tags would cause the JavaScript to load prior to the content on your page. Placing it right before the </body> tag will allow it to load after your content as been loaded.
Questions? Comments? Suggestions? Comments form is below.
Ryan Barr is a professional web designer and developer. You can read this original post on his blog.
|
clippingimages Mon May 31, 2010 4:32 am
great :)
|
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN