html-使用css-transforms为所有浏览器创建遮罩,遇到定位问题

前端之家收集整理的这篇文章主要介绍了html-使用css-transforms为所有浏览器创建遮罩,遇到定位问题 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要创建一个遮罩,以在基于vh的所有浏览器中覆盖图像(无剪切路径)

我使用带有旋转变换的div作为蒙版,然后在内部反转旋转.

我遇到的问题是内部内容的位置不正确.图像需要与内部容器的左上方对齐.

我努力了:

>使用顶部和左侧的值进行定位无效.
>使用transform移动内部容器有效,但是我找不到所需的值是如何计算的.

https://jsfiddle.net/owfgLnv7/5/

  1. .container {
  2. width: 70vh;
  3. height: 100vh;
  4. background-color: blue;
  5. position: absolute;
  6. left: 0;
  7. top: 0;
  8. z-index: 0;
  9. }
  10. .tri {
  11. position: absolute;
  12. width: 70vh;
  13. height: 70vh;
  14. transform: rotate(45deg);
  15. top: calc((100vh - 70vh) / 2);
  16. transform-origin: center center;
  17. background-color: transparent;
  18. z-index: 2;
  19. overflow: hidden;
  20. }
  21. .reset-tri {
  22. position: relative;
  23. z-index: 1;
  24. transform: rotate(-45deg);
  25. transform-origin: center center;
  26. }
  27. .inner-container {
  28. background: black;
  29. }

需要获取图像,使其左上对齐并正常流动

最佳答案
基本上,将元素转换(在此处旋转)后,会将它们从流程中删除-因此,尺寸标注将不会像您不选择时那样表现.

一种方法就是使用简单的数学:

>如果您旋转a侧的45度正方形(此处为70vh正方形),则对角线将为√2* a〜1.414 * a,
>因为transform-origin在此处居中,这意味着溢出宽度或高度等于(1.414 * a-a)/ 2或(1.414-1)* a / 2.
>可以为容器的宽度声明类似的参数,该宽度等于宽度:calc(1.414 * 70vh)

请参见下面的演示:

  1. body {
  2. margin: 0;
  3. }
  4. .page {
  5. width: 100vw;
  6. height: 100vh;
  7. background: grey;
  8. }
  9. .container {
  10. width: calc(1.414 * 70vh); /* changed */
  11. height: 100vh;
  12. background-color: blue;
  13. position: absolute;
  14. left: 0;
  15. top: 0;
  16. z-index: 0;
  17. }
  18. .tri {
  19. position: absolute;
  20. width: 70vh;
  21. height: 70vh;
  22. transform: rotate(45deg);
  23. top: calc(0.414 * 70vh / 2); /* changed */
  24. left: calc(0.414 * 70vh / 2); /* added */
  25. transform-origin: center center;
  26. background-color: transparent;
  27. z-index: 2;
  28. overflow: hidden;
  29. }
  30. .reset-tri {
  31. position: relative;
  32. z-index: 1;
  33. transform: rotate(-45deg);
  34. transform-origin: center center;
  35. }
  36. .inner-container {
  37. background: black;
  38. }
  1. <div class="page">
  2. <div class="container">
  3. <div class="tri">
  4. <div class="reset-tri">
  5. <div class="inner-container">
  6. <img src="https://openclipart.org/download/230732/360sj3.svg" />
  7. </div>
  8. </div>
  9. </div>
  10. </div>
  11. </div>

使用背景图片

对于近乎完美的遮罩,您可以:

>将图像移动到reset-tri容器中的背景图像,然后
>添加一个scale(1.414)转换以完全填充原始未转换的tri容器.

请参见下面的演示:

  1. body {
  2. margin: 0;
  3. }
  4. .page {
  5. width: 100vw;
  6. height: 100vh;
  7. background: grey;
  8. }
  9. .container {
  10. width: calc(1.414 * 70vh); /* changed */
  11. height: 100vh;
  12. background-color: blue;
  13. position: absolute;
  14. left: 0;
  15. top: 0;
  16. z-index: 0;
  17. }
  18. .tri {
  19. position: absolute;
  20. width: 70vh;
  21. height: 70vh;
  22. transform: rotate(45deg);
  23. top: calc(0.414 * 70vh / 2); /* changed */
  24. left: calc(0.414 * 70vh / 2); /* added */
  25. transform-origin: center center;
  26. background-color: transparent;
  27. z-index: 2;
  28. overflow: hidden;
  29. }
  30. .reset-tri {
  31. position: relative;
  32. z-index: 1;
  33. transform: rotate(-45deg) scale(1.414); /* scale by √2 */
  34. transform-origin: center center;
  35. width: 70vh;
  36. height: 70vh;
  37. /* use a bacground image */
  38. background-size: cover;
  39. background-image: url("https://openclipart.org/download/230732/360sj3.svg");
  40. }
  1. <div class="page">
  2. <div class="container">
  3. <div class="tri">
  4. <div class="reset-tri"></div>
  5. </div>
  6. </div>
  7. </div>

使用图像元素

要在不使用背景图像的情况下实现近乎完美的蒙版,可以返回到之前的标记添加object-fit:cover来填充包含其包装器内部容器尺寸的img元素-请参见下面的演示:

  1. body {
  2. margin: 0;
  3. }
  4. .page {
  5. width: 100vw;
  6. height: 100vh;
  7. background: grey;
  8. }
  9. .container {
  10. width: calc(1.414 * 70vh); /* changed */
  11. height: 100vh;
  12. background-color: blue;
  13. position: absolute;
  14. left: 0;
  15. top: 0;
  16. z-index: 0;
  17. }
  18. .tri {
  19. position: absolute;
  20. width: 70vh;
  21. height: 70vh;
  22. transform: rotate(45deg);
  23. top: calc(0.414 * 70vh / 2); /* changed */
  24. left: calc(0.414 * 70vh / 2); /* added */
  25. transform-origin: center center;
  26. background-color: transparent;
  27. z-index: 2;
  28. overflow: hidden;
  29. }
  30. .reset-tri {
  31. position: relative;
  32. z-index: 1;
  33. transform: rotate(-45deg) scale(1.414); /* scale by √2 */
  34. transform-origin: center center;
  35. width: 70vh;
  36. height: 70vh;
  37. }
  38. .inner-container {
  39. height: 100%; /* fill the parent wrapper */
  40. }
  41. .inner-container > img {
  42. width: 100%;
  43. height: 100%;
  44. object-fit: cover; /* the image fills the parent container */
  45. }
  1. <div class="page">
  2. <div class="container">
  3. <div class="tri">
  4. <div class="reset-tri">
  5. <div class="inner-container">
  6. <img src="https://openclipart.org/download/230732/360sj3.svg" />
  7. </div>
  8. </div>
  9. </div>
  10. </div>
  11. </div>

猜你在找的HTML相关文章