我需要创建一个遮罩,以基于vh
(否clip-path
)在所有浏览器中覆盖图像
我正在使用带有旋转变换的div作为蒙版,然后在内部反转旋转。
我遇到的问题是内部内容的位置不正确。图片需要对齐到内部容器的左上方。
我尝试过:
https://jsfiddle.net/owfgLnv7/5/
.container {
width: 70vh;
height: 100vh;
background-color: blue;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
.tri {
position: absolute;
width: 70vh;
height: 70vh;
transform: rotate(45deg);
top: calc((100vh - 70vh) / 2);
transform-origin: center center;
background-color: transparent;
z-index: 2;
overflow: hidden;
}
.reset-tri {
position: relative;
z-index: 1;
transform: rotate(-45deg);
transform-origin: center center;
}
.inner-container {
background: black;
}
需要获取图像以使其左上对齐并正常流动
答案 0 :(得分:2)
基本上,当对元素进行转换(在此处旋转)时,它们会从流程中删除-因此尺寸不会像您不执行的那样表现。
一种方法只是使用简单的数学:
a
边的方形旋转45度(此处为70vh方形),则对角线将为√2 * a
〜1.414 * a
,< / li>
transform-origin
是center
,这意味着溢出宽度或高度等于(1.414 * a - a) / 2
或(1.414 - 1) * a / 2
。container
的宽度声明width: calc(1.414 * 70vh)
请参见下面的演示
body {
margin: 0;
}
.page {
width: 100vw;
height: 100vh;
background: grey;
}
.container {
width: calc(1.414 * 70vh); /* changed */
height: 100vh;
background-color: blue;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
.tri {
position: absolute;
width: 70vh;
height: 70vh;
transform: rotate(45deg);
top: calc(0.414 * 70vh / 2); /* changed */
left: calc(0.414 * 70vh / 2); /* added */
transform-origin: center center;
background-color: transparent;
z-index: 2;
overflow: hidden;
}
.reset-tri {
position: relative;
z-index: 1;
transform: rotate(-45deg);
transform-origin: center center;
}
.inner-container {
background: black;
}
<div class="page">
<div class="container">
<div class="tri">
<div class="reset-tri">
<div class="inner-container">
<img src="https://openclipart.org/download/230732/360sj3.svg" />
</div>
</div>
</div>
</div>
</div>
使用背景图片
对于近乎完美的遮罩,您可以:
将image
移到background-image
容器中的reset-tri
和
添加一个scale(1.414)
转换以完全填充原始的未转换 tri
容器。
请参见下面的演示
body {
margin: 0;
}
.page {
width: 100vw;
height: 100vh;
background: grey;
}
.container {
width: calc(1.414 * 70vh); /* changed */
height: 100vh;
background-color: blue;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
.tri {
position: absolute;
width: 70vh;
height: 70vh;
transform: rotate(45deg);
top: calc(0.414 * 70vh / 2); /* changed */
left: calc(0.414 * 70vh / 2); /* added */
transform-origin: center center;
background-color: transparent;
z-index: 2;
overflow: hidden;
}
.reset-tri {
position: relative;
z-index: 1;
transform: rotate(-45deg) scale(1.414); /* scale by √2 */
transform-origin: center center;
width: 70vh;
height: 70vh;
/* use a bacground image */
background-size: cover;
background-image: url("https://openclipart.org/download/230732/360sj3.svg");
}
<div class="page">
<div class="container">
<div class="tri">
<div class="reset-tri"></div>
</div>
</div>
</div>
使用图像元素
对于不使用background-image
的近乎完美的 masking ,您可以返回上一个标记,然后将object-fit: cover
添加到填充尺寸的img
元素中包装inner-container
的内容-参见下面的演示:
body {
margin: 0;
}
.page {
width: 100vw;
height: 100vh;
background: grey;
}
.container {
width: calc(1.414 * 70vh); /* changed */
height: 100vh;
background-color: blue;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
.tri {
position: absolute;
width: 70vh;
height: 70vh;
transform: rotate(45deg);
top: calc(0.414 * 70vh / 2); /* changed */
left: calc(0.414 * 70vh / 2); /* added */
transform-origin: center center;
background-color: transparent;
z-index: 2;
overflow: hidden;
}
.reset-tri {
position: relative;
z-index: 1;
transform: rotate(-45deg) scale(1.414); /* scale by √2 */
transform-origin: center center;
width: 70vh;
height: 70vh;
}
.inner-container {
height: 100%; /* fill the parent wrapper */
}
.inner-container > img {
width: 100%;
height: 100%;
object-fit: cover; /* the image fills the parent container */
}
<div class="page">
<div class="container">
<div class="tri">
<div class="reset-tri">
<div class="inner-container">
<img src="https://openclipart.org/download/230732/360sj3.svg" />
</div>
</div>
</div>
</div>
</div>