简体   繁体   English

为什么灌装杯子的 css animation 与交互作用

[英]why css animation of filling a cup works with interaption

Here I just want to fill the cup by 'bear'.在这里,我只想用“熊”来填满杯子。 So I thought that I just can change background image and imitate the filling process.所以我认为我可以改变背景图像并模仿填充过程。 But how you see background runs right or left and I don't know why.但是您如何看待背景向右或向左运行,我不知道为什么。 If you have a solution or another better way please help me.如果您有解决方案或其他更好的方法,请帮助我。

This is html这是 html

 * { box-sizing: border-box; }.container { padding: 5em; background: #1B3D6E; text-align: center; }.load { animation-name: loading; animation-duration: 4s; animation-iteration-count: infinite; } @keyframes loading { 0% { } 20% { background: url('https://i.ibb.co/7bK4D36/Vector-1.png'); background-repeat: no-repeat; background-size: auto; background-position: bottom; } 50% { background: url('https://i.ibb.co/74yw3Fh/Bar-Loader.png'); background-repeat: no-repeat; } 70% { background: url('https://i.ibb.co/74yw3Fh/Bar-Loader.png'); background-repeat: no-repeat; background-size: auto; background-position: 4px 5px; } 90% { background: url('https://i.ibb.co/74yw3Fh/Bar-Loader.png'); background-repeat: no-repeat; background-size: auto; background-position: 4px 5px; } 100% { background: url('https://i.ibb.co/74yw3Fh/Bar-Loader.png'); background-repeat: no-repeat; background-size: auto; background-position: 4px 5px; } }
 <div class="container"> <img class="load" src="https://i.ibb.co/mbpyFZ2/Loader.png" width="48" height="90" alt="Loading..."> </div>

So to see all animation please run snipper on full page.因此,要查看所有 animation,请在整页上运行 snipper。 Thanks谢谢

The problem is that your Bar-Loader.png image is a different size than the Loader.png and when you set the background-position in the animation it gets animated.问题是您的 Bar-Loader.png 图像的大小与 Loader.png 不同,当您在 animation 中设置背景位置时,它会变为动画。 I think that the ideal solution would be having the two images of the same size so you don't have to change the position according to the Loader border size.我认为理想的解决方案是让两个图像大小相同,这样您就不必根据 Loader 边框大小更改 position。

Anyway a little workaround (i know it's not perfect) could be using background-size: contain so the Bar-Loader.png gets a little bit stretched to fit the other image.无论如何,一个小解决方法(我知道它并不完美)可能是使用 background-size: contains 所以 Bar-Loader.png 得到一点拉伸以适应其他图像。

 * { box-sizing: border-box; }.container { padding: 5em; background: #1B3D6E; text-align: center; }.load { animation-name: loading; animation-duration: 4s; animation-iteration-count: infinite; } @keyframes loading { 0% { background: url('https://i.ibb.co/7bK4D36/Vector-1.png'); background-repeat: no-repeat; background-size: contain; background-position: bottom; } 100% { background: url('https://i.ibb.co/74yw3Fh/Bar-Loader.png'); background-repeat: no-repeat; background-size: contain; background-position: bottom; } }
 <div class="container"> <img class="load" src="https://i.ibb.co/mbpyFZ2/Loader.png" width="48" height="90" alt="Loading..."> </div>

I tried to keep it as simple as possible with a transition with only one breakpoint我试图通过只有一个断点的过渡来使其尽可能简单

I would consider another png file that I will be using as mask to make the animation easier where you can consider a gradient:我会考虑使用另一个 png 文件作为掩码,以使 animation 更容易,您可以在其中考虑渐变:

 .container { padding: 5em; background: #1B3D6E; text-align: center; }.load { animation: loading 4s infinite; background: linear-gradient(orange 0 0) /* coloration */ bottom/ /* position */ 100% 0% /* width=100% height=0% */ no-repeat; -webkit-mask: url(https://i.ibb.co/nkQm9Zb/Loader.png) center/contain no-repeat; mask: url(https://i.ibb.co/nkQm9Zb/Loader.png) center/contain no-repeat; } @keyframes loading { to { background-size:100% 100% /* width=100% height=100% */ } }
 <div class="container"> <img class="load" src="https://i.ibb.co/mbpyFZ2/Loader.png" width="48" height="90" alt="Loading..."> </div> below the mask<br> <img src="https://i.ibb.co/nkQm9Zb/Loader.png" style="background:#000;border:5px solid #000">

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

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