简体   繁体   English

用透明覆盖物覆盖div

[英]Cover a div with a transparent overlay

I have a page with a two column layout, where both columns scroll individually, and have dynamic size. 我有一个两列布局的页面,其中两列都单独滚动,并具有动态大小。 I would like to cover one of the columns in a transparent light grey div, so that it looks inactive. 我想覆盖透明浅灰色div中的一列,以使其看起来不活跃。

The layout is done using CSS grid layout. 布局使用CSS网格布局完成。

<div class="container">
  <div class="column">
    Left content
  </div>
  <div class="column">
    Right content
  </div>
</div>

Css: CSS:

.container {
  display: grid;
  grid-template-columns: calc(50%) calc(50%);
  grid-template-rows: 100%;
  height: 100px;
}

.column {
  overflow-y: scroll;
}

Here is a JSFiddle, but it will only work in Chrome, and you must enable "Experimental Web Platform features" in chrome://flags: https://jsfiddle.net/152on3bc/ (It will also work in Electron if you feel like setting that up) 这是一个JSFiddle,但它只能在Chrome中运行,你必须在chrome:// flags中启用“实验性Web平台功能”: https//jsfiddle.net/152on3bc/ (如果你觉得它也适用于Electron喜欢这样设置)

The desired effect would look like this (same caveats re: chrome://flags): https://jsfiddle.net/hawsfL9t/1/ but that one doesn't work once you scroll the left div. 期望的效果看起来像这样(相同的警告:chrome:// flags): https//jsfiddle.net/hawsfL9t/1/但是一旦你滚动左div就不起作用了。

Since this is for an electron app, any solution that only works in Chrome or Electron is fine. 由于这是一个电子应用程序,任何只适用于Chrome或Electron的解决方案都可以。 Using plain JavaScript is also fine, no need to be CSS only. 使用纯JavaScript也很好,不需要只是CSS。

You can add a class to the div which you want to show inactive. 您可以将一个类添加到要显示为非活动状态的div。

<div class="column inactive">

and in the CSS use: 并在CSS中使用:

.inactive {
    opacity: 0.5;
}

If you want to use colors, you can use: 如果要使用颜色,可以使用:

"Transparency using RGBA". “使用RGBA的透明度”。

*Updated the post. *更新了帖子。

Hope it helps. 希望能帮助到你。 :) :)

 .container { display: grid; grid-template-columns: calc(50%) calc(50%); grid-template-rows: 100%; height: 100px; } .column { overflow-y: scroll; } .left { float: left; width: 50%; } .right { float: right; width: 50%; } .inactive { background: rgba(76, 175, 80, 0.5); opacity: 0.5; } 
 <div class="container"> <div class="column left"> <p>Left content<br><br> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec massa enim. Aliquam viverra at est ullamcorper sollicitudin. Proin a leo sit amet nunc malesuada imperdiet pharetra ut eros.</p> </div> <div class="column right inactive"> <p>Right content<br><br> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec massa enim. Aliquam viverra at est ullamcorper sollicitudin. Proin a leo sit amet nunc malesuada imperdiet pharetra ut eros.</p> </div> </div> 

You can create overlay class where you want to show disabled or inactive as 您可以创建要在其中显示已禁用或非活动的覆盖类

<div class="overlay">

and style as 和风格一样

.overlay{
    opacity: 0.5;
    pointer-events: none;
}

Regards. 问候。

There is one very usefull jquery plugin available for wrapping div with transparent div it is jQuery BlockUI Plugin 有一个非常有用的jquery插件可用于使用透明div包装div它是jQuery BlockUI插件

by using this plugin you can apply block ui in any element as mentioned below 通过使用此插件,您可以在任何元素中应用块ui,如下所述

Blocking UI 阻止UI

  $('div.test').block({ 
                    message: '<h1>Processing</h1>', 
                    css: { border: '3px solid #a00' } 
                }); 

Unblocking UI 解锁UI

$('div.test').unblock(); 

or you can add a class inactive. 或者您可以添加一个不活动的类。

<div class="column inactive">

In css: .inactive { background:rgba(255,255,255,0.5); } 在css中: .inactive { background:rgba(255,255,255,0.5); } .inactive { background:rgba(255,255,255,0.5); }

and remove all the text section background colors. 并删除所有文本部分背景颜色。

You can use the absolute positioned idea you were working with, and use JavaScript to set the height of the overlay to the height of the scrolled content. 您可以使用您正在使用的绝对定位的想法,并使用JavaScript将叠加的高度设置为滚动内容的高度。

 var h = document.getElementById('column2').scrollHeight; var block = document.getElementById('block'); block.style.height = h + 'px'; 
 .container { display: grid; grid-template-columns: calc(50%) calc(50%); grid-template-rows: 100%; height: 100px; } .column { overflow-y: scroll; position: relative; } .block { position: absolute; background: pink; opacity: 0.5; width:100%; } 
 <div class="container"> <div class="column"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin blandit urna a purus consequat tristique. Nam sed quam rhoncus, bibendum lacus vel, bibendum ligula. Maecenas semper, libero sit amet pretium ultrices, lorem dui laoreet felis, sed elementum libero orci eget risus. Sed sed mi enim. Ut sit amet est non metus ultrices tristique vel at velit. Praesent ultrices, libero rhoncus congue porta, metus elit pellentesque lectus, id blandit erat sem id nunc. Sed pretium auctor sapien, et molestie dolor aliquet et. Etiam pulvinar tristique nisi, et condimentum neque maximus ut. Integer quis ex lorem. Suspendisse nec arcu ut elit dictum consequat ac ut nibh. Praesent pretium consequat nulla id dictum. Vivamus lobortis ullamcorper nibh, a fringilla ligula interdum vitae. Cras dignissim, mi a hendrerit posuere, mi arcu lacinia dui, placerat ullamcorper dui arcu in ante. </div> <div class="column" id="column2"> <div id="block" class="block"> </div> Suspendisse consequat commodo egestas. In consequat augue vel diam fermentum vehicula. Mauris faucibus arcu quam, vitae finibus nulla facilisis non. In ultricies est a justo consectetur, nec volutpat justo varius. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc suscipit gravida varius. Quisque at tempus nulla, ut mattis eros. </div> </div> 

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

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