tram
Version:
Cross-browser CSS3 transitions in JavaScript
332 lines (321 loc) • 21.2 kB
HTML
<html>
<head>
<title>doc</title>
<style>
/*github.com style (c) Vasily Polovnyov <vast@whiteants.net>*/
pre code {
display: block; padding: 0.5em;
color: #333;
background: #f8f8ff
}
pre .comment,
pre .template_comment,
pre .diff .header,
pre .javadoc {
color: #998;
font-style: italic
}
pre .keyword,
pre .css .rule .keyword,
pre .winutils,
pre .javascript .title,
pre .nginx .title,
pre .subst,
pre .request,
pre .status {
color: #333;
font-weight: bold
}
pre .number,
pre .hexcolor,
pre .ruby .constant {
color: #099;
}
pre .string,
pre .tag .value,
pre .phpdoc,
pre .tex .formula {
color: #d14
}
pre .title,
pre .id {
color: #900;
font-weight: bold
}
pre .javascript .title,
pre .lisp .title,
pre .clojure .title,
pre .subst {
font-weight: normal
}
pre .class .title,
pre .haskell .type,
pre .vhdl .literal,
pre .tex .command {
color: #458;
font-weight: bold
}
pre .tag,
pre .tag .title,
pre .rules .property,
pre .django .tag .keyword {
color: #000080;
font-weight: normal
}
pre .attribute,
pre .variable,
pre .lisp .body {
color: #008080
}
pre .regexp {
color: #009926
}
pre .class {
color: #458;
font-weight: bold
}
pre .symbol,
pre .ruby .symbol .string,
pre .lisp .keyword,
pre .tex .special,
pre .prompt {
color: #990073
}
pre .built_in,
pre .lisp .title,
pre .clojure .built_in {
color: #0086b3
}
pre .preprocessor,
pre .pi,
pre .doctype,
pre .shebang,
pre .cdata {
color: #999;
font-weight: bold
}
pre .deletion {
background: #fdd
}
pre .addition {
background: #dfd
}
pre .diff .change {
background: #0086b3
}
pre .chunk {
color: #aaa
}
</style>
</head>
<body>
<p><a style="float:right" href="https://github.com/bkwld/tram">[view on github]</a> v0.8.3</p>
<h1 id="tram-js">tram.js</h1>
<p>Cross-browser CSS3 transitions in JavaScript.</p>
<h2 id="about">About</h2>
<p>The idea behind Tram is to take the performance and flexibility of CSS transitions and define them in JavaScript - offering a more powerful, expressive API with auto-stopping, sequencing, and cross-browser fallbacks.</p>
<p>Tram currently depends on jQuery for a few reasons: (1) Per-element data API, (2) Cross-browser CSS getters/setters, and (3) scrollTop/Left offset helpers. Keep these features in mind when making custom jQuery builds or porting tram to your library.</p>
<p>Available on npm: <code>npm install tram</code><br>Available on bower: <code>bower install tram</code> </p>
<p>File size:</p>
<ul>
<li>dev ~<code>45 kb</code></li>
<li>min ~<code>16 kb</code></li>
<li>gzip ~<code>4 kb</code></li>
</ul>
<h2 id="examples">Examples</h2>
<ul>
<li><a href="http://bkwld.github.io/tram/examples/basic-opacity.html">basic-opacity</a></li>
<li><a href="http://bkwld.github.io/tram/examples/scroll-top-left.html">scroll-top-left</a></li>
<li><a href="http://bkwld.github.io/tram/examples/sequencing.html">sequencing</a></li>
<li><a href="http://bkwld.github.io/tram/examples/modify-settings.html">modify-settings</a></li>
<li><a href="http://bkwld.github.io/tram/examples/to-from-auto.html">to-from-auto</a></li>
<li><a href="http://bkwld.github.io/tram/examples/transforms.html">transforms</a></li>
<li><a href="http://bkwld.github.io/tram/examples/wait-usage.html">wait-usage</a></li>
</ul>
<h2 id="how-it-works">How it works</h2>
<p>On first load, Tram will use feature detection to determine whether
the browser supports CSS transitions. If yes, Tram will manage styles
and trust the browser to handle the frame by frame animation.
If no, Tram will set styles on each frame, using its own tweening
engine powered by <a href="http://paulirish.com/2011/requestanimationframe-for-smart-animating/" title="requestAnimationFrame">requestAnimationFrame</a> and <a href="http://updates.html5rocks.com/2012/05/requestAnimationFrame-API-now-with-sub-millisecond-precision" title="performance.now()">performance.now()</a>.</p>
<p><em>Please keep your arms and legs inside the tram at all times.</em></p>
<h3 id="basic-usage">Basic usage</h3>
<p>Tram can be loaded via AMD, CommonJS, browser globals, or all of the above (<a href="https://github.com/umdjs/umd">via UMD</a>).</p>
<pre><code class="lang-js"><span class="keyword">var</span> tram = window.tram;
<span class="comment">// or</span>
<span class="keyword">var</span> tram = <span class="keyword">require</span>(<span class="string">'tram'</span>);</code></pre>
<p>Before you add a transition to an element, you must first wrap it with the <code>tram()</code> method. This stores a Tram class instance in the element data, which is used for auto-stop and other state.</p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>;</code></pre>
<p>You may optionally save a reference to this instance, which may help performance for a large group of elements.</p>
<pre><code class="lang-js"><span class="keyword">var</span> myTram = tram(element); <span class="comment">// optional</span></code></pre>
<p>Each property must now be defined using the <code>add()</code> method. This should feel very familiar to CSS3 transition shorthand: <code>property-name duration easing-function delay</code></p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>.<span class="title">add</span><span class="params">('opacity <span class="number">500</span>ms ease-out')</span>;</code></pre>
<p>Once a transition is defined, it is stored in element data. You may override settings later, for example:</p>
<pre><code class="lang-js">tram(element).add(<span class="attribute">'opacity</span> <span class="number">2</span>s'); // changed duration <span class="keyword">to</span> <span class="number">2</span> seconds</code></pre>
<p>To begin a transition on your element, the <code>start()</code> method is used.</p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>.<span class="title">start</span><span class="params">(<span class="tuple">{ opacity: <span class="number">0.5</span> }</span>)</span>;</code></pre>
<p>If you'd like to listen for the transition end event, use <code>then()</code> and supply a function:</p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>
.<span class="title">start</span><span class="params">(<span class="tuple">{ opacity: <span class="number">0.5</span> }</span>)</span>
.<span class="title">then</span><span class="params">(function () <span class="tuple">{ console.<span class="function_name">log</span>('done!') }</span>);</code></pre>
<p>Sequencing is also available by using <code>then()</code>. For example:</p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>
.<span class="title">start</span><span class="params">(<span class="tuple">{ opacity: <span class="number">0.5</span> }</span>)</span>
.<span class="title">then</span><span class="params">(<span class="tuple">{ opacity: <span class="number">1</span> }</span>)</span>
.<span class="title">then</span><span class="params">(<span class="tuple">{ opacity: <span class="number">0</span> }</span>)</span>;</code></pre>
<p>Tram provides some virtual properties to help with CSS3 transforms.</p>
<pre><code class="lang-js">tram(element)
.add(<span class="string">'transform 1s ease-out-quint'</span>)
.start({ <span class="method">x:</span> <span class="number">100</span>, <span class="method">rotate:</span> <span class="number">45</span> }); // <span class="method">aka:</span> translateX(<span class="number">100</span>px) rotate(<span class="number">45</span>deg)</code></pre>
<p>If you need to set style values right away, use the <code>set()</code> method. This will stop any transitions, and immediately set the values.</p>
<pre><code class="lang-js"><span class="function"><span class="title">tram</span><span class="params">(element)</span>.<span class="title">set</span><span class="params">(<span class="tuple">{ x: <span class="number">0</span>, opacity: <span class="number">1</span> }</span>)</span>;</code></pre>
<p>Stopping a transition may be done using the <code>stop()</code> method. This also happens automatically whenever <code>start()</code> or <code>set()</code> are called.</p>
<pre><code class="lang-js">tram(element).stop(<span class="string">'opacity'</span>); <span class="comment">// specific property</span>
tram(element).stop({ opacity: <span class="keyword">true</span>, x: <span class="keyword">true</span> }); <span class="comment">// multiple properties</span>
tram(element).stop(); <span class="comment">// stops all property transitions</span></code></pre>
<p>That's about it. For more, check out the <a href="examples">examples</a> or refer to the docs below.</p>
<h2 id="methods">Methods</h2>
<p>TODO document each method</p>
<h2 id="properties">Properties</h2>
<p>Browser support for transitioning DOM properties is limited, so Tram attempts to cover the most common cross-browser properties, plus a few extras. This list was compiled using CSS animation specs <a href="http://oli.jp/2010/css-animatable-properties/" title="oli.jp">here</a> and <a href="http://www.w3.org/TR/css3-transitions/#properties-from-css-" title="w3.org">here</a>.</p>
<h3 id="supported-property-names-values">Supported property names / values</h3>
<pre><code class="lang-js">'color' <span class="comment">// color</span>
'background' <span class="comment">// color</span>
'outline-color' <span class="comment">// color</span>
'border-color' <span class="comment">// color</span>
'border-top-color' <span class="comment">// color</span>
'border-right-color' <span class="comment">// color</span>
'border-bottom-color' <span class="comment">// color</span>
'border-left-color' <span class="comment">// color</span>
'border-width' <span class="comment">// length</span>
'border-top-width' <span class="comment">// length</span>
'border-right-width' <span class="comment">// length</span>
'border-bottom-width' <span class="comment">// length</span>
'border-left-width' <span class="comment">// length</span>
'border-spacing' <span class="comment">// length</span>
'letter-spacing' <span class="comment">// length</span>
'margin' <span class="comment">// length</span>
'margin-top' <span class="comment">// length</span>
'margin-right' <span class="comment">// length</span>
'margin-bottom' <span class="comment">// length</span>
'margin-left' <span class="comment">// length</span>
'padding' <span class="comment">// length</span>
'padding-top' <span class="comment">// length</span>
'padding-right' <span class="comment">// length</span>
'padding-bottom' <span class="comment">// length</span>
'padding-left' <span class="comment">// length</span>
'outline-width' <span class="comment">// length</span>
'opacity' <span class="comment">// number</span>
'top' <span class="comment">// length, percentage</span>
'right' <span class="comment">// length, percentage</span>
'bottom' <span class="comment">// length, percentage</span>
'left' <span class="comment">// length, percentage</span>
'font-size' <span class="comment">// length, percentage</span>
'text-indent' <span class="comment">// length, percentage</span>
'word-spacing' <span class="comment">// length, percentage</span>
'width' <span class="comment">// length, percentage</span>
'<span class="built_in">min</span>-width' <span class="comment">// length, percentage</span>
'<span class="built_in">max</span>-width' <span class="comment">// length, percentage</span>
'height' <span class="comment">// length, percentage</span>
'<span class="built_in">min</span>-height' <span class="comment">// length, percentage</span>
'<span class="built_in">max</span>-height' <span class="comment">// length, percentage</span>
'line-height' <span class="comment">// number, length, percentage</span>
'transform' <span class="comment">// (see transform info below)</span>
'scroll-top' <span class="comment">// number (tween-only)</span>
'scroll-left' <span class="comment">// number (tween-only)</span>
<span class="comment">// TODO - planned support</span>
<span class="comment">// 'background-position' // [x, y] length, percentage</span>
<span class="comment">// 'transform-origin' // [x, y] length, percentage</span>
<span class="comment">// 'clip' // [x, y, w, h] rectangle</span>
<span class="comment">// 'crop' // [x, y, w, h] rectangle</span></code></pre>
<p><strong>Note:</strong> <code>dash-style</code> names are required for <code>.add()</code>, but other methods like <code>.start()</code> and <code>.stop()</code> may use <code>camelCase</code>.</p>
<h3 id="transforms">Transforms</h3>
<p>TODO describe transform shortcuts w/ examples</p>
<pre><code class="lang-js"><span class="string">'x'</span> <span class="comment">// length, percentage</span>
<span class="string">'y'</span> <span class="comment">// length, percentage</span>
<span class="string">'z'</span> <span class="comment">// length, percentage</span>
<span class="string">'rotate'</span> <span class="comment">// angle</span>
<span class="string">'rotateX'</span> <span class="comment">// angle</span>
<span class="string">'rotateY'</span> <span class="comment">// angle</span>
<span class="string">'rotateZ'</span> <span class="comment">// angle</span>
<span class="string">'scale'</span> <span class="comment">// number</span>
<span class="string">'scaleX'</span> <span class="comment">// number</span>
<span class="string">'scaleY'</span> <span class="comment">// number</span>
<span class="string">'scaleZ'</span> <span class="comment">// number</span>
<span class="string">'skew'</span> <span class="comment">// angle</span>
<span class="string">'skewX'</span> <span class="comment">// angle</span>
<span class="string">'skewY'</span> <span class="comment">// angle</span>
<span class="string">'perspective'</span> <span class="comment">// length</span></code></pre>
<h3 id="easings">Easings</h3>
<p>A useful site with demos of most of these is <a href="http://easings.net/">easings.net</a></p>
<pre><code class="lang-js">// Defaults
<span class="attribute">'ease</span>'
<span class="attribute">'ease</span>-<span class="keyword">in</span>'
<span class="attribute">'ease</span>-<span class="keyword">out</span>'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>'
<span class="attribute">'linear</span>'
// Quad
<span class="attribute">'ease</span>-<span class="keyword">in</span>-quad'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-quad'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-quad'
// Cubic
<span class="attribute">'ease</span>-<span class="keyword">in</span>-cubic'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-cubic'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-cubic'
// Quart
<span class="attribute">'ease</span>-<span class="keyword">in</span>-quart'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-quart'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-quart'
// Quint
<span class="attribute">'ease</span>-<span class="keyword">in</span>-quint'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-quint'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-quint'
// Sine
<span class="attribute">'ease</span>-<span class="keyword">in</span>-sine'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-sine'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-sine'
// Expo
<span class="attribute">'ease</span>-<span class="keyword">in</span>-expo'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-expo'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-expo'
// Circ
<span class="attribute">'ease</span>-<span class="keyword">in</span>-circ'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-circ'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-circ'
// Back
<span class="attribute">'ease</span>-<span class="keyword">in</span>-back'
<span class="attribute">'ease</span>-<span class="keyword">out</span>-back'
<span class="attribute">'ease</span>-<span class="keyword">in</span>-<span class="keyword">out</span>-back'</code></pre>
<h2 id="todo">TODO</h2>
<ul>
<li>Add .get(prop) method to return current value</li>
<li>Support array values for props like 'background-position'</li>
</ul>
<h2 id="contributing">Contributing</h2>
<ol>
<li><p>If you'd like to contribute to this project, please submit all pull requests to the <code>dev</code> branch. Any pull requests sent to <code>master</code> will be closed. This is mostly to offset the convenience of having various dist/* files available on the master branch.</p>
</li>
<li><p>Grunt CLI tools may be helpful. The following commands should start a watch script that concats source files on each save:<br>(from the root directory)<br><code>npm install grunt -g</code><br><code>grunt</code> </p>
</li>
<li><p>Once you're ready to send a pull request, please view <code>test/suite.html</code> in your browser to confirm that all tests are passing.</p>
</li>
</ol>
<h2 id="thanks">Thanks</h2>
<p>Special thanks to the following open source authors + libraries.</p>
<p>@ded - <a href="https://github.com/ded/morpheus">https://github.com/ded/morpheus</a><br>@rstacruz - <a href="https://github.com/rstacruz/jquery.transit">https://github.com/rstacruz/jquery.transit</a><br>@visionmedia - <a href="https://github.com/visionmedia/move.js">https://github.com/visionmedia/move.js</a><br>@jayferd - <a href="https://github.com/jayferd/pjs">https://github.com/jayferd/pjs</a> </p>
<h2 id="mit-license">MIT License</h2>
<p>This code may be freely distributed under the <a href="http://danro.mit-license.org/">MIT license</a>.</p>
<h3 id="terms-of-use-easing-equations">Terms Of Use - Easing Equations</h3>
<p>Open source under the <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>.</p>
<p>Copyright © 2001 Robert Penner
All rights reserved.</p>
<p>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
<ul>
<li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
<li>Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.</li>
</ul>
<p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
</body>
</html>