如何用一个元素创建3D透视图图像? [英] How to create a 3D perspective image with one element?

查看:126
本文介绍了如何用一个元素创建3D透视图图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,给定图像可以实现3D透视效果.结果如下:

I have a code that given an image does the effect of perspective in 3D. This is the result:

这是代码:

.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>

问题是,当我在HTML中将它与<div><a>一起使用时,我想与<img>

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">

CSS会如何?请注意,resul有阴影.

how will be the CSS? Note that the resul has shadows to.

推荐答案

这里是使用clip-pathmask的近似值.在某些情况下,由于mask

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">

这篇关于如何用一个元素创建3D透视图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