【问题标题】:Is a see-through child div possible?可以透视子div吗?
【发布时间】:2019-03-06 12:54:05
【问题描述】:

图像是祖父 div,黑色半透明叠加层是父 div,裁剪部分是子 div。用户将看到祖父图像和父叠加层,然后他可以使用子裁剪器 div 裁剪它。我尝试使用 opacity 和 rgba 背景失败了。

这些疯狂的方法似乎对我有用 -

  1. 在子div的背景中设置祖父图像,然后更改background-position的x/y。
  2. 将 child 和 parent 合并到一个 div 中,并使用 rgba border 作为覆盖(我朋友的建议)。
  3. 在 stackoverflow 上找到 this,它使用 box-shadow 而不是边框​​,似乎与 #2 类似。

我对 #2 和 #3 的小不满是我需要为虚线边框添加另一个 div,以便用户清楚地知道他在裁剪什么。但我对所有这些方法的最大不满是,这些方法看起来都不像 正确 方法。

有没有合适的/更好的/2018-ish/“这很明显,你这个白痴”的方式来做到这一点?

更新:这是基本标记(如果这有助于解决这个问题,我也可以使用不同的标记)

#grandparentImage {
  background: url(https://9to5mac.com/wp-content/uploads/sites/6/2018/07/Desert-2.jpg) no-repeat;
  background-size: cover;
  position: relative;
  height: 500px;
}

#parentOverlay {
  background: rgba(0,0,0,0.5);
  height: 100%;
  position: relative;
}

#childCropper {
  border: 1px dashed #ccc;
  left: 50px;
  height: 100px;
  width: 100px;
  position: absolute;
  top: 50px;
}
<div id="grandparentImage">
  <div id="parentOverlay">
    <div id="childCropper"></div>
  </div>
</div>

编辑:是not a duplicate of the marked question,因为这个问题涉及如何抓取裁剪后的图像,所以这个问题涉及如何向用户展示他正在裁剪的内容。更多关于 UI 而非数据。

【问题讨论】:

  • 你能提供一些基本的标记以便我们可以用它来测试吗?
  • 你可以使用两个div,一个作为背景(不透明度低),一个在前面,背景位置只显示被选中的部分
  • 您使用两个图像的第一种方法是该任务的众所周知的解决方案,并在大多数裁剪库中使用。另一种解决方案是使用画布,但这里还有很多工作
  • 您可以尝试clip-path: inset() 或clip-path: polygon 以获得更高级的形状。

标签: javascript html css


【解决方案1】:

覆盖 div(概念证明)

.parent,
.child {
  background-image: url(https://scontent-lht6-1.cdninstagram.com/vp/0f18c710d8dc3ebd48819b3f9f44b5cc/5C28EE7E/t51.2885-15/e35/29094825_1798384780455300_8914767740305145856_n.jpg?se=7&ig_cache_key=MTc0MDQ5MzIwMjE5OTYyODM5MQ%3D%3D.2);
  background-size: contain;
}

.parent {
  height: 1072px;
  width: 1072px;
  opacity: 0.3
}

.child {
  position: absolute;
  top: 150px;
  left: 20px;
  height: 200px;
  width:500px;
  background-position: -20px -150px; 
  background-size: 1072px 1072px
}
<div class="parent"></div>
<div class="child"></div>

【讨论】:

  • 感谢 Dementic,这看起来与我在问题 #1 中列出的方法相同。我正在寻找一种更好的 方法。我看到您已将父母和孩子合二为一,但它仍然是一种类似的方法,因为它在父母/孩子技巧中使用了相同的图像。
【解决方案2】:

这对于伪元素来说似乎是一个完美的工作。所以这个解决方案是问题中#2建议的升级,但不是使用元素本身,而是使用:after:

#grandparentImage {
  background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/%D0%94%D0%B7%D0%B5%D0%BC%D0%B1%D1%80%D0%BE%D0%BD%D1%8F._%D0%9F%D0%B5%D1%80%D0%B2%D1%8B%D0%B5_%D0%BB%D1%83%D1%87%D0%B8_%D1%81%D0%BE%D0%BB%D0%BD%D1%86%D0%B0.jpg/800px-%D0%94%D0%B7%D0%B5%D0%BC%D0%B1%D1%80%D0%BE%D0%BD%D1%8F._%D0%9F%D0%B5%D1%80%D0%B2%D1%8B%D0%B5_%D0%BB%D1%83%D1%87%D0%B8_%D1%81%D0%BE%D0%BB%D0%BD%D1%86%D0%B0.jpg) no-repeat;
  background-size: cover;
  position: relative;
  height: 500px;
  overflow: hidden;
  z-index: 1;
}

