【问题标题】:Image z-index onhover mutiple SVG polygon悬停多个 SVG 多边形时的图像 z-index
【发布时间】:2019-10-27 16:32:20
【问题描述】:

我正在研究 SVG,我的概念是我有两个图像.overlap,并使用 svg 多边形制作了五个不同的三角形。我的目标是实现第一次覆盖完整的图像显示,并且当悬停在三角形形状背景上时需要显示.box 框图像。 这是我在给出叠加图像之前的初始代码。

.box {
  position: relative;
  background-image: url('https://picsum.photos/id/1/1000/800');
  background-repeat: no-repeat;
  width: 100%;
  height: 600px;
  background-size: cover;
}

polygon {
  stroke-width: 1;
  stroke: red;
  fill: #ffffff;
}

polygon:hover {
  fill: transparent;
}
<div class="box">
  <svg viewbox="0 0 200 100">
		<polygon points="0,100 100,100 0,50 "
			    style="stroke:#000000;"/>
		<polygon points="0,50 100,100 50,00 0,0 "
			    style="stroke:#000000;"/>
		<polygon points="100,100 50,00 150,0"
			    style="stroke:#000000;"/>
		<polygon points="100,100 200,50 200,0 150,0"
			    style="stroke:#000000;"/>
		<polygon points="100,100 200,100, 200,50"
			    style="stroke:#000000;"/>
	</svg>
</div>

现在我添加了覆盖图像需要位于.box 图像和polygons 形状之上。现在,悬停我想像这样以当前多边形形状显示 .box 图像

代码在这里

.box {
  position: relative;
  background-image: url('https://picsum.photos/id/1/1000/800');
  background-repeat: no-repeat;
  width: 100%;
  height: 600px;
  background-size: cover;
}

polygon {
  stroke-width: 1;
  stroke: red;
  fill: #ffffff;
}

polygon:hover {
  fill: transparent;
}

.overlay:hover polygon {
  z-index: 100;
}

.overlay {
  position: absolute;
  background-image: url('https://picsum.photos/id/118/1000/800');
  background-repeat: no-repeat;
  width: 100%;
  height: 600px;
  background-size: cover;
  z-index: 10;
}
<div class="overlay"></div>
<div class="box">

  <svg viewbox="0 0 200 100">
		<polygon points="0,100 100,100 0,50 "
			    style="stroke:#000000;"/>
		<polygon points="0,50 100,100 50,00 0,0 "
			    style="stroke:#000000;"/>
		<polygon points="100,100 50,00 150,0"
			    style="stroke:#000000;"/>
		<polygon points="100,100 200,50 200,0 150,0"
			    style="stroke:#000000;"/>
		<polygon points="100,100 200,100, 200,50"
			    style="stroke:#000000;"/>
	</svg>
</div>

任何人都可以在悬停时帮我解决这个问题,我们需要调整 div 的 z-indexes 和多边形填充。

