问题
I have a code that given an image does the effect of perspective in 3D. This is the result:
This is the code:
.thumb {
margin: 100px;
perspective: 1000px;
width: 1920px;
height: 1200px;
}
.thumb a {
--bg-pseudo: rotateY(120deg); /*Modificar transform */
--bg-pseudo2: --bg-pseudo2', '-5px -15px 45px 130px rgba(0, 0, 0, 0.3); /*modificar box-shadow */
display: block;
width: 100%;
height: 100%;
background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)),url('https://www.wallpaperk.com/wallpapers/white-wolf-painting-5111.jpg');
background-size: 0, cover;
transform-style: preserve-3d;
transition: all 2s;
transform: rotateY(40deg);
/* From rotateX */
transform-origin: left;
/* From bottom */
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.thumb a:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 50px;
height: 100%;
background: inherit;
background-size: cover, cover;
background-position: 0px 0px;
transform: var(--bg-pseudo);
transform-origin: left;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
}
.thumb a:before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
box-shadow: var(--bg-pseudo2);
transition: all 2s;
opacity: 1;
transform: rotateX(0) translateZ(-20px) scale(0.85);
transform-origin: bottom;
}
<div class="thumb" id="thumb">
<a href="#"></a>
</div>
The problem is that when I use this in the HTML is with a <div>
and <a>
, I want use it with a <img>
The question is : Is there a way to do this same effect just by putting a class in the image?
<img src="foto.jpg" class="3dPerspective">
how will be the CSS? Note that the resul has shadows to.
回答1:
Here is an approximation using clip-path
and mask
. It may not render well in some cases due to decreased opacity done by the mask
.box {
--x:10px;
--y:20px;
clip-path:polygon(
0 var(--x),var(--y) 0,
100% 0,100% 100%,
var(--y) 100%,0 calc(100% - var(--x)));
margin:30px;
transform-origin:left;
transform:perspective(1000px) rotateY(40deg);
-webkit-mask:linear-gradient(to right,#fff var(--y),rgba(0,0,0,0.8) 0);
mask:linear-gradient(to right,#fff var(--y),rgba(0,0,0,0.8) 0);
}
<img src="https://picsum.photos/id/1072/600/300" class="box">
<img src="https://picsum.photos/id/104/600/300" class="box">
Another idea with outline better supported with no opacity issue but you will need to cut the image a little:
.box {
--x:10px;
--y:20px;
clip-path:polygon(
0 calc(var(--x) + var(--y)),var(--y) var(--y),
calc(100% - var(--y)) var(--y),calc(100% - var(--y)) calc(100% - var(--y)),
var(--y) calc(100% - var(--y)),0 calc(100% - var(--x) - var(--y)));
margin:30px;
transform-origin:left;
transform:perspective(1000px) rotateY(40deg);
outline: var(--y) solid rgba(0,0,0,0.4);
outline-offset: calc(-1*var(--y));
}
<img src="https://picsum.photos/id/1072/600/300" class="box">
<img src="https://picsum.photos/id/104/600/300" class="box">
来源:https://stackoverflow.com/questions/62379425/how-to-create-a-3d-perspective-image-with-one-element