- 稳定的 Firefox 版本使用 W3C 标准的无前缀
transition-property
CSS 属性;
- 你必须在 JS 样式中应用驼峰式 CSS 属性;
- 您正在尝试将样式设置为 nodeList,这是DOM QSA 方法返回的内容。您需要对其进行迭代。
对于跨浏览器解决方案,使用jQuery会很简单:
$('p').css({transitionProperty: "all", transitionDuration: '2s'});
见小提琴
如果你想要一个纯 JS 解决方案,下面是测试支持的 CSS 属性的代码$.cssHooks
:
function styleSupport(prop) {
var vendorProp, supportedProp,
// capitalize first character of the prop to test vendor prefix
capProp = prop.charAt(0).toUpperCase() + prop.slice(1),
prefixes = ["Moz", "Webkit", "O", "ms"],
div = document.createElement("div");
if (prop in div.style) {
// browser supports standard CSS property name
supportedProp = prop;
} else {
// otherwise test support for vendor-prefixed property names
for (var i = 0; i < prefixes.length; i++) {
vendorProp = prefixes[i] + capProp;
if (vendorProp in div.style) {
supportedProp = vendorProp;
break;
}
}
}
// avoid memory leak in IE
div = null;
// add property to $.support so it can be accessed elsewhere
//$.support[prop] = supportedProp;
return supportedProp;
}
然后就这样使用它:
(function() {
var transitionProperty = styleSupport("transitionProperty");
var transitionDuration = styleSupport("transitionDuration");
var actor_object = document.querySelectorAll("p");
for (var i = 0, l = actor_object.length; i < l; i++) {
actor_object[i].style[transitionProperty] = "all";
actor_object[i].style[transitionDuration] = "2s";
}
}());
小提琴