【问题讨论】:

    标签: html css svg


    【解决方案1】:

    如果您希望 svg 出现在其父 div 上方,您可以更改 svg 元素的属性,如下所示:

       /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
       /*necessary for expanding .box div and svg*/
       /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
       html,body{
        width:100%;
        height:100%;
       }
       
       .box {
          width:100%;
          height:100%;
          
          position: relative;
          background-image: url('https://picsum.photos/id/1/1000/800');
          background-repeat: no-repeat;
          width: 100%;
          
          /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
          /*I CHANGED HEIGHT TO 100% FOR BOX AS IT WASNT SPANNING THE ENTIRE PAGE - in turn, the "overlay"
          div's background image could be seen...*/
          /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
          height: 100%;
          background-size: cover;
        }
        
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        /*position the svg element relative to its parent so it can be moved up with z-index*/
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        svg{
          position:relative;
          z-index:999;
        }
    
        polygon {
          stroke-width: 1;
          stroke: red;
          fill: #ffffff;
        }
    
        polygon:hover {
          fill: transparent;
        }
    
        .overlay:hover polygon {
          z-index: 100;
        }
    
        .overlay {
          position: absolute;
          background-image: url('https://picsum.photos/id/118/1000/800');
          background-repeat: no-repeat;
          width: 100%;
          height: 100%;
          background-size: cover;
          z-index: 10;
        }
    <div class="overlay"></div>
    <div class="box">
    
      <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <!--in order to make the svg span the entirety of its parent, I set width and height attributes
      for it to 100%-->
      <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <svg width="100%" height="100%" preserveAspectRatio="none" viewbox="0 0 200 100">
        <polygon points="0,100 100,100 0,50 "
              style="stroke:#000000;"/>
        <polygon points="0,50 100,100 50,0 0,0 "
              style="stroke:#000000;"/>
        <polygon points="100,100 50,00 150,0"
              style="stroke:#000000;"/>
        <polygon points="100,100 200,50 200,0 150,0"
              style="stroke:#000000;"/>
        <polygon points="100,100 200,100, 200,50"
              style="stroke:#000000;"/>
      </svg>
    </div>

    【讨论】:

    • 我编辑了我的答案并包含了我使用的代码;希望对你有帮助
    • 不需要显示初始叠加图像。
    • 哦,好的,我明白了。对不起,我有点误会了。给我一秒钟
    【解决方案2】:

    我会调整my previous answer的代码如下:

    .box {
      width:450px;
      height:250px;
      position:relative;
      overflow:hidden;
      z-index:0;
      background:url(https://picsum.photos/id/13/1000/800) center/cover;
    }
    .box > div {
      position:absolute;
      top:0;
      left:0;
      right:0;
      bottom:0;
      background-size:cover;
      background-position:center;
      opacity:0;
    }
    .box > div:nth-child(1) {
      clip-path:polygon(20% 0,80% 0, 50% 100%);
    }
    .box > div:nth-child(2) {
      clip-path:polygon(0 0, 20% 0,50% 100%,0 40%);
    }
    .box > div:nth-child(3) {
      clip-path:polygon(100% 0,80% 0,50% 100%,100% 40%);
    }
    
    .box > div:nth-child(4) {
      clip-path:polygon(0 100%,50% 100%,0 40%);
    }
    .box > div:nth-child(5) {
      clip-path:polygon(100% 100%,50% 100%,100% 40%);
    }
    
    .box > div:hover {
       opacity:1;
    }
    <div class="box">
      <div style="background-image:url(https://picsum.photos/id/1/1000/800)"></div>
      <div style="background-image:url(https://picsum.photos/id/10/1000/800)"></div>
      <div style="background-image:url(https://picsum.photos/id/90/1000/800)"></div>
      <div style="background-image:url(https://picsum.photos/id/102/1000/800)"></div>
      <div style="background-image:url(https://picsum.photos/id/118/1000/800)"></div>
    </div>

    这是一个插图,向您展示clip-path中使用的不同点

    同一张图片:

    .box {
      width:450px;
      height:250px;
      position:relative;
      overflow:hidden;
      z-index:0;
      background:url(https://picsum.photos/id/13/1000/800) center/cover;
    }
    .box > div {
      position:absolute;
      top:0;
      left:0;
      right:0;
      bottom:0;
      background-image:url(https://picsum.photos/id/118/1000/800);
      background-size:cover;
      background-position:center;
      opacity:0;
    }
    .box > div:nth-child(1) {
      clip-path:polygon(20% 0,80% 0, 50% 100%);
    }
    .box > div:nth-child(2) {
      clip-path:polygon(0 0, 20% 0,50% 100%,0 40%);
    }
    .box > div:nth-child(3) {
      clip-path:polygon(100% 0,80% 0,50% 100%,100% 40%);
    }
    
    .box > div:nth-child(4) {
      clip-path:polygon(0 100%,50% 100%,0 40%);
    }
    .box > div:nth-child(5) {
      clip-path:polygon(100% 100%,50% 100%,100% 40%);
    }
    
    .box > div:hover {
       opacity:1;
    }
    <div class="box">
      <div ></div>
      <div ></div>
      <div ></div>
      <div ></div>
      <div ></div>
    </div>

    【讨论】:

    • 这正是我需要的唯一点是在我想要添加的所有多边形相同的图像中,如果添加单独的相同图像我需要调整图像位置。你能建议我关于这唯一的一张图片,只是以不同的三角形形状查看。
    • @Husna 只需对所有 div 使用相同的图像,无需调整任何其他内容......我使用内联样式添加了不同的图像,只需删除它并使用 CSS 中的图像(检查更新)
    • 好的。我真的很感谢你在这里付出的时间。 :)
    【解决方案3】:

    Temani's CSS only solution 很棒,但是请注意这是still not supported in Safari nor in IE/Edge

    对于这些浏览器,您需要回退到 SVG,它还实现了 &lt;clipPath&gt; 元素。

    .overlay {
      background-image: url('https://picsum.photos/id/118/1000/800');
      background-repeat: no-repeat;
      background-size: cover;
    }
    .overlay use {
      opacity: 0;
    }
    .overlay use:hover {
      opacity: 1;
    }
    <svg class="overlay" viewBox="0 0 200 100">
      <defs>
        <clipPath id='clip-1'>
          <polygon points="0,100 100,100 0,50"/>
        </clipPath>
        <clipPath id='clip-2'>
          <polygon points="0,50 100,100 50,00 0,0"/>
        </clipPath>
        <clipPath id='clip-3'>
          <polygon points="100,100 50,00 150,0"/>
        </clipPath>
        <clipPath id='clip-4'>
          <polygon points="100,100 200,50 200,0 150,0"/>
        </clipPath>
        <clipPath id='clip-5'>
          <polygon points="100,100 200,100, 200,50"/>
        </clipPath>
        <image id="img" x="0" y="0" width="200" height="100" preserveAspectRatio="xMidYMid slice"
          xlink:href="https://picsum.photos/id/1/1000/800" />
      </defs>
      <use xlink:href="#img" clip-path="url(#clip-1)"/>
      <use xlink:href="#img" clip-path="url(#clip-2)"/>
      <use xlink:href="#img" clip-path="url(#clip-3)"/>
      <use xlink:href="#img" clip-path="url(#clip-4)"/>
      <use xlink:href="#img" clip-path="url(#clip-5)"/>
    </svg>
    <div class="box"></div>

    所以,是的,它更冗长,但它应该适用于自 IE9 以来的所有浏览器。

    【讨论】:

    • 谢谢。在这里,我不希望图像上的多边形线像 Temani 输出那样,您可以在代码中进行更改并删除图像上的线。
    • @Husna 那就更简单了
    • 线条还是很细的 1px 即将到来,您能建议我如何删除这些线条吗?我们删除了笔画,但仍然是这些线条的来源。
    • 那是抗锯齿,你必须让你的多边形比必要的大一点(例如让它们在每个方向上流血一个单位)。对不起,我现在没时间了。啊,或者实际上我们可以像 Temani 一样制作:反转隐藏的内容
    • 我编辑使用与 Temani 相同的技术:在悬停时显示切片。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 2013-04-07
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多