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

css - How to set box shadow via javascript? - Stack Overflow

programmeradmin0浏览0评论

I know you can do the following:

element.style.box-shadow ="something";

How would I cover -moz and -webkit extensions?

If I did element.style.-webkit-box-shadow="something" and element.style.-moz-box-shadow="something", would I be covered?

I know you can do the following:

element.style.box-shadow ="something";

How would I cover -moz and -webkit extensions?

If I did element.style.-webkit-box-shadow="something" and element.style.-moz-box-shadow="something", would I be covered?

Share Improve this question edited Sep 22, 2013 at 1:36 rink.attendant.6 46.2k63 gold badges110 silver badges157 bronze badges asked May 20, 2012 at 14:37 SamFisher83SamFisher83 3,9959 gold badges41 silver badges53 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

The option is called webkitBoxShadow, mozBoxShadow, etc. - to convert a CSS option name to a .style. property remove the leading dash and then convert it from lowercase-with-dashes to lowerCamelCase.

It would be better to set a class that declares those shadow properties for the various browsers and append that class to the className of the target element.

something like:

.shadowed{
    /*all shadow declarations for various browsers*/ 
    -o-
    -webkit-
    -moz-
    -ms-
    box-shadow
}

element.className += ' shadowed';
element.style['-webkit-box-shadow'] = "something";

Or in Mozilla:

element.style['-moz-box-shadow'] = "something";

you can try out this for text shadow . and for box shadow just replace text with box.

<html>
 <body>

  <p id="demo">Text shadow.</p>

   <button type="button" onclick="myFunction()">Add shadow to text</button>

   <script>
   function myFunction() {
   document.getElementById("demo").style.textShadow="2px 2px 4px green";
 }
 </script>

  </body>
发布评论

评论列表(0)

  1. 暂无评论