How Can I Efficiently Apply Vendor Prefixes to CSS Properties with JavaScript?

DDD
Release: 2024-11-18 19:32:02
Original
1029 people have browsed it

How Can I Efficiently Apply Vendor Prefixes to CSS Properties with JavaScript?

Setting Vendor-Prefixed CSS using Javascript

Manually applying vendor prefixes to CSS properties can be tedious. The provided code snippet demonstrates this process for the transform property:

var transform = 'translate3d(0,0,0)';
elem.style.webkitTransform = transform;
elem.style.mozTransform = transform;
elem.style.msTransform = transform;
elem.style.oTransform = transform;
Copy after login

Is there a more efficient way to achieve this, preferably with a single line of JavaScript?

Solution

While there isn't a known library that performs this task, creating a custom function is straightforward since vendor prefixes are consistent in their syntax and names.

function setVendor(element, property, value) {
  element.style["webkit" + property] = value;
  element.style["moz" + property] = value;
  element.style["ms" + property] = value;
  element.style["o" + property] = value;
}
Copy after login

This function can then be used to set vendor-prefixed properties:

setVendor(elem, 'transform', 'translate3d(0,0,0)');
Copy after login

The above is the detailed content of How Can I Efficiently Apply Vendor Prefixes to CSS Properties with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template