javascript - jQuery 到常规 Javascript - 覆盖

标签 javascript jquery popup overlay

我使用以下 jQuery 代码为我的网站创建了某种覆盖层。

$(function() {
var docHeight = $(document).height();
$("body").append("<div id='overlay'></div><div id='my_popup'></div>");
$("#overlay")
  .height(docHeight)
  .css({
     'opacity' : 0.4,
     'position': 'absolute',
     'top': 0,
     'left': 0,
     'background-color': 'black',
     'width': '100%',
     'z-index': 5000
  });
});

我想使用常规 javascript 产生相同的结果,没有 jquery,我将如何开始/进行此操作?

此外,我当前使用的代码会导致覆盖层在每个页面上重新加载,因为它应该只加载在主页或登陆页面上。有人可以建议解决这个问题吗?

最佳答案

直接从 jQuery 转换为 Vanilla:

(function() {
    if (window.location.href.split('/').pop() == "yourlandingpage") {
        var body = document.body,
            html = document.documentElement,
            docHeight = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight,             html.scrollHeight, html.offsetHeight );

        body.innerHTML += "<div id='overlay'></div><div id='my_popup'></div>";
        document.getElementById("overlay").style.height = docHeight + "px";
        document.getElementById("overlay").className = "NewClass";
    }
}());

然后声明CSS:

.NewClass {
    opacity: 0.4;
    position: absolute;
    top: 0;
    left: 0;
    background-color: black;
    width: 100%;
    z-index: 5000;
}

<强> JSFiddle Example

关于javascript - jQuery 到常规 Javascript - 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976556/

相关文章:

javascript - 这个脚本标签是什么?

html - jqm 中的全局弹出窗口

javascript - 如何从两个不同的页面引用弹出窗口?

javascript - 谷歌地图获取 Pane

javascript - Python 服务器 Websocket 握手响应

javascript - 禁止在悬停时打开 jquery 菜单中的子菜单

jquery - 这在 jquery $ ('#id' , javascript_object) 中意味着什么;

javascript - 如何注册 document.onkeypress 事件

javascript - 如何选择这种格式的动态 ID?

android - 同时出现多个对话框