【发布时间】:2012-01-13 16:04:31
【问题描述】:
我不仅想检测过渡支持,还想通过一个函数调用设置正确的前缀。这样做会有什么明显的问题吗?
function getTransitionPrefix() {
var el = document.getElementsByTagName("body")[0],
cssDec = (typeof window.getComputedStyle === "undefined") ? {} : window.getComputedStyle(el,null),
transition = typeof cssDec.WebkitTransition !== "undefined" ? "Webkit" :
typeof cssDec.MozTransition !== "undefined" ? "Moz":
typeof cssDec.msTransition !== "undefined" ? "ms" :
typeof cssDec.OTransition !== "undefined" ? "O" : false;
return transition;
}
【问题讨论】:
-
不确定您到底想做什么,但浏览器特定的 CSS 属性前缀是:
-webkit-,-moz-,-ms-,-o-。 -
@Strelok 在 JavaScript 中,变量不能有破折号,所以它们被设为
Webkit、Moz、ms、O。 -
@WilliamVanRensselaer,是的,这是正确的,但他正在做的是将字符串作为前缀返回。我想不可能知道他将使用它们做什么,因为他没有提供任何代码。 OP 在问什么,这甚至不是一个真正的问题。
-
@Strelok 他可能打算通过 JavaScript 使用前缀字符串来设置转换...
elem.style[ pref + "Transition" ] = "transition settings"; -
@WilliamVanRensselaer 我打算以这种方式设置元素样式。我在使用
style属性时遇到了问题,而不是使用getComputedStyle,但我不再看到它了。为了在元素CSSStyleDeclaration中查找键,我想这两种方法在功能上是等效的。那是我的 OP 问题的一部分,以及我的“棘手”三元运算符(回想起来看起来很乱)是否站得住脚。谢谢,我认为你的回答做得很好。
标签: javascript css cross-browser css-transitions