• Stars
    star
    186
  • Rank 207,316 (Top 5 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-browser CSS3 transitions in JavaScript.

tram.js

Cross-browser CSS3 transitions in JavaScript.

About

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.

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.

Available on npm: npm install tram
Available on bower: bower install tram

File size:

  • dev ~45 kb
  • min ~16 kb
  • gzip ~4 kb

Examples

How it works

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 requestAnimationFrame and performance.now().

Please keep your arms and legs inside the tram at all times.

Basic usage

Tram can be loaded via AMD, CommonJS, browser globals, or all of the above (via UMD).

var tram = window.tram;
// or
var tram = require('tram');

Before you add a transition to an element, you must first wrap it with the tram() method. This stores a Tram class instance in the element data, which is used for auto-stop and other state.

tram(element);

You may optionally save a reference to this instance, which may help performance for a large group of elements.

var myTram = tram(element); // optional

Each property must now be defined using the add() method. This should feel very familiar to CSS3 transition shorthand: property-name duration easing-function delay

tram(element).add('opacity 500ms ease-out');

Once a transition is defined, it is stored in element data. You may override settings later, for example:

tram(element).add('opacity 2s'); // changed duration to 2 seconds

To begin a transition on your element, the start() method is used.

tram(element).start({ opacity: 0.5 });

If you'd like to listen for the transition end event, use then() and supply a function:

tram(element)
  .start({ opacity: 0.5 })
  .then(function () { console.log('done!') });

Sequencing is also available by using then(). For example:

tram(element)
  .start({ opacity: 0.5 })
  .then({ opacity: 1 })
  .then({ opacity: 0 });

Tram provides some virtual properties to help with CSS3 transforms.

tram(element)
  .add('transform 1s ease-out-quint')
  .start({ x: 100, rotate: 45 }); // aka: translateX(100px) rotate(45deg)

If you need to set style values right away, use the set() method. This will stop any transitions, and immediately set the values.

tram(element).set({ x: 0, opacity: 1 });

Stopping a transition may be done using the stop() method. This also happens automatically whenever start() or set() are called.

tram(element).stop('opacity'); // specific property
tram(element).stop({ opacity: true, x: true }); // multiple properties
tram(element).stop(); // stops all property transitions

That's about it. For more, check out the examples or refer to the docs below.

Methods

TODO document each method

Properties

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 here and here.

Supported property names / values

'color'                // color
'background'           // color
'outline-color'        // color
'border-color'         // color
'border-top-color'     // color
'border-right-color'   // color
'border-bottom-color'  // color
'border-left-color'    // color
'border-width'         // length
'border-top-width'     // length
'border-right-width'   // length
'border-bottom-width'  // length
'border-left-width'    // length
'border-spacing'       // length
'letter-spacing'       // length
'margin'               // length
'margin-top'           // length
'margin-right'         // length
'margin-bottom'        // length
'margin-left'          // length
'padding'              // length
'padding-top'          // length
'padding-right'        // length
'padding-bottom'       // length
'padding-left'         // length
'outline-width'        // length
'opacity'              // number
'top'                  // length, percentage
'right'                // length, percentage
'bottom'               // length, percentage
'left'                 // length, percentage
'font-size'            // length, percentage
'text-indent'          // length, percentage
'word-spacing'         // length, percentage
'width'                // length, percentage
'min-width'            // length, percentage
'max-width'            // length, percentage
'height'               // length, percentage
'min-height'           // length, percentage
'max-height'           // length, percentage
'line-height'          // number, length, percentage
'transform'            // (see transform info below)
'scroll-top'           // number (tween-only)
'scroll-left'          // number (tween-only)

// TODO - planned support
// 'background-position'  // [x, y] length, percentage
// 'transform-origin'     // [x, y] length, percentage
// 'clip'                 // [x, y, w, h] rectangle
// 'crop'                 // [x, y, w, h] rectangle

Note: dash-style names are required for .add(), but other methods like .start() and .stop() may use camelCase.

Transforms

TODO describe transform shortcuts w/ examples

'x'                    // length, percentage
'y'                    // length, percentage
'z'                    // length, percentage
'rotate'               // angle
'rotateX'              // angle
'rotateY'              // angle
'rotateZ'              // angle
'scale'                // number
'scaleX'               // number
'scaleY'               // number
'scaleZ'               // number
'skew'                 // angle
'skewX'                // angle
'skewY'                // angle
'perspective'          // length

Easings

A useful site with demos of most of these is easings.net

// Defaults
'ease'
'ease-in'
'ease-out'
'ease-in-out'
'linear'

// Quad
'ease-in-quad'
'ease-out-quad'
'ease-in-out-quad'

// Cubic
'ease-in-cubic'
'ease-out-cubic'
'ease-in-out-cubic'

// Quart
'ease-in-quart'
'ease-out-quart'
'ease-in-out-quart'

// Quint
'ease-in-quint'
'ease-out-quint'
'ease-in-out-quint'

// Sine
'ease-in-sine'
'ease-out-sine'
'ease-in-out-sine'

// Expo
'ease-in-expo'
'ease-out-expo'
'ease-in-out-expo'

// Circ
'ease-in-circ'
'ease-out-circ'
'ease-in-out-circ'

// Back
'ease-in-back'
'ease-out-back'
'ease-in-out-back'

TODO

  • Add .get(prop) method to return current value
  • Support array values for props like 'background-position'

Contributing

  1. If you'd like to contribute to this project, please submit all pull requests to the dev branch. Any pull requests sent to master will be closed. This is mostly to offset the convenience of having various dist/* files available on the master branch.

  2. Grunt CLI tools may be helpful. The following commands should start a watch script that concats source files on each save:
    (from the root directory)
    npm install grunt -g
    grunt

  3. Once you're ready to send a pull request, please view test/suite.html in your browser to confirm that all tests are passing.

Thanks

Special thanks to the following open source authors + libraries.

@ded - https://github.com/ded/morpheus
@rstacruz - https://github.com/rstacruz/jquery.transit
@visionmedia - https://github.com/visionmedia/move.js
@jayferd - https://github.com/jayferd/pjs

MIT License

This code may be freely distributed under the MIT license.

Terms Of Use - Easing Equations

Open source under the BSD License.

Copyright © 2001 Robert Penner All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • 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.
  • 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.

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.

More Repositories

1

croppa

Image thumbnail creation through specially formatted URLs for Laravel.
PHP
496
star
2

cloner

A trait for Laravel Eloquent models that lets you clone a model and it's relationships, including files. Even to another database.
PHP
458
star
3

decoy

A Laravel model-based CMS
PHP
303
star
4

laravel-pug

Pug view adapter for Laravel and Lumen
PHP
159
star
5

vue-ssr-carousel

A performance focused Vue carousel designed for SSR/SSG environments.
JavaScript
131
star
6

vue-in-viewport-directive

Vue 2 directive that adds css classes when the element is the viewport
JavaScript
120
star
7

vue-in-viewport-mixin

Vue 2 mixin to determine when a DOM element is visible in the client window
JavaScript
102
star
8

vue-visual

Vue 2 image and video loader supporting lazy loading, background videos, fixed aspect ratios, low rez poster images, transitions, loaders, slotted content and more.
JavaScript
59
star
9

laravel-haml

Wraps MtHaml for ease use in Laravel
PHP
45
star
10

reporter

Generate styled logs of Laravel requests that include application timing, memory usage, input data, and sql queries
PHP
30
star
11

nuxt-stylus-resources-loader

Stylus resources (e.g. variables, mixins etc.) module for NuxtJs
JavaScript
25
star
12

upchuck

A simple, automatic handler of file uploads for Laravel's Eloquent models using using Flysystem.
PHP
22
star
13

body-scroll-toggle

Enables / disables scroll on the body
JavaScript
19
star
14

vue-height-tween-transition

Tween the height of the parent of transitioning items for use in accordions or carousels.
JavaScript
16
star
15

nuxt-spa-store-init

A simple Nuxt module that will hydrate the VueX store when running in SPA mode (not SSR)
JavaScript
15
star
16

scrapey

Get info about a URL such as you get when sharing a link on Facebook.
PHP
11
star
17

shopify-gtm-instrumentor

Helpers for sending standardized dataLayer events from a Shopify site, inspired by GA Enhanced Ecommerce.
JavaScript
11
star
18

freezer

Using Laravel, creates cached versions of full pages that can be served directly by Apache
PHP
9
star
19

vue-balance-text

A Vue directive that implements the "balance-text" package to create line breaks to make each line of text in an element equal.
JavaScript
8
star
20

nuxt-coffeescript-module

Adds Coffeescript support to your Nuxt app
JavaScript
8
star
21

vue-hamburger

A hamburger icon that transitions to a close icon
JavaScript
7
star
22

php-library

PHP utility classes
PHP
7
star
23

data-layer-events

Push clean events onto Google Tag Manager's dataLayer
JavaScript
7
star
24

stylus-library

Stylus utility mixins
Stylus
7
star
25

vue-tween-number

A Vue component that tweens a number value.
JavaScript
6
star
26

bukwild-contentful-utils

Utilities for interacting with Contentful, designed with Vue and Nuxt in mind.
JavaScript
5
star
27

lightkeeper

Averages multiple successive Lighthouse tests to arrive at a more accurate PageSpeed score
JavaScript
5
star
28

nuxt-page-transition-and-anchor-handler

Scroll to top before page transition, handle anchor links, and cross-dissolve between pages
JavaScript
4
star
29

sass-to-stylus

Script to convert a project's sass files to stylus
CoffeeScript
4
star
30

codebasehq

Tools for integrating Laravel apps with CodebaseHQ features
PHP
4
star
31

cloak

Opinionated Nuxt + Craft boilerplate
CoffeeScript
4
star
32

contentful-color-selector

A UI Extension for Contentful that generate a dropdown for selecting a space-wide color
HTML
4
star
33

vue-embed

Parse html from the CMS for use with statically generated sites
JavaScript
3
star
34

vue-in-view

Vue component for triggering animations, adding classes, firing events, and syncing slot variables based on visibility in the viewport.
JavaScript
3
star
35

sass-library

Sass utility mixins
CSS
3
star
36

create-cloak-app

Sets up a new Cloak (Nuxt + Craft/Contentful) based project.
CoffeeScript
3
star
37

vue-routing-anchor-parser

A Vue directive that parses child elements for internally linking anchor tags and binds their click events to use Vue Router's push().
JavaScript
3
star
38

buk-builder

NodeJS-based asset versioning, RequireJS build tool.
JavaScript
2
star
39

following-circles

Generative art demo using Backbone
JavaScript
2
star
40

katamari

Convert distances into terms that are more easily visualized
JavaScript
2
star
41

auto-publish

Automatically publish all Laravel workbench assets on every request
PHP
2
star
42

vscode-config

For collaborating on shared VS Code configuration.
2
star
43

asana-gitlab-bridge

Opinionated, self-hosted tool that keeps GitLab in sync with Asana
CoffeeScript
2
star
44

craft-webhook-scheduler

A Craft plugin that triggers webhooks when scheduled posts become active.
PHP
2
star
45

benchpress

Wordpress boilerplate
PHP
2
star
46

vue-unorphan

A Vue directive that implements the "unorphan" package to prevent line breaks between the last two words of an element.
JavaScript
2
star
47

craft-netlify-deploy-status

A Craft plugin that shows Netlify deploy statuses.
PHP
2
star
48

vue-detachable-header

Vue component that wraps your header and renders at the top of the viewport when scrolling up.
JavaScript
2
star
49

vue-modal

A component that renders a modal window with slotted content. Includes trapped tabbing for ADA compliance
JavaScript
1
star
50

cloak-i18n

Localization conventions for Cloak + Craft.
JavaScript
1
star
51

cloak-boilerplate

Modules that setup standard Cloak conventions.
JavaScript
1
star
52

js-library

JS utility modules
JavaScript
1
star
53

jquery-backbone-views

jQuery plugin for instantiating Backbone views from selected elements
JavaScript
1
star
54

nuxt-remote-asset-cache

Store remote assets in the local build during Nuxt generation
CoffeeScript
1
star
55

photoshop-actions

Photoshop actions we use
1
star
56

window-event-mediator

Mediator pattern for window event handling
JavaScript
1
star
57

filestack-test

Testing connecting to Filestack
PHP
1
star
58

cloak-sandbox

A simplified Cloak app for demos and experiments.
Stylus
1
star
59

vue-cover-video-component

Vue component for dynamically loading single-page HTML 5 videos
Vue
1
star
60

light-or-dark

Return whether a hex or rgb color is light or dark
JavaScript
1
star
61

react-visual

Renders images and videos into a container.
TypeScript
1
star
62

sitemap-from-routes

Generate a sitemap directly from your Laravel routes/web.php.
PHP
1
star
63

cloak-algolia

Nuxt module for syncing records to Algolia and bootstrapping Instantsearch.js
JavaScript
1
star
64

vue-media-loader-directive

Vue Directive preloads media depending on pixel density and viewport size
CoffeeScript
1
star