cycle
Advanced cycle plugin integration
On 22, Aug 2009 | 8 Comments | In jQuery | By abcoder
On my another post I mentioned that cycle is a great jQuery plugin. I use it very often for slideshow. Recently I used it on a project where the slideshow had many(20+) wallpaper sized images. So loading time was a great issue.
The default cycle integration includes all the images during page loading which is okay for small images, but highly affects the page loading time for high resolution images. cycle plugin is highly flexible! I fixed the issue by loading images dynamically one after another at the end of each slide, literally before the beginning of each slide.
Here is how I did it:
<div id="slideshow"><!-- slides will be added here --></div>
var gallery = ["images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpg", "images/6.jpg", "images/7.jpg", "images/8.jpg", "images/9.jpg", "images/10.jpg", "images/11.jpg", "images/12.jpg", "images/13.jpg", "images/14.jpg"]; // and so on.. array of images
// Array clone function.
Array.prototype.clone = function () {
var arr = new Array();
for (var property in this) {
arr[property] = typeof(this[property]) == 'object' ? this[property].clone() : this[property];
}
return arr;
}
var gal = null; // gal should be global as I'll use it inside onBefore function
$(function () {
gal = gallery.clone(); // cloning the array is not mandatory, but doing so will keep the main array unchanged.
$('#slideshow').empty().append('<img src="' + gal.pop() + '" /><img src="' + gal.shift() + '" /><img src="' + gal.shift() + '" />') // adding the last, first and second element of the array to the slideshow div, then start the cycle plugin.
.cycle({
startingSlide: 1,
// this is required (1 is the 2nd slide) as the first slide is the last element of the array. if you use manual next/prev button clicking on #prev will show the first element.
before: onBefore, // loads the next image at the beginning of each slide. (NOT all at once)
next: "#next",
prev: "#prev"
});
var totalSlideCount = 1 + gal.length;
function onBefore(curr, next, opts, fwd){
// at first addSlide is not defined, so we should return.
if (!opts.addSlide) return;
// if all the elements of the array are added no need to add them again!
if (opts.slideCount == totalSlideCount) return;
// shift or pop from slide array
var nextSlideSrc = fwd ? gal.shift() : gal.pop();
// add next slide
opts.addSlide('<img src="' + nextSlideSrc + '" />', fwd == false);
}
});
In this code above, cloning the array is optional. I used it as I needed the main array for later use. Probably you know if you use the global array reference to a local variable and apply pop()/shift()/push() functions to the local variable, the global one will be affected. cos the local variable is just a reference to the address of the global array variable.
At first the last, 1st and 2nd elements of the array are added to the #slideshow div and the cycle is started from the 2nd slide. Before starting of each slide onBefore function is called. The onBefore function adds new slide to the slideshow and stops when all the array elements are added to the slide.
You may use it for small size images also if you need to improve the page loading duration.
jQuery cycle plugin as carousel
On 14, Jun 2009 | 27 Comments | In jQuery | By abcoder
The jQuery cycle plugin is really amazing! I love this plugin for its huge options for image/div slide show. Right now I’m working on a project which has 3 slideshow on one page. One is simple fade effect with text divs, another one is also fade effect with div + image + text and play/pause, next/back option. The third one is a simple carousel with continuous play mode.
I implemented the first two easily with cycle plugin. But I got stuck with the carousel one. The play and the next state is fine as I used the “scrollLeft” fx. The problem is when I click previous button the fx should be “scrollRight” and not “scrollLeft”. I thought it may need a little complex customized function to do this job of applying different transition effect on previous and next event. I had no clue for it. Even no help from google search! I do not like using a lot of plugins at a time. I know there is jCarousel / jCarousel lite plugin for this, but I still believe there must be a way for doing this with cycle.
It took several hours to find out the simple solution. It’s already inside the cycle plugin, but kinda hidden! I am using the “scrollHorz” fx effect and it is fixed now. WOW! again I discovered that “cycle” is really a marvelous plugin of jQuery.
Here is the example on the malsup site: http://www.malsup.com/jquery/cycle/scrollhv.html
This is the simple code I used:
$('#carousels').cycle({
prev: '#left-arrow',
next: '#right-arrow',
pause: 1,
fx: 'scrollHorz',
timeout: 6000
});
© Copyright 2012 ABCoder |













Recent Comments