除了固定区域外,整个屏幕都变暗?


88

我想创建一个教程,它将引导用户确切地单击。我试图覆盖整个屏幕,<div>这会变得暗淡,除了一个所有元素特定区域是在一个固定的widthheighttopleft

问题是,我找不到“取消”父母的方法background-color(也是透明的)。

在下面的片段中,hole是应该没有任何div的div background-color,包括其父级的div 。

能做到吗?有任何想法吗?

#bg{
   background-color:gray;
   opacity:0.6;
   width:100%;
   height:100vh;
}
#hole{
   position:fixed;
   top:100px;
   left:100px;
   width:100px;
   height:100px;
}
<div id="bg">
    <div id="hole"></div>
</div>

这是我要达到的目标的模型图:

在此处输入图片说明


13
如果您要创建功能导览/教程,则可能对其中的js库感兴趣。它们具有一些漂亮的功能,可以省去编写大量一次性CSS效果的麻烦。查看introjs.comlinkedin.github.io/hopscotch
octern

2
这只是一个关于lib的建议:footerhook.github.io/chardin.js
在线Thomas

Answers:


125

您只用一个就可以完成,div然后给它一个box-shadow

编辑: 正如@Nick Shvelidze指出的那样,您应该考虑添加pointer-events: none

新增vmax的价值box-shadow为@Prinzhorn建议

div {
  position: fixed;
  width: 200px;
  height: 200px;
  top: 50px;
  left: 50px;
  /* for IE */
  box-shadow: 0 0 0 1000px rgba(0,0,0,.3);
  /* for real browsers */
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.3);
  pointer-events: none;
}
<div></div>


28
pointer-events: none如果您要单击其下方的区域,请确保添加。
Nick Shvelidze

4
1,000px在我看来似乎还不够-似乎在很多情况下,这种情况无法显示在屏幕上,否则确实是一个聪明的解决方案。
ESR

1
@EdmundReed,您可以根据自己的需要进行调整:)
VilleKoo

8
box-shadow: 0 0 0 100vmax rgba(0,0,0,.3);将保证覆盖整个屏幕
Prinzhorn's

2
@VilleKoo,您可以将其添加到1000px的行后面,并作为回退
Prinzhorn

19

您可以创建一个SVG,该SVG填充有一条路径,该路径在需要的地方带有孔。但是我想您需要找到一种处理点击的方法,因为所有点击都将针对重叠的svg。我document.getElementFromPoint可以在这里为您提供帮助。dn


我认为谷歌反馈表使用SVG调暗休息区。
杜尔加

3

也可以类似于@VilleKoo的答案来完成此操作,但要带边框。

div {
    border-style: solid;
    border-color: rgba(0,0,0,.3);
    pointer-events: none;
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0;
    border-width: 40px 300px 50px 60px;
}
<div></div>


2

您可以使用CSS实现与VilleKoo的回答相同的效果outline属性。它具有出色的浏览器支持(并且也可以在IE8 +中使用)。轮廓的语法与边框相同,但与边框不同,它们不占用空间,而是在内容上方绘制。

对于IE9 +,您也可以替换99999pxcalc(100 * (1vh + 1vw - 1vmin))

这种方法的缺点outline是不受border-radius

演示:

div {
  position: fixed;
  width: 200px;
  height: 200px;
  top: 50px;
  left: 50px;
  /* IE8 */
  outline: 99999px solid rgba(0,0,0,.3);
  /* IE9+ */
  outline: calc(100 * (1vw + 1vh - 1vmin)) solid rgba(0,0,0,.3);
  /* for other browsers */
  outline: 100vmax solid rgba(0,0,0,.3);
  pointer-events: none;
}
<div></div>


0

这是使用@VilleKoo CSS的简单jQuery代码

var Dimmer = (function(){
  var el = $(".dimmer");
  
  return {
    showAt: function(x, y) {
      el
        .css({
          left: x,
          top: y,
        })
        .removeClass("hidden");
    },
    
    hide: function() {
      el.addClass("hidden");
    }
  }
}());


$(".btn-hide").click(function(){ Dimmer.hide(); });
$(".btn-show").click(function(){ Dimmer.showAt(50, 50); });
.dimmer {
  position: fixed;
  width: 200px;
  height: 200px;
  top: 50px;
  left: 50px;
  box-shadow: 0 0 0 1000px rgba(0,0,0,.3); /* for IE */
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.3); /* for real browsers */
  pointer-events: none;
}

.hidden { display: none; }
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <div>
    <input type="text">
    <select name="" id=""></select>
    <input type="checkbox">
  </div>
  <div>
    <input type="text">
    <select name="" id=""></select>
    <input type="checkbox">
  </div>
  <div>
    <input type="text">
    <select name="" id=""></select>
    <input type="checkbox">
  </div>
  <div>
    <input type="submit" disabled>
  </div>
  
  <input type="button" value="Hide" class="btn-hide">
  <input type="button" value="Show" class="btn-show">
  <div class="dimmer hidden"></div>
  <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</body>
</html>


0
#bg {
   background-color: gray;
   opacity: 0.6;
   position: absolute;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   z-index: 99998;
}
#hole {
   position: fixed;
   top: 100px;
   left: 100px;
   width: 100px;
   height: 100px;
   z-index: 99999;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.