最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Access the -webkit- vendor prefix in JavaScript - Stack Overflow

programmeradmin0浏览0评论

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;}

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;}
Share Improve this question edited Jun 15, 2015 at 2:32 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Feb 24, 2012 at 22:26 lindhelindhe 8894 gold badges15 silver badges33 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 25

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.

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

If you don't want to keep digging up those pesky style properties and their naming conventions you can always use jQuery to keep it simple.

$('#myDiv').css("-webkit-transition", "value");
发布评论

评论列表(0)

  1. 暂无评论