简体   繁体   English

我如何为这个钻石图像设置动画,使其看起来像 3d 钻石向下旋转?

[英]How I can animate this diamond image so it looks as 3d diamond is rotating downward?

I want to animate this diamond diamong images in such a way it look like a real diamond is rotating.我想以一种看起来像真正的钻石在旋转的方式来为这个钻石钻石图像设置动画。 As a 3d diamond is rotating downward.作为 3d 钻石向下旋转。 Please help me it means a lot.请帮助我这意味着很多。 Thanks.谢谢。

what i try我尝试什么

animation is used in second section of website动画用于网站的第二部分

<section class="relative flex items-center justify-center w-screen h-screen bg-gray-600">
    <div class="absolute w-40 h-20 top-28" >
        <div class="">
            <img  class="ml-3 w-36 diamond_animtion"  src="fake/diamond.png" alt="">
        </div>
        <img class="absolute ml-12 top-14" style="width: 70px;" src="fake/Prong.png" alt="">
    </div>
    <img class="w-96 h-96" src="fake/ring.png" alt="">
</section>

how i can slow down animation speed如何减慢动画速度

You can do the trick using css overflow and background position, then use JS to make it move.您可以使用 css 溢出和背景位置来实现这一点,然后使用 JS 使其移动。

 let height = 0, steps = 0; const animation = setInterval((function() { height += 290.35, document.getElementById("diamond").style.backgroundPosition = `0px -${height}px`, 18 == steps++ && clearInterval(animation) }), 42); //With scroll
 #diamond { height: 290px; width: 351px; background: url('https://i.stack.imgur.com/HplbX.png') 0px 0px; background-size: 100%; overflow: hidden; }
 <div id="diamond"> </div>

According to your example, here's could be a solution to animate diamond when scroll.根据您的示例,这可能是滚动时为钻石设置动画的解决方案。

NOTE: This solution is not suitable for production because of scroll steps glitch but you can use it and modify to fit your needs注意:由于滚动步骤故障,此解决方案不适合生产,但您可以使用它并进行修改以满足您的需求

 let bounding, height = 145, //Equal your diamond CSS height topPos = 0, scrollPos = 0, steps = 0, diamond = document.getElementById("diamond"); function scrollDiamond(o) { "down" === o && steps < 18 ? (act = `0px -${topPos+=height}px`, steps++) : "up" === o && steps > -1 && (act = `0px -${topPos-=height}px`, steps--) diamond.style.backgroundPosition = act; } window.addEventListener("scroll", (function() { bounding = document.body.getBoundingClientRect().top; bounding > scrollPos ? scrollDiamond("up") : scrollDiamond("down"), scrollPos = bounding }));
 body { height: 200vh; } #diamond { position: fixed; /* Your image heigh divided by 40 (or whatever you want) */ height: 145px; width: 175px; /* Your image width divided by 40 (or whatever you want)*/ background: url('https://i.stack.imgur.com/HplbX.png') 0px 0px/100%; }
 <div id="diamond"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM