Scriptaculous opacity flicker
Thursday, 5th February, 2009Update: A better solution, which enables more than one effect to be used is Effect.Queues.
I recently used scriptaculous at work to provide some animation effects in a product. Specifically using Effect.Opacity(). However I ran into a problem in all browsers, on both Windows and Mac OS X, where the page element would flicker during the opacity change. My code went something like this:
changeItem: function(container, item) { var html = '<div>' + item['title'] + '</div>'; new Effect.Opacity( container, { from: 1, to: 0, duration: 0.5 } ); container.innerHTML = html; new Effect.Opacity( container, { from: 0, to: 1, duration: 1 } ); },
After reading about bugs in Firefox where I had to set the opacity to 0.99 instead of 1; or setting the background color, width and height to the elements that I was applying these effects on, I eventually solved it doing this:
changeItem: function(container, item) { var html = '<div>' + item['title'] + '</div>'; container.setStyle({opacity: 0}); container.innerHTML = html; new Effect.Opacity( container, { from: 0, to: 1, duration: 1 } ); },
The first effect I was applying was not running in sequence - I mistaken assumed it would. These two effects either side of the innerHTML statement were fighting one another. Removing the first didn’t change the visual effect much and cured the flickering! Pass it on.