#childCropper {
  border: 2px dashed #ccc;
  position: absolute;
  top: 50px;
  left: 50px;
  height: 200px;
  width: 200px;
}

#childCropper:after {
  content: "";
  width: 100%;
  height: 100%;
  border: 1000px solid rgba(0, 0, 0, 0.5);
  position: absolute;
  top: -1000px;
  left: -1000px;
  z-index: -1;
}
<div id="grandparentImage">
  <div id="childCropper"></div>
</div>

注意:不再需要#parentOverlay 元素。此解决方案还要求祖父元素具有overflow: hidden 属性和z-index (why?)。

【讨论】:

  • 老兄!他们说疯狂和天才之间只有一线之隔,你似乎就靠这条线了。
  • 对此表示赞同,但会暂时保留这个问题,看看是否有其他人分享 2018 年最前沿的真正抠图解决方案。
【解决方案3】:

我猜这就是你要找的东西:

overlay-mask {
  background-color: rgba(0,0,0,.65);
  clip-path: polygon(0% 0%, 75% 0%, 75% 25%, 25% 25%, 25% 75%, 75% 75%, 75% 0%, 100% 0%, 100% 100%, 0 100%);
  z-index: 1;
  pointer-events: none;
  /* rest is optional, you could use 
   * `position:absolute` to place it in a parent with `relative` 
   */
  position: fixed;
  top: 0; bottom: 0; left: 0; right: 0;
}

body {
  margin: 0;
  background: url("https://loremflickr.com/800/600") no-repeat center center /cover;
  min-height: 100vh;
}
&lt;overlay-mask&gt;&lt;/overlay-mask&gt;

它是一个简单的形状,遵循黑暗区域的多边形。点的位置可以用百分比来表示,使用calc() 或者甚至提供一个自定义的&lt;svg&gt; id(并使用外部工具,如Adobe Illustrator 来生成它。

当前浏览器覆盖范围:87.99%。

您可以在掩码下包含任何内容。并且,您可以使用position:absolute 并将其与position:relative 一起放入所需的容器中,而不是使用position:fixed,以应用于该容器。


另一种方法是使用&lt;svg&gt;s &lt;path&gt;。使用 smil 动画或纯 CSS 关键帧对它们进行动画处理非常简单。

例子:

#overlay-mask {
  z-index: 1;
  pointer-events: none;
  /* rest is optional, you could use 
   * `position:absolute` to place it in a parent with `relative` 
   */
  position: fixed;
  top: 0; bottom: 0; left: 0; right: 0;
  color: rgba(0,0,0,.65);
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  left: -2px;
  top: -2px;
}

body {
  margin: 0;
  background: url("https://loremflickr.com/800/600") no-repeat center center /cover;
  min-height: 200vh;
}
h2 {color: white;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="overlay-mask" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
     preserveAspectRatio="none"
     viewBox="0 0 600 600" width="600" height="600">
    <defs>
        <path d="M0 600L0 0L600 0L600 600L0 600ZM100 200L200 200L200 100L100 100L100 200Z" id="cutPath">
        <animate attributeType="XML" attributeName="d" 
        values="M0 600L0 0L600 0L600 600L0 600ZM100 200L200 200L200 100L100 100L100 200Z; M0 600L0 0L600 0L600 600L0 600ZM200 300L300 300L300 200L200 200L200 200Z;M0 600L0 0L600 0L600 600L0 600ZM100 300L300 300L300 100L100 100L100 200Z;M0 600L0 0L600 0L600 600L0 600ZM100 200L200 200L200 100L100 100L100 100Z"
        keyTimes="0; 0.33; 0.66; 1"
        dur="3s" repeatCount="indefinite"
        />
        </path>
    </defs>
    <use xlink:href="#cutPath" opacity="1" fill="currentColor" fill-opacity="1"></use>
    <use xlink:href="#cutPath" opacity="1" fill="none" stroke="white" stroke-width="2"
    stroke-dasharray="1,1"
    ></use>
</svg>

<h2>Scroll down...</h2>

【讨论】:

  • 这看起来很棒,因为看起来我只需要使用 javascript 更改一个属性 - clip-path。不过,这似乎对我在 Safari 上不起作用,但我想只需要一个额外的 -webkit 前缀。
  • @rmn:剪辑路径可以使用 SCSS 编写,显然,使用 JS 函数获取当前位置和间隙大小,以跟随可调整大小的 div。就性能而言,它比其他任何东西都快。
  • 很酷的功能!你可以在polygon 中使用绝对值而不是百分比吗?还有边界呢?
  • @ibrahimmahrir 是的,边界.. :-/ 兴奋地忘记了
  • 仍然缺少边框:p
【解决方案4】:

您可以在#childCropper 上设置box-shadow 和100vmax 传播半径。这样它就会一直覆盖屏幕:

#grandparentImage {
  background: url(https://9to5mac.com/wp-content/uploads/sites/6/2018/07/Desert-2.jpg) no-repeat;
  background-size: cover;
  position: relative;
  height: 500px;
}

#childCropper {
  position: absolute;  
  top: 50px;
  left: 50px;
  height: 200px;
  width: 200px;
  border: 1px dashed #ccc;
  box-shadow: 0 0 0 100vmax rgba(0,0,0,0.5);
}

