简体   繁体   English

在 CSS 中显示 Overlay 时如何删除所有滚动和单击事件

[英]How to remove all scrolling and click events when displaying Overlay in CSS

I have an Overlay that I display on a webpage while some SQL is performed in the background, this SQL can take a good few seconds, so I set this Overlay to be on the screen for 10 seconds using JavaScript.我有一个在网页上显示的叠加层,而一些 SQL 在后台执行,这个 SQL 可能需要几秒钟,所以我使用 Z686155AF75A60EDDDF6 将此叠加层设置为在屏幕上显示 10 秒What I want to do, is prevent the user from clicking, and scrolling altogether while this Overlay is visible.我想要做的是阻止用户在此叠加层可见时完全单击和滚动。

I have the pointer events set to 'none' which doesn't seem to work, even with a high z-index, and displaying as 'block'.我将指针事件设置为“无”,这似乎不起作用,即使 z-index 很高,并显示为“块”。 For some reason, when the Overlay is displayed, the user can still scroll, click, and highlight text just like the overlay is not even there.出于某种原因,当显示叠加层时,用户仍然可以滚动、单击和突出显示文本,就像叠加层不存在一样。

This is my Overlay in CSS:这是我在 CSS 中的叠加层:

    #overlay {

        position:fixed;
        display: block;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(120, 120, 120, 0.9);
        pointer-events: none;
        z-index:100000;
      
    } 

My JavaScript:我的 JavaScript:

  //The function to start displaying the overlay

    function start()
    {   
     document.getElementById("overlay").style.display = "block";        
    }


    //The function to stop displaying the overlay
    function end()
    {
            alert("Finished!");
            document.getElementById("overlay").style.display = "none";
    }

 

    setTimeout(function() { start(); }, 1);  //starting the Overlay
    setTimeout(function() { end(); }, 10000); //ending the Overlay after 10 seconds

You should disable scrolling by add overflow: hidden to body您应该通过添加overflow: hiddenbody

function start()
{   
   document.getElementById("overlay").style.display = "block";        
   document.getElementsByTagName("body")[0].style.overflow = "hidden";        
}


//The function to stop displaying the overlay
function end()
{
   alert("Finished!");
   document.getElementById("overlay").style.display = "none";
   document.getElementsByTagName("body")[0].style.overflow = null;        
}

Remove pointer-events: none to allow the user to click on the overlay, and add user-select:none;移除pointer-events: none以允许用户点击覆盖,并添加user-select:none; instead.反而。

This way, the user can not select anything on the overlay.这样,用户就不能 select 上覆盖任何东西。 The user will only be able to resume selection on the other parts of the page once the overlay is hidden.一旦覆盖层被隐藏,用户将只能在页面的其他部分恢复选择。

 function start() { document.getElementById("overlay").style.display = "block"; } //The function to stop displaying the overlay function end() { document.getElementById("overlay").style.display = "none"; console.log('Finished'); } setTimeout(function() { start(); }, 1); //starting the Overlay setTimeout(function() { end(); }, 10000); //ending the Overlay after 10 seconds
 #overlay { position: fixed; display: block; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(120, 120, 120, 0.9); z-index: 100000; user-select: none; overflow: hidden; /* not really needed, but this prevents scroll bars from appearing */ } #main { background-color: #bada55; display: inline-block; padding: 5em; margin: auto; }
 <div id="overlay"> <p> This is part of the overlay inner HTML </p> </div> <div id="main"> This is something not inside the overlay </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM