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

CSS Cross-browser opacity from Javascript - Stack Overflow

programmeradmin0浏览0评论

I want to turn down the opacity on an element when a user performs an action.

The current code I have is:

document.getElementById('MyOpaqueElement').style.opacity = 0.3;
// There are checks in the real code for NULL, etc.

This works on Firefox, Safari, etc. IE8 has different ideas about opacity. I have read a couple of articles but have yet to find a definitive answer on the most portable method to do this in a cross-browser way.

I want to turn down the opacity on an element when a user performs an action.

The current code I have is:

document.getElementById('MyOpaqueElement').style.opacity = 0.3;
// There are checks in the real code for NULL, etc.

This works on Firefox, Safari, etc. IE8 has different ideas about opacity. I have read a couple of articles but have yet to find a definitive answer on the most portable method to do this in a cross-browser way.

Share Improve this question edited Jan 20, 2010 at 18:05 Timothy S. Van Haren 8,9662 gold badges31 silver badges34 bronze badges asked Jan 20, 2010 at 18:00 Loki AstariLoki Astari 265k86 gold badges342 silver badges572 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

There are various browsers-specific settings and notations you need to take into consideration.

See Quirksmode: CSS2 - Opacity

I suggest using a Framework like JQuery, Prototype, MooTools or Dojo. I know it seems ridiculous to add dozens of kilobytes of code just for some opacity at first, but it's really worth it. It just works in one way for all browsers.

EDIT- Poster is using jquery..

Easy way:

  $(el).css('opacity', '0.3');

(I checked the jquery sources, and it handles opacity for cross-browser patibility automatically, so that should work)

Or for a CSS solution: Just give it a class, e.g. 'transparent', and add this to your CSS file:

.transparent {
    filter: alpha(opacity=30); /* for IE */
    -khtml-opacity: 0.3;      /* khtml, old safari */
    -moz-opacity: 0.3;       /* old mozilla, netscape */
    opacity: 0.3;           /* good browsers: FF, safari, opera */
}

The equivalent should be document.getElementById('MyOpaqueElement').style.filter = 'alpha(opacity=30)';

By the way, even if you don't use a library like YUI or JQuery, you can download them and search their sources for the word

opacity

.

发布评论

评论列表(0)

  1. 暂无评论