Final Revision for Scrolling Animation Code, Maybe
Introduction
After I revised the scrolling animation jQuery code yesterday, I was going to rewrite it using Mootools. However, I noticed that this code was doing two unnecessary actions. The first one is the removing of the first set of images, and another one is the appending of new sets of images. Several minutes after my eyes watched the scrollbar animate back and forth, I revised the code to the following.
The Code
if ($("#whoWeAreImages").length != 0) {
// Scroll Animation for the images in the Who We Are page
var $whoWeAreImages = $("#whoWeAreImages"),
$wwaLink = $whoWeAreImages.find("a"),
$wwaImg = $whoWeAreImages.find("img"),
scrollWidth = $whoWeAreImages.attr("scrollWidth");
// Do the following once the page is loaded
$whoWeAreImages.css({overflow:"hidden"})Â Â Â // Hide the images scrollbar
.scrollLeft(0)Â Â Â // Make sure the scrollbar is at the beginning when the page is reloaded
.append($(this).html()) // Add one more set of images
 // Begin the animation
.autoscroll({
direction: 0,
step: 50,
onEvaluate: function() {
// Click action for the images in the Who We Are page
$wwaLink.fancybox({
transitionIn: "elastic",
transitionOut: "elastic"
})
// Scroll to the beginning once it scrolls to the beginning of the second set of images
if ($(this).scrollLeft() >= scrollWidth) {
$(this).autoscroll("pause");
$(this).attr("scrollLeft", "0");
$(this).autoscroll("resume");
}
}
})
.autoscroll("addpausesource", $wwaImg);
}
Explanation
In the above code, I removed the remove() action and added the append action at the beginning of the loop. It means that it will only has two sets of images available instead of an infinite loop that remove and append continuously to limit two loops which has slower performance.
After this revision, the animation lag still exists. This is probably the final revision of the code before I rewrite it using Mootools because I might discover some techniques during the rewriting of this code.
Revision for the Auto Scrolling Animation code
Introduction
Since I wrote the auto scrolling animation code with jQuery two days ago, I struggled with the browser performance while running this animation. It seems that the animation needs lots of CPU power and it sometimes appears to be lagging. In order to solve the CPU issue, I included a jQuery plugin named Autoscroll. As the name applies, it automatically scrolls the specified element. This has the same feature as the code I wrote, but it’s a plugin and it will immediately stop the animation when in hover state whereas my code stops animation few seconds after the cursor was hovered over the element.
The Source Code
The following is the revised code:
if ($("#whoWeAreImages").length != 0) {
// Scroll Animation for the images in the Who We Are page
var $whoWeAreImages = $("#whoWeAreImages"),
$wwa = $(".WWA:first"),
$wwaLink = $whoWeAreImages.find("a"),
$wwaImg = $whoWeAreImages.find("img"),
scrollWidth = $whoWeAreImages.attr("scrollWidth"),
wwaHTML = $whoWeAreImages.html();
// Do the following once the page is loaded
$whoWeAreImages.css({overflow:"hidden"})Â Â Â // Hide the images scrollbar
.scrollLeft(0)Â Â Â // Make sure the scrollbar is at the beginning when the page is reloaded
// Begin the animation
.autoscroll({
direction: 0,
step: 50,
onEvaluate: function() {
// Click action for the images in the Who We Are page
$wwaLink.fancybox({
transitionIn: "elastic",
transitionOut: "elastic"
})
// Remove the first set of images in order to limit loop
if ($(this).scrollLeft() >= scrollWidth) {
$wwa.remove();
$(this).autoscroll("pause");
$(this).attr("scrollLeft", "0");
$(this).autoscroll("resume");
}
},
onEdge: function(a) {
if ($(this).attr("scrollLeft") != 0) {
// Append the same images to the end of the last image if it finished scrolling to loop through the animation
$(this).append(wwaHTML);
}
}
})
.autoscroll("addpausesource", $wwaImg);
}
Explanation
As you can see in the code, I optimized the code to use the chaining method for jQuery.
I also changed the way images were looped. When scrollLeft greater than or equals to scrollWidth, the first set of images will be removed/deleted, pause the animation, set scrollLeft to zero, and restart the animation.
This means that when the position of the scrollbar reaches the beginning of the second set of images (or second loop), delete the first set of images using :first pseudo selector and reset the scrollbar position to the beginning. Since the scrollbar position will be at the end of the first loop and the beginning of the second loop, remove the first loop and reset the scrollbar position have n0 effect on the animation. The animation would continue to loop through the images without interruption but it’s limited to two loops.
Although the code was optimized, the animation still has some lag. It will sometimes stop and continue without interaction, much like the under-powered graphics card. I have not yet find any solution to this issue yet. However, I found out that Mootools animation is sometimes more smooth than jQuery. I will try to use Mootools to animate the auto scroll later.
Do you believe that Mootools animation is more smooth than jQuery? Please share your opinion in the discussion.
More Than One Month Usage of Chromium
I used Google Chrome on Ubuntu 9.10 and switched to Chromium on Ubuntu 10.04. Apart from being open source, I can’t tell any difference between Google Chrome and Chromium. During the switch process from Firefox to Chromium in the web development environment, I didn’t have any problems since I mostly used server-side script languages. Recently I have been interested in MooTools JavaScript Framework and followed its guide to test the code in Chromium. All went well until I test the class code. It seems that Chromium does not support document.write() JavaScript function very well. It worked in Firefox after my test with Chromium failed. I’m not familiar with Chromium’s JavaScript engine, but in my experience, Chrome has similar problem since the early build of Chrome 1. Long time ago, I used Chrome 1 alpha as my default web development environment in Windows. It disappointed me that Chrome didn’t have a complete JavaScript engine yet. It is ashamed that Google still has little bit of problems with its browser’s JavaScript engine as the market share of Chrome continues to go up. As a user, my overall usage experience of Chromium is good. However, Chromium doesn’t suited for my web development yet.
Site Menu Animation Prototype
I have been playing around with MooTools recently and came up with an idea to allow users to change the orientation of the menu for my updated site with animation. MooTools is an Object Oriented JavaScript framework similar to jQuery. The main reason I chose this framework instead of jQuery is that it can easily create customized animations. Although I’m still learning how to use MooTools’ full features, I wrote a HTML page that toggles between vertical and horizontal menu with custom animations just by using MooTools framework. Note that this is a prototype to the upcoming updated site menu. It only contains the background color and doesn’t contain any content. Here is the MooTools code for the page:
window.addEvent("domready", function() {
$('hori').setStyle('opacity','0');
$("change").addEvent("click", function() {
$('change').setStyles({
'visibility':'hidden',
'opacity':0
});
var changeMenu = new Fx.Tween($('menu'));
changeMenu.start('width','50px')
.chain(function() {
changeMenu.start('height','400px');
})
.chain(function() {
changeMenu.start("left", "10px");
})
.chain(function() {
changeMenu.start("top", "40px");
})
.chain(function() {
$('hori').fade('in');
});
return false;
});
$("hori").addEvent("click", function() {
$('hori').setStyles({
'visibility':'hidden',
'opacity':0
});
var changeMenu = new Fx.Tween($('menu'));
changeMenu.start("top", "50px")
.chain(function() {
changeMenu.start("left", "100px");
})
.chain(function() {
changeMenu.start('height','50px');
})
.chain(function() {
changeMenu.start('width','600px');
})
.chain(function() {
$('change').fade('in');
});
return false;
});
});
Notice that I used CSS property names and MooTools functions to create animations. You can view the whole source code by right-click the prototype page since it is just a plain HTML and JavaScript page.
I created this prototype with the help of MooTools 1.2 Beginner’s Guide and MooTools Docs.