Как достичь «глубины» с помощью 3D-преобразования CSS - PullRequest
4 голосов
/ 25 апреля 2019

Я пытаюсь создать «перспективный макет» с использованием CSS. Существует множество учебников о том, как добиться этого с помощью 3D-слоев в Photoshop, но я бы хотел сделать это с помощью CSS. Вот пример того, чего я пытаюсь достичь: enter image description here

А вот код (с использованием необработанного изображения, https://i.imgur.com/foDEYpB.png):

#perspective {
  width: 400px;
  height: 500px;
  position: absolute;
  background-image: url("https://i.imgur.com/foDEYpB.png");
  background-repeat: no-repeat;
  background-size: cover;
  top: 50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -250px;
  transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
  box-shadow: -15px 15px 20px rgba(0, 0, 0, 0.5);
}
<div id='perspective'></div>

Я довольно близко, но не уверен, как достичь "глубины" или "высоты", когда изображение выглядит поднятым. Увеличено в версии упомянутой «глубины», где изображение повторяется по сторонам: enter image description here

P.S. если кто-нибудь знает правильное название для того, что я называю «глубиной», я хотел бы знать!

Ответы [ 2 ]

3 голосов
/ 25 апреля 2019

Вот хакерская идея использовать несколько фонов для имитации такого эффекта.Хитрость заключается в том, чтобы добавить 2 полупрозрачных градиента, чтобы создать эффект тени, а затем еще 2 градиента, чтобы вырезать небольшую часть угла для получения трехмерной фигуры.

Результат может быть не идеальным для всех изображений:

.wrapper {
  display:inline-block;
  perspective:1000px;
}
.box {
  margin: 50px;
  width:200px;
  height:200px;
  transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
  background:
  linear-gradient(to bottom right,transparent 49%,#fff 52%) bottom right/14px 10px,
  linear-gradient(to top left,transparent 49%,#fff 52%) top left /10px 14px,
  
  linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)) 0 0px/10px 100%,   
  linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)) 100% 100%/calc(100% - 10px) 10px,   
  url(https://picsum.photos/id/1061/1000/800) center/cover;
  background-repeat:no-repeat;
}
<div class="wrapper" >
<div class="box" >
</div>
</div>

С вашим изображением вы можете иметь определенный градиент, как показано ниже:

body {
  background:#ccc;
  
}
.wrapper {
  display:inline-block;
  perspective:1000px;
}
.box {
  margin: 50px;
  width:200px;
  height:250px;
  transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
  background:
  linear-gradient(to bottom right,transparent 49%,#ccc 52%) bottom right/16px 10px,
  linear-gradient(to top left,transparent 49%,#ccc 52%) top left /10px 12px,
  
  linear-gradient(#efefef,#efefef) 100% 100%/calc(100% - 10px) 10px, 
  linear-gradient(-226deg,#222428 13px,#ff4946 13px,#ff4946 77px,#592D30 77px,#592D30 100px,#222428 100px,#222428 108px,#efefef 108px,#efefef 161px) 0 0px/10px 100%,      
  
  url(https://i.imgur.com/foDEYpB.png) center/cover;
  background-repeat:no-repeat;
}
<div class="wrapper">
  <div class="box">
  </div>
</div>
3 голосов
/ 25 апреля 2019

Попробуйте добавить три типа изображений для создания 3D-эффектов. Используйте transform свойство с вращением для изображений, чтобы получить желаемый результат.

Ссылка на ответ здесь .

.perspective {
    position: relative;
    width: 400px;
    height: 500px;
    transform-style: preserve-3d;
    transition: all 500ms ease-in;
    transform: rotateY(20deg) rotateX(60deg) rotateZ(-10deg);
    transform: rotateY(15deg) rotateX(50deg) rotateZ(-15deg);
    box-shadow: -40px 80px 80px -10px rgba(0, 0, 0, 0.7);
    cursor: pointer;
    margin-right: 30px;
    display: inline-block;
    margin-left: 30%;
}

.perspective img {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 400px;
    height: 500px;
    transform: translateZ(16px);
}

.bottom,
.left {
    position: absolute;
    width: 400px;
    height: 500px;
    display: block;
    transition: all 1s linear;
    overflow: hidden;
    border-radius: 3px;
    transform: translateZ(16px);
}

.left {
    transform: rotateY(270deg) translateX(-1px);
    transform-origin: center left;
    width: 18px;
}

.bottom {
    transform: rotateX(90deg) translateY(15px) translateZ(-480px);
    transform-origin: bottom center;
    height: 18px;
}

.bottom img {
    transform: rotateX(180deg);
    width: 100%;
    height: 500px;
    left: 0px;
}
<div class="perspective">
    <img src="https://i.imgur.com/foDEYpB.png">
    <div class="bottom"><img src="https://i.imgur.com/foDEYpB.png"></div>
    <div class="left"><img src="https://i.imgur.com/foDEYpB.png"></div>
</div>
...