Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the -webkit- vendor prefix in JavaScript

If I were writing a JavaScript line to set a style attribute of an element it could look like this (this example: "width"):

document.getElementById('myDiv').style.width="50px";

and if there is a dash in the CSS element it would look like this (this example: "margin-top"):

document.getElementById('myDiv').style.marginTop="15px";

But how do I access the prefix -webkit-, if I want to give it a style like this example:

{-webkit-transition: width 1s;}
like image 929
lindhe Avatar asked Feb 24 '12 22:02

lindhe


2 Answers

You have two options:

  • style["-webkit-transition"]
  • style.WebkitTransition

The first directly works. The second notation is called camel case, and foo-bar-baz becomes fooBarBaz. As a result, when a non camel case string starts with -, the first letter is capitalized in camel case.

like image 190
pimvdb Avatar answered Sep 30 '22 15:09

pimvdb


One possibility would to use for example jquery, to make it easy. If you want a pure javascript solution, then read this: http://www.javascriptkit.com/javatutors/setcss3properties.shtml

like image 31
Sven Bieder Avatar answered Sep 30 '22 15:09

Sven Bieder