Path: blob/trunk/third_party/closure/goog/fx/css3/transition.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview CSS3 transition base library.16*17* @author [email protected] (Chris Henry)18*/1920goog.provide('goog.fx.css3.Transition');2122goog.require('goog.Timer');23goog.require('goog.asserts');24goog.require('goog.fx.TransitionBase');25goog.require('goog.style');26goog.require('goog.style.transition');27282930/**31* A class to handle targeted CSS3 transition. This class32* handles common features required for targeted CSS3 transition.33*34* Browser that does not support CSS3 transition will still receive all35* the events fired by the transition object, but will not have any transition36* played. If the browser supports the final state as set in setFinalState37* method, the element will ends in the final state.38*39* Transitioning multiple properties with the same setting is possible40* by setting Css3Property's property to 'all'. Performing multiple41* transitions can be done via setting multiple initialStyle,42* finalStyle and transitions. Css3Property's delay can be used to43* delay one of the transition. Here is an example for a transition44* that expands on the width and then followed by the height:45*46* <pre>47* var animation = new goog.fx.css3.Transition(48* element,49* duration,50* {width: 10px, height: 10px},51* {width: 100px, height: 100px},52* [53* {property: width, duration: 1, timing: 'ease-in', delay: 0},54* {property: height, duration: 1, timing: 'ease-in', delay: 1}55* ]56* );57* </pre>58*59* @param {Element} element The element to be transitioned.60* @param {number} duration The duration of the transition in seconds.61* This should be the longest of all transitions.62* @param {Object} initialStyle Initial style properties of the element before63* animating. Set using {@code goog.style.setStyle}.64* @param {Object} finalStyle Final style properties of the element after65* animating. Set using {@code goog.style.setStyle}.66* @param {goog.style.transition.Css3Property|67* Array<goog.style.transition.Css3Property>} transitions A single CSS368* transition property or an array of it.69* @extends {goog.fx.TransitionBase}70* @constructor71* @struct72*/73goog.fx.css3.Transition = function(74element, duration, initialStyle, finalStyle, transitions) {75goog.fx.css3.Transition.base(this, 'constructor');7677/**78* Timer id to be used to cancel animation part-way.79* @private {number}80*/81this.timerId_;8283/**84* @type {Element}85* @private86*/87this.element_ = element;8889/**90* @type {number}91* @private92*/93this.duration_ = duration;9495/**96* @type {Object}97* @private98*/99this.initialStyle_ = initialStyle;100101/**102* @type {Object}103* @private104*/105this.finalStyle_ = finalStyle;106107/**108* @type {Array<goog.style.transition.Css3Property>}109* @private110*/111this.transitions_ = goog.isArray(transitions) ? transitions : [transitions];112};113goog.inherits(goog.fx.css3.Transition, goog.fx.TransitionBase);114115116/** @override */117goog.fx.css3.Transition.prototype.play = function() {118if (this.isPlaying()) {119return false;120}121122this.onBegin();123this.onPlay();124125this.startTime = goog.now();126this.setStatePlaying();127128if (goog.style.transition.isSupported()) {129goog.style.setStyle(this.element_, this.initialStyle_);130// Allow element to get updated to its initial state before installing131// CSS3 transition.132this.timerId_ = goog.Timer.callOnce(this.play_, undefined, this);133return true;134} else {135this.stop_(false);136return false;137}138};139140141/**142* Helper method for play method. This needs to be executed on a timer.143* @private144*/145goog.fx.css3.Transition.prototype.play_ = function() {146// This measurement of the DOM element causes the browser to recalculate its147// initial state before the transition starts.148goog.style.getSize(this.element_);149goog.style.transition.set(this.element_, this.transitions_);150goog.style.setStyle(this.element_, this.finalStyle_);151this.timerId_ = goog.Timer.callOnce(152goog.bind(this.stop_, this, false), this.duration_ * 1000);153};154155156/** @override */157goog.fx.css3.Transition.prototype.stop = function() {158if (!this.isPlaying()) return;159160this.stop_(true);161};162163164/**165* Helper method for stop method.166* @param {boolean} stopped If the transition was stopped.167* @private168*/169goog.fx.css3.Transition.prototype.stop_ = function(stopped) {170goog.style.transition.removeAll(this.element_);171172// Clear the timer.173goog.Timer.clear(this.timerId_);174175// Make sure that we have reached the final style.176goog.style.setStyle(this.element_, this.finalStyle_);177178this.endTime = goog.now();179this.setStateStopped();180181if (stopped) {182this.onStop();183} else {184this.onFinish();185}186this.onEnd();187};188189190/** @override */191goog.fx.css3.Transition.prototype.disposeInternal = function() {192this.stop();193goog.fx.css3.Transition.base(this, 'disposeInternal');194};195196197/**198* Pausing CSS3 Transitions in not supported.199* @override200*/201goog.fx.css3.Transition.prototype.pause = function() {202goog.asserts.assert(false, 'Css3 transitions does not support pause action.');203};204205206