Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

html - CSS- Applying perspective while rotating an img in css

I'm trying to apply perspective while rotating animation is on in CSS.

However, it works in either way only like

rotating is working only or perspective is working only as below. this one or

.vinyl {
    width: 200px;
    display: block;
    margin: auto;
    opacity: 0.8;
    transform: rotate3d(1,0,0,45deg)
}

this one

.vinyl {
    width: 200px;
    display: block;
    margin: auto;
    opacity: 0.8;
/*     transform: rotate3d(1,0,0,45deg); */
    animation: rotation 2s infinite linear; 
}

how do I apply rotation animation and perspective at the same time? as if the image is rotating and the viewer is watching it with a perspective.

Full code link is in the following link.

.vinyl {
    width: 200px;
    display: block;
    margin: auto;
    opacity: 0.8;
/*     transform: rotate3d(1,0,0,45deg); */
    animation: rotation 2s infinite linear; 
}

.container{
transform-style: preserve-3d;
perspective: 350px;
}

@keyframes rotation {
    from {
        -webkit-transform: rotate(0deg);
    }
    to {
        -webkit-transform: rotate(359deg);
    }
}
<div class="container">
<img class = 'vinyl' src="https://picsum.photos/seed/picsum/200/300" alt="">
</div>
question from:https://stackoverflow.com/questions/65910983/css-applying-perspective-while-rotating-an-img-in-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Keep using your 3D transformation inside the animation:

.vinyl {
  width: 200px;
  display: block;
  margin: auto;
  opacity: 0.8;
  animation: rotation 2s infinite linear;
}

.container {
  transform-style: preserve-3d;
  perspective: 350px;
}

@keyframes rotation {
  from {
    transform: rotate3d(1, 0, 0, 45deg) rotate(0);
  }
  to {
    transform: rotate3d(1, 0, 0, 45deg) rotate(360deg);
  }
}
<div class="container">
  <img class='vinyl' src="https://picsum.photos/seed/picsum/200/300" alt="">
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...