Revision of Twine: Sugarcane custom CSS passage transitions from Thu, 02/21/2013 - 01:22

I've modified my previous Sugarcane passage transition code to allow you to define your own transitions with CSS.

First, include this code in a script passage:

History.prototype.display=function(d,b,a){var c=tale.get(d);this.history.unshift({passage:c,variables:clone(this.history[0].variables)});
this.history[0].hash=this.save();var e=c.render();e.style.visibility="visible";if(a!="offscreen"){var p=$("passages");
for(var i=0;i<p.childNodes.length;i+=1){var q=p.childNodes[i];q.classList.add("transition-out");
setTimeout(function(){p.removeChild(q);},1000);}e.classList.add("transition-in");
setTimeout(function(){e.classList.remove("transition-in");},1);p.appendChild(e);}if((a=="quietly")||(a=="offscreen")){e.style.visibility="visible";
}if(a!="offscreen"){document.title=tale.title;this.hash=this.save();document.title+=": "+c.title;
window.location.hash=this.hash;window.scroll(0,0);}return e;};

What this does is cause the outgoing passage to gain a "transition-out" class, and the incoming passage to instantly gain and lose a "transition-in" class. This enables you to define new passage transitions by styling these 3 classes using CSS.

For starters, add this to the CSS for your .passage:

.passage {
	transition: 0.25s;
	-webkit-transition: 0.25s;
	-moz-transition: 0.25s;
	-o-transition: 0.25s;
	-ms-transition: 0.25s;
}
The transition property causes elements to transition between styles when they gain and lose them, in a specific way. In this case, it's a quarter-second, purely linear transition. You need to write these five variants in order to cover the major browsers (you can also include -khtml-transition for those few Konqueror users).

A very basic example:

.transition-in {
	position:fixed;
	opacity:0;
}
When a new passage arrives, its style will transition from the "transition-in" style to the normal passage style. In this case, it starts invisible ("opacity:0") and fades in to visibility. Note the "position:fixed" - either "transition-in" or "transition-out" should have position:fixed if you want them to neatly appear above or below each other.

.transition-out {
	position:fixed;
	opacity:0;
}

Putting identical styles for "transition-out" means that departing passages will fade out of visibility as well.

A broad number of CSS attributes can be used with this technique, and it can also be combined with tag-based passage CSS to give different passages different transitions. Experiment!