javascript - 关闭居中 div 的不透明度

标签 javascript html css

我正在写一个出现在网站中心的框。为此,我动态 (js) 创建了两个元素 - 覆盖整个页面并具有 0.5 不透明度以显示某些网站的叠加层,以及一个不应具有不透明度的框。

问题是叠加层和盒子都有点透明。

this.createOverlay = function () {
            handler = document.createElement('div');
            handler.style.display = 'hidden';
            handler.style.width = '100%';
            handler.style.height = '100%';
            handler.style.top = 0;
            handler.style.left = 0;
            handler.style.position = 'absolute';
            handler.style.background = 'black';
            handler.style.color = "#aaaaaa";
            handler.style.opacity = "0.5";
            handler.style.filter = "alpha(opacity = 5)";
            return this;
        };
        this.createCenteredBox = function (width, height, url) {
            var data = JSON.parse(data);

            handler = document.createElement('a');
            handler.href = data.product_feed_deep_link;
            handler.target = "_blank";
            handler.style.display = "block";
            handler.style.width = width + "px";
            handler.style.height = height + "px";
            handler.style.position = "absolute";
            handler.style.color = "black";
            handler.style.backgroundColor = "#ffffff";
            handler.style.opacity = "1";
            handler.style.top = "50%";
            handler.style.left = "50%";
            handler.style.marginTop = "-" + height / 2 + "px";
            handler.style.marginLeft = "-" + width / 2 + "px";
            handler.style.padding = "0 10px 10px 10px";
            handler.style.borderRadius = "4px";
            var div = document.createElement('div');
            handler.appendChild(div);
            return this;
        };

这是代码,无论我将不透明度设置为 1 还是不透明度过滤器,我都无法关闭框的不透明度。

我该如何解决这个问题?

最佳答案

不透明度不是继承的(see here),但是,应用了不透明度属性的元素(后代)中的所有元素都将受到影响。

解决此问题的最佳方法是使用 rgba

handler.style.background = "rgba(0, 0, 0, .5)";  // RGB 0,0,0 is #000 (black).
//handler.style.opacity = "0.5";
//handler.style.filter = "alpha(opacity = 5)";

参见 2nd and 3rd answer here还有

关于javascript - 关闭居中 div 的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453941/

相关文章:

javascript - jquery的对话框模式可以改变模式内的页面吗?

javascript - 单击动态创建的链接时删除 div

html - 是否可以预览 Angular 模板?

html - 将 img/logo 居中并在 logo 周围制作导航栏元素

css - 设置大纲 :none for a, :hover 和 :active 但为 :focus 启用浏览器默认设置

javascript - 在对象数组上使用下划线的 “difference” 方法

JavaScript:将一组字符替换为另一组字符

javascript - HTML 表单 : display a text in an input (textarea)

javascript - 执行使用 python smtplib 发送的电子邮件的 Javascript

html - 浏览器滚动条全高