css 使用任意颜色在不同z索引的容器中创建孔

v440hwme  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(346)

我一直在寻找一个满意的答案,这一段时间使用谷歌/Stackoverflow和ChatGPT,但我真的不能弄清楚。
我试图创建一个包含3 x 3个小盒子(正方形)的盒子(主容器)。我们的想法是让主容器的z索引比square高,让square透明,这样当我对某个东西进行动画时,动画元素(比如z索引为1)在穿过正方形之间的空间时会被隐藏(例如,当你看到一个物体时)。主容器的可见部分),但是当穿过正方形时可见。
下面的代码可以工作,但只使用黑色作为主容器的背景色。我希望能够获得相同的结果与任何颜色。

// Create a colored square element
var square = document.createElement('div');
square.style.width = '2vw';
square.style.height = '2vw';
square.style.backgroundColor = 'red';
square.style.position = 'absolute';

// Set the initial position of the square to center left
square.style.left = '0';
square.style.top = 'calc(50vh - 1vw)';

// Add the square element to the page
document.body.appendChild(square);

// Animate the square
function animateSquare() {
  var currentPosX = parseFloat(square.style.left);
  
  // Update the square's position
  var newPosX = currentPosX + 5; // Move the square to the right by 5 pixels
  
  square.style.left = newPosX + 'px';

  // Check if the square has reached the center right
  if (newPosX + square.offsetWidth >= window.innerWidth / 2) {
    // If the square has reached the center right, stop animating
    return;
  }
  
  // Continue animating
  requestAnimationFrame(animateSquare);
}

// Start the animation
animateSquare();
.main-container {
  position: relative;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
  width: 30vw;
  height: 30vw;
  align-items: center;
  justify-content: space-evenly;
  padding: 1%;
  border: solid 1px gray;
  border-radius: 10%;
  background-color: black;
  mix-blend-mode: hard-light;
  z-index: 3;
}

.square {
  width: 24%;
  height: 24%;
  margin: 1%;
  border-radius: 20%;
  background-color: rgb(204, 118, 118);
  z-index: -1;
}
<div class="main-container">
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
</div>
oxf4rvwz

oxf4rvwz1#

使用<clipPath id="clip" clipPathUnits="objectBoundingBox">创建一个SVG图像(更容易使用SVG编辑器),并将其用作元素的CSS clip-path
这里有一个9个正方形的例子(你可以像你的例子一样创建圆形边框的正方形。这是一个概念的证明)

body {
  background-color: #4a9;
}

.grid {
  position: relative;
  background: url(https://placekitten.com/408/287) 50% / cover;
  width: 300px;
  height: 300px;
  clip-path: url(#clip);
}

.ball {
  display: flex;
  justify-content: center;
  align-items: center;
  background: gold;
  border-radius: 50%;
  width: 50%;
  height: 50%;
  animation: moveBall 2s ease-in-out alternate infinite;
}

@keyframes moveBall {
  0%   { translate: -20% -20%; }
  100% { translate: 120% 120%; }
}
<div class="grid">
  <div class="ball">HI!</div>
</div>

<svg id="clipSvg" x="0px" y="0px" viewBox="0 0 1 1" style="width: 100%; height: 100%;">
  <defs>
    <clipPath id="clip" clipPathUnits="objectBoundingBox">
      <rect x="0" y="0" width="0.32" height="0.32" fill="black"/>
      <rect x="0.34" y="0" width="0.32" height="0.32" fill="black"/>
      <rect x="0.68" y="0" width="0.32" height="0.32" fill="black"/>
      <rect x="0" y="0.34" width="0.32" height="0.32" fill="black"/>
      <rect x="0.34" y="0.34" width="0.32" height="0.32" fill="black"/>
      <rect x="0.68" y="0.34" width="0.32" height="0.32" fill="black"/>
      <rect x="0" y="0.68" width="0.32" height="0.32" fill="black"/>
      <rect x="0.34" y="0.68" width="0.32" height="0.32" fill="black"/>
      <rect x="0.68" y="0.68" width="0.32" height="0.32" fill="black"/>
    </clipPath>
  </defs>
</svg>

如果你想让你的球即使在外面也能看到,那么任务就更简单了。创建一个带有所需穿孔(圆形正方形)的黑色SVG形状,在其容器内使用该形状的z索引,确保球的z索引低于穿孔形状。

guykilcj

guykilcj2#

我认为你需要一个元素的遮罩。请参阅this Clipping and Masking in CSS文章,但您必须自己创建矢量。
创建一个与您提供的图片完全相同的矢量,但只填充您希望动画可见的位置。然后简单地遮罩正在为矢量设置动画的元素。
下面是文章中的一个例子

img {
  width: 120px;
  margin: 20px;
}

.clip-svg {
  clip-path: url(#myClip);
}

body {
  background: linear-gradient(
    to bottom,
    red,
    yellow
  );
}

body, html {
  height: 100%;
}
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" alt="" class="clip-svg">

<svg width="0" height="0">
  <defs>
    <clipPath id="myClip">
      <circle cx="100" cy="100" r="40"/>
      <circle cx="60" cy="60" r="40"/>
    </clipPath>
  </defs>
</svg>

相关问题