javascript - 如何降低 CSS 不透明度

标签 javascript css opacity

我需要纯 javascript 函数来找到 HTML 元素的当前不透明度并将其降低 10%。有什么有效的跨浏览器解决方案吗?

请不要使用像 jQuery 这样的 JS 框架(我有我的理由所以不要问为什么),谢谢。

最佳答案

使用 IE 变体

var style = document.getElementById(...).style;
if(browserIsIE) {
  var regExpResult = /alpha\(opacity\s*=\s*(\d*)\s*\)/.exec(style.filter);
  var opacity;
  if(regExpResult && regExpResult.constructor == Array && regExpResult[1] && opacity = parseInt(regExpResult[1])) {
    style.filter = "alpha(opacity = " + (opacity - 10) + ")";
  } else {
    style.filter = "alpha(opacity = 90)"; 
  }
} else {
  style.opacity = style.opacity=='' ? 0.9 : parseFloat(style.opacity)-0.1;
}

关于javascript - 如何降低 CSS 不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235227/

相关文章:

html - 如何更改 HTML/CSS 中的文本透明度?

html - 无法控制div内图像的不透明度

css - 如何使用 CurrentColor 设置边框的不透明度?

javascript - knockout 复杂数据模型绑定(bind)

html - 如何对齐图像和无序列表

jquery - 为什么 Chrome 无法正确处理使用 jQuery 添加的 flexbox 元素?

CSS - 嵌套 anchor 标记链接

javascript - javascript 中的 pageShow 事件

javascript - 搜索在 Gridview 上不起作用

javascript - 无法使用 javascript 或 jquery 将 '</div> <div>' 插入 html 页面中的位置