body {
  margin: 0;
}
<div id="grandparentImage">
  <div id="childCropper"></div>
</div>

【讨论】:

  • 让我想起了 Snatch 的一句话:"not too subtle, but... effective"。 +1。
  • 接受这个答案,因为它的创新性、可读性和完整性(在我的场景中,它也处理虚线边框)。但对于阅读本文并寻找感觉正确的解决方案的人来说,Andrei's answer 使用clip-path。
【解决方案5】:

这是另一种仅使用 one 元素的方法,您可以在其中依靠渐变和多个背景来创建裁剪的叠加层以及虚线边框:

#grandparentImage {
  --g:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5));
  --t:repeating-linear-gradient(to right ,#ccc 0,#ccc 2px,transparent 2px, transparent 4px);
  --b:repeating-linear-gradient(to bottom,#ccc 0,#ccc 2px,transparent 2px, transparent 4px);
  background-image: 
    /*the border*/ 
    var(--t),var(--t),var(--b),var(--b),
    /*the overlay*/
    var(--g),var(--g),var(--g),var(--g),
    /*the image*/
    url(https://picsum.photos/1000/800?image=1069);
  background-size: 
    /*the border*/ 
    40% 2px,40% 2px,2px 40%,2px 40%,
    /*the overlay*/
    100% 30%,100% 30%,20% 40%, 40% 40%,
    /*the image*/
    cover;
  background-position:
    /*the border*/ 
    33.33% 30%,left 33.33% bottom 30%,20% 50%,60% 50%,
    /*the overlay*/
    top,bottom,left center,right center,
    /*the image*/
    center;
  background-repeat:no-repeat;
  position: relative;
  height: 100vh;
}

body {
 margin:0;
}
<div id="grandparentImage">

</div>

覆盖将由 4 个渐变形成一个矩形形状,每个边框将是一个重复渐变以交替白色/透明。

困难的部分是理解不同的值以及background-size/background-position 的计算是如何完成的。这是一个很好的阅读:background-position not working in percentage for linear-gradient


我们也可以和你的点截图:

#grandparentImage {
  --g:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5));
  --t:repeating-linear-gradient(to right ,#ccc 0,#ccc 2px,transparent 2px, transparent 4px);
  --b:repeating-linear-gradient(to bottom,#ccc 0,#ccc 2px,transparent 2px, transparent 4px);
  --d:radial-gradient(#ccc 60%,transparent 62%);
  background-image: 
    /*the dots*/
    var(--d),var(--d),var(--d),var(--d),var(--d),var(--d),var(--d),var(--d),
    /*the border*/ 
    var(--t),var(--t),var(--b),var(--b),
    /*the overlay*/
    var(--g),var(--g),var(--g),var(--g),
    /*the image*/
    url(https://picsum.photos/1000/800?image=1069);
  background-size: 
    /*the dots*/
    10px 10px,10px 10px,10px 10px,10px 10px,10px 10px,10px 10px,10px 10px,10px 10px,
    /*the border*/ 
    40% 2px,40% 2px,2px 40%,2px 40%,
    /*the overlay*/
    100% 30%,100% 30%,20% 40%, 40% 40%,
    /*the image*/
    cover;
  background-position:
    /*the dots*/
    20% 30%,20% 70%,20% 50%,60% 30%,60% 50%,60% 70%,40% 30%,40% 70%, 
    /*the border*/ 
    33.33% 30%,left 33.33% bottom 30%,20% 50%,60% 50%,
    /*the overlay*/
    top,bottom,left center,right center,
    /*the image*/
    center;
  background-repeat:no-repeat;
  position: relative;
  height: 100vh;
}

body {
 margin:0;
}
<div id="grandparentImage">

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多