While putting together (cutting up into XHTML/CSS) the new design for the Goldsmiths website, which we are currently building, I decided to use jCarousel Lite for a product gallery. This light-weight version of the well known jQuery plugin jCarousel, allows for the base functionality of the full blown plugin without the additional overhead which allows you to do almost anything with the full version.
Although the difference in size could be considered negligible with jCarousel minified being 13.8kb and jCarousel minified being 2.32kb, with the advent of Google Caffeine those precious kb could be vital in gaining that extra spot in the rankings.
So, I set about using jCarousel Lite and found it very simple to install in the website and within minutes I had my carousel rolling with it's auto scrolling, it was great. I continued to play with it whilst styling the elements and noticed a few things which might not be considered a big deal but irked me all the same which were that:
- While having the auto scroll active and manually scrolling through elements using my next and previous buttons the auto scroll would just kick in (the timeout would trigger it) and move me on another element, or back one.
- When increasing the amount of elements which scroll, using the scroll variable to 3+ with circular motion being set, upon getting to the first or last item it would suddenly jump (pop) along an item or two, thus making the circular motion un-smooth.
- Similar to the above when scrolling 3+ elements per scroll with circular motion it would sometimes scroll through to the incorrect item (depending on the amount of items I had in place).
Upon finding these issues I decided to go about fixing them, so into the plugin I went, with high hopes and a fist full of jQuery knowledge to help me on my way.
The first issue was a piece of cake to fix, so the timer was clearly not resetting when I chose to manually scroll, so I simply told it to do just that by storing the timeoutID and clearing it when go is called to scroll and setting it again when the scroll has completed.
Upon fixing the first issue I moved onto the others, quickly pinpointing the problem to be when the carousel loops back on itself with the circular functionality. Looking into this section of code (the go function) it was clear that there was something wrong with the math involved as the current element was not taken into account when choosing the position it should jump to in order to prepare for the next scroll (issue 2). So I had a play with this and after a few tries I got it working, and upon testing it with several variations of scroll count (1-5) I concluded that it was issue 2 was now fixed.
I finally moved onto issue 3 which was contained besides issue 2 (as the jump position and element to scroll to are setup at the same time), which again was a case of altering the math in order to take into account several other elements, which again included the current element. So i made some changes and tested out again on several variations of scroll and concluded that issue 3 is now also fixed.
Below is the difference in code for issues 2&3.
jCarousel Lite Original:
if(o.circular) {
if(to<=o.start-v-1) {
ul.css(animCss, -((itemLength-(v*2))*liSize)+"px"); //issue 1
curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll; //issue 2
} else if(to>=itemLength-v+1) {
ul.css(animCss, -( (v) * liSize ) + "px" ); //issue 1
curr = to==itemLength-v+1 ? v+1 : v+o.scroll; //issue 2
} else curr = to;
}
jCarousel Lite Fixed:
if(o.circular) {
if(to<=o.start-v-1) {
ul.css(animCss, -((itemLength-(v*2-curr))*liSize)+"px"); //issue 1
curr = to == o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll+curr; //issue 2
} else if(to>=itemLength-v+1) {
ul.css(animCss, -((curr - (itemLength - (v*2))) * liSize) + "px" ); //issue 1
curr = to == itemLength-v+1 ? v+1 : curr - (itemLength - (v*2)) + o.scroll; //issue 2
} else curr = to;
}
So if like me you get irked by little annoying bugs as are demonstrated in jCarousel Lite, then you can download jCarousel Lite-Fixed from below, which includes the fixes above. And if you fix it in a better way or find that my changes don't 100% fix the bugs/create new ones then
let us know.
JCarousel Lite 1.0.1 Fixed -
uncompressed -
minified -
packed
POSTED BY: Henry Ruhl