gpt4 book ai didi

网格图像中带有 Spritesheet 的 CSS 动画(不是连续的)

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:56 26 4
gpt4 key购买 nike

我正在尝试为 sprite 图像制作动画,并找到了这个很好的例子:

博客:http://simurai.com/blog/2012/12/03/step-animation/ (已屈服于 linkrot)。
回程机:http://web.archive.org/web/20140208085706/http://simurai.com/blog/2012/12/03/step-animation/
代码 fiddle :https://codepen.io/simurai/pen/tukwj

JSFiddle:http://jsfiddle.net/simurai/CGmCe/

.hi {
width: 50px;
height: 72px;
background-image: url("http://s.cdpn.io/79/sprite-steps.png");

-webkit-animation: play .8s steps(10) infinite;
-moz-animation: play .8s steps(10) infinite;
-ms-animation: play .8s steps(10) infinite;
-o-animation: play .8s steps(10) infinite;
animation: play .8s steps(10) infinite; }

@-webkit-keyframes play { from { background-position: 0px; } to { background-position: -500px; } }

@-moz-keyframes play { from { background-position: 0px; } to { background-position: -500px; } }

@-ms-keyframes play { from { background-position: 0px; } to { background-position: -500px; } }

@-o-keyframes play { from { background-position: 0px; } to { background-position: -500px; } }

@keyframes play { from { background-position: 0px; } to { background-position: -500px; } }

我想做同样的事情,但使用正方形(幂或两倍大小)图像图集而不是动画条。例如,这个:

square image atlas

最佳答案

由于这可能是一项难以调试的任务,我想从同样的问题开始,但在更容易调试的环境中。

我选择在整个图像上做一个矩形动画。

.hi {
width: 320px;
height: 315px;
background-image: url("http://i.stack.imgur.com/CjMscm.jpg");
position: relative;
border: solid 1px black;
}

.hi:before {
content: "";
position: absolute;
width: 100%;
height: 53px;
left: 0px;
top: 0px;
border: solid 1px red;
-webkit-animation: playv 18s steps(6) infinite;
}

@-webkit-keyframes playv {
0% { top: 0px; }
100% { top: 315px; }
}

.hi:after {
content: "";
position: absolute;
width: 53px;
height: 100%;
left: 266px;
top: 0px;
border: solid 1px red;
-webkit-animation: playh 3s steps(6) infinite;
}

@-webkit-keyframes playh {
0% { left: 0px; }
100% { left: 320px; }
}
<div class="hi">
</div>

在图像上,我显示了 2 个伪元素,一个是行选择器,另一个是列选择器。然后我调整动画直到它们正常为止


现在,让我们验证同时设置两个动画是否有效:

.hi {
width: 320px;
height: 315px;
background-image: url("http://i.stack.imgur.com/CjMscm.jpg");
position: relative;
border: solid 1px black;
}

.hi:before {
content: "";
position: absolute;
width: 53px;
height: 53px;
left: 0px;
top: 0px;
border: solid 1px red;
-webkit-animation: playv 18s steps(6) infinite, playh 3s steps(6) infinite;
}

@-webkit-keyframes playv {
0% { top: 0px; }
100% { top: 315px; }
}

@-webkit-keyframes playh {
0% { left: 0px; }
100% { left: 320px; }
}
<div class="hi">
</div>


现在是最终元素:

.hi {
width: 53px;
height: 53px;
background-image: url("http://i.stack.imgur.com/CjMscm.jpg");
position: relative;
border: solid 1px black;
-webkit-animation: playv 1s steps(6) infinite, playh 0.1667s steps(6) infinite;
animation: playv 1s steps(6) infinite, playh 0.1667s steps(6) infinite;
}

@-webkit-keyframes playv {
0% {
background-position-y: 0px;
}
100% {
background-position-y: -315px;
}
}

@-webkit-keyframes playh {
0% {
background-position-x: 0px;
}
100% {
background-position-x: -320px;
}
}

@keyframes playv {
0% {
background-position-y: 0px;
}
100% {
background-position-y: -315px;
}
}

@keyframes playh {
0% {
background-position-x: 0px;
}
100% {
background-position-x: -320px;
}
}
<div class="hi">
</div>

所有这些都是针对 webkit 浏览器的,删除 IE 和 FF 的前缀。此外,在这种方法中,无法避免在左下角显示 2 个空白图像。如果您没有完整的网格,并且不想显示空图像,则需要将所有关键帧一一指定

关于网格图像中带有 Spritesheet 的 CSS 动画(不是连续的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22571983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com