transition.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility methods to deal with CSS3 transitions
  16. * programmatically.
  17. * @author chrishenry@google.com (Chris Henry)
  18. */
  19. goog.provide('goog.style.transition');
  20. goog.provide('goog.style.transition.Css3Property');
  21. goog.require('goog.array');
  22. goog.require('goog.asserts');
  23. goog.require('goog.dom');
  24. goog.require('goog.dom.TagName');
  25. goog.require('goog.dom.safe');
  26. goog.require('goog.dom.vendor');
  27. goog.require('goog.functions');
  28. goog.require('goog.html.SafeHtml');
  29. goog.require('goog.style');
  30. goog.require('goog.userAgent');
  31. /**
  32. * A typedef to represent a CSS3 transition property. Duration and delay
  33. * are both in seconds. Timing is CSS3 timing function string, such as
  34. * 'easein', 'linear'.
  35. *
  36. * Alternatively, specifying string in the form of '[property] [duration]
  37. * [timing] [delay]' as specified in CSS3 transition is fine too.
  38. *
  39. * @typedef { {
  40. * property: string,
  41. * duration: number,
  42. * timing: string,
  43. * delay: number
  44. * } | string }
  45. */
  46. goog.style.transition.Css3Property;
  47. /**
  48. * Sets the element CSS3 transition to properties.
  49. * @param {Element} element The element to set transition on.
  50. * @param {goog.style.transition.Css3Property|
  51. * Array<goog.style.transition.Css3Property>} properties A single CSS3
  52. * transition property or array of properties.
  53. */
  54. goog.style.transition.set = function(element, properties) {
  55. if (!goog.isArray(properties)) {
  56. properties = [properties];
  57. }
  58. goog.asserts.assert(
  59. properties.length > 0, 'At least one Css3Property should be specified.');
  60. var values = goog.array.map(properties, function(p) {
  61. if (goog.isString(p)) {
  62. return p;
  63. } else {
  64. goog.asserts.assertObject(p, 'Expected css3 property to be an object.');
  65. var propString =
  66. p.property + ' ' + p.duration + 's ' + p.timing + ' ' + p.delay + 's';
  67. goog.asserts.assert(
  68. p.property && goog.isNumber(p.duration) && p.timing &&
  69. goog.isNumber(p.delay),
  70. 'Unexpected css3 property value: %s', propString);
  71. return propString;
  72. }
  73. });
  74. goog.style.transition.setPropertyValue_(element, values.join(','));
  75. };
  76. /**
  77. * Removes any programmatically-added CSS3 transition in the given element.
  78. * @param {Element} element The element to remove transition from.
  79. */
  80. goog.style.transition.removeAll = function(element) {
  81. goog.style.transition.setPropertyValue_(element, '');
  82. };
  83. /**
  84. * @return {boolean} Whether CSS3 transition is supported.
  85. */
  86. goog.style.transition.isSupported = goog.functions.cacheReturnValue(function() {
  87. // Since IE would allow any attribute, we need to explicitly check the
  88. // browser version here instead.
  89. if (goog.userAgent.IE) {
  90. return goog.userAgent.isVersionOrHigher('10.0');
  91. }
  92. // We create a test element with style=-vendor-transition
  93. // We then detect whether those style properties are recognized and
  94. // available from js.
  95. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  96. var transition = 'opacity 1s linear';
  97. var vendorPrefix = goog.dom.vendor.getVendorPrefix();
  98. var style = {'transition': transition};
  99. if (vendorPrefix) {
  100. style[vendorPrefix + '-transition'] = transition;
  101. }
  102. goog.dom.safe.setInnerHtml(
  103. el, goog.html.SafeHtml.create('div', {'style': style}));
  104. var testElement = /** @type {Element} */ (el.firstChild);
  105. goog.asserts.assert(testElement.nodeType == Node.ELEMENT_NODE);
  106. return goog.style.getStyle(testElement, 'transition') != '';
  107. });
  108. /**
  109. * Sets CSS3 transition property value to the given value.
  110. * @param {Element} element The element to set transition on.
  111. * @param {string} transitionValue The CSS3 transition property value.
  112. * @private
  113. */
  114. goog.style.transition.setPropertyValue_ = function(element, transitionValue) {
  115. goog.style.setStyle(element, 'transition', transitionValue);
  116. };