简体   繁体   English

切换半透明组件覆盖div

[英]Toggle covering a div with semi-transparent component

I am wondering how to cover a <div> with some useful HTML component/image to make the <div> : 我想知道如何用一些有用的HTML组件/图像覆盖<div>来制作<div>

  • A) Unclickable (unreachable for mouse etc) A)不可点击(鼠标等无法访问)
  • B) Look a little ~0.3f blur (because of it transparent foreground) B)看起来约0.3f模糊(因为它是透明的前景)
  • C) make the effect dynamic I mean make the <div> covered/uncovered by some event? C)使效果动态化我的意思是使<div>被某些事件覆盖/未被覆盖?

So my question is: What is the optimal way to make the previously mentioned effect with HTML, CSS, and Javascript? 所以我的问题是:用HTML,CSS和Javascript产生上述效果的最佳方法是什么?

(A) Making the div "Unclickable (unreachable for mouse etc)" (A)将div设置为“不可点击(鼠标等无法访问)”

This can be accomplished by using the various CSS positioning elements to place two divs in the exact same location. 这可以通过使用各种CSS定位元素将两个div放置在完全相同的位置来实现。 Then to stack one div on top of the other, use z-index (the element with the higher z-index will be stacked on top of the element with the lower z-index ). 然后将一个div叠放在另一个div上,请使用z-index (具有较高z-index的元素将堆叠在具有较低z-index的元素之上)。 For example: 例如:

HTML: HTML:

<div id="div1">
    <a href="#">Link</a>
</div>
<div id="div2"></div>

CSS: CSS:

#div1, #div2 {
    width: 50px;
    height: 50px;
    top: 0;
    left: 0;
    position: fixed;
}

#div2 {
    z-index: 1;
}

(B) Making the div "Look a little ~0.3f blur (because of it transparent foreground)" (B)使div“看起来有点〜0.3f模糊(因为它是透明的前景)”

To do this, you would use CSS opacity . 为此,您将使用CSS opacity For example: 例如:

#div2 {
    opacity: .3;
}

(C) "Make the effect dynamic I mean make the covered/uncovered by some event." (C)“使效果具有动态性,我的意思是使某些事件覆盖/不显示”。

You can accomplish this using Javascript. 您可以使用Javascript完成此操作。 The simplest approach would probably be to use document.getElementById to add display:none to the div you want to remove. 最简单的方法可能是使用document.getElementByIddisplay:none添加到要删除的div中。

<script>
  document.getElementById('div2').style.display = 'none';
</script>

NOTE : These are not the only ways to accomplish this task. 注意 :这些不是完成此任务的唯一方法。 I am just trying to give you a launching point. 我只是想给您一个出发点。 I recommend reviewing the MDN links I posted and writing code that works best for what you are specifically trying to accomplish. 我建议您查看我发布的MDN链接,并编写最适合您要尝试完成的代码。

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

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