css3实现雪碧图帧动画(转-完善)
从网上查到了相关资料,转过来研究学习一下,
背景:
此需求是在html界面实现,纯原生,没有像vue生命周期那些东西。
场景:
一个单纯的图片背景,想要在上面加个动图。一瞬间脑子里想到两个方案:
一:最省前端功夫的:ui压缩一个gif图出来
二:需要前端写点代码的:把ui提供的多张图片,利用css3动画,进行动画切换,达到动图的效果
情况一:ui压缩出的gif出现白边的问题,就不太好看,而且ui已经尽力了。。。
效果图:

情况二:使用css3动画属性,也发现了不适用的问题:
效果图:截图不能展示问题
老老实实使用css3.keyframe和animation来实现,这里也发现了问题,图片够小,即使预先在head加载了图片,但是没有添加到body中,可以在network看到图片加载成功,然后在动画里使用延迟,在qq、谷歌均可以正常加载,唯独在谷歌出现依旧出现闪烁。
基于此两种解决方案:
1:在head中写一个js,其目的是,快于页面加载,就生成img标签,赋值图片路径,append到页面。
间接实现图片预先加载,而后进行css3帧动画,当然你的页面不需要用到他,你可以隐藏掉。(此方案不太优雅,但能成事)
图片形式:

js:
for(var i = 0;i<10;i++){
var img = new Image()
img.onload = await function () {}.bind(this)
img.src='/static/img/'+i+'_03.png'
document.body.append(img)
}
.frame-logo .lo-wrap .img-bg{
width: 440px;
height: 424px;
display: flex;
position: absolute;
top: 3vh;
left: 9vw;
background: url('/static/img/frame1/1_03.png');
background-size:cover;
animation: identifier_p 2s infinite;
}
@keyframes identifier_p {
0% {
background: url('/static/img/frame1/1_03.png');
}
7% {
background: url('/static/img/frame1/2_03.png');
}
14% {
background: url('/static/img/frame1/3_03.png');
}
21% {
background: url('/static/img/frame1/4_03.png');
}
28% {
background: url('/static/img/frame1/5_03.png');
}
35% {
background: url('/static/img/frame1/6_03.png');
}
42% {
background: url('/static/img/frame1/7_03.png');
}
49% {
background: url('/static/img/frame1/8_03.png');
}
56% {
background: url('/static/img/frame1/9_03.png');
}
63% {
background: url('/static/img/frame1/10_03.png');
}
70% {
background: url('/static/img/frame1/11_03.png');
}
77% {
background: url('/static/img/frame1/12_03.png');
}
84% {
background: url('/static/img/frame1/13_03.png');
}
91% {
background: url('/static/img/frame1/14_03.png');
}
98% {
background: url('/static/img/frame1/14_03.png');
}
100% {
background: url('/static/img/frame1/1_03.png');
}
}
2:使用雪碧图进行实现,将多张帧的图,合成雪碧图,然后改变位置进行实现。
animation: identifier_p 2s steps(1, start) infinite;这行代码中的steps(1, start)是实现的关键,如果没有他,这个动画相当可怕,等同于看一张不停的改变定位的背景图。有了他才能真正称为是“帧动画”,可以说是相当丝滑了,比gif体验好多了。
图片形式:



css:
.frame-logo .lo-wrap .img-bg{
width: 440px;
height: 424px;
display: flex;
position: absolute;
top: 3vh;
left: 9vw;
background: url('/static/img/test/css_sprites.png');
background-size:cover;
animation: identifier_p 2s steps(1, start) infinite;
//steps:(number,star/end)
}
@keyframes identifier_p {
0% {
background: url('/static/img/test/css_sprites.png') -10px -10px;
}
7% {
background: url('/static/img/test/css_sprites.png') -488px -10px;
}
14% {
background: url('/static/img/test/css_sprites.png') -10px -474px;
}
21% {
background: url('/static/img/test/css_sprites.png') -488px -474px;
}
28% {
background: url('/static/img/test/css_sprites.png') -966px -10px;
}
35% {
background: url('/static/img/test/css_sprites.png') -966px -474px;
}
42% {
background: url('/static/img/test/css_sprites.png') -10px -938px;
}
49% {
background: url('/static/img/test/css_sprites.png') -488px -938px;
}
56% {
background: url('/static/img/test/css_sprites.png') -966px -938px;
}
63% {
background: url('/static/img/test/css_sprites.png') -1444px -10px;
}
70% {
background: url('/static/img/test/css_sprites.png') -1444px -474px;
}
77% {
background: url('/static/img/test/css_sprites.png') -1444px -938px;
}
84% {
background: url('/static/img/test/css_sprites.png') -10px -1402px;
}
91% {
background: url('/static/img/test/css_sprites.png') -488px -1402px;
}
98% {
background: url('/static/img/test/css_sprites.png') -10px -1402px;
}
100% {
background: url('/static/img/test/css_sprites.png') -488px -1402px;
}
}
原文地址 :https://blog.csdn.net/qq_44706619/article/details/121426057
补充 :
Steps(<number_of_steps>,<direction>)
方向为”start”表示一个左–持续函数,在动画开始时,动画的第一段将会马上完成。以左侧端点为起点,立即跳到第一个step的结尾处。它会立即跳到第一段的结束并且保持这样的状态直到第一步的持续时间结束。后面的每一帧都将按照此模式来完成动画。
方向为”end”表示一个右–持续函数。动画执行时,在每一帧里,动画保持当前状态直到这一段的持续时间完成,才会跳到下一步的起点,后面的每一帧都按照这个模式来进行,在最后一帧的起点,等到这一帧的持续时间结束,整个动画的执行也已经结束,执行动画的元素来不及跳到这一帧的终点,直接回到了整个动画起点,开始了第二次动画。每个选择本质上从一个不同的面移动这个元素并且将产生一个不同的位置在这个相同的动画里。
这面是示例图:


References:
House of fun casino
References:
https://instapages.stream/story.php?title=crypto-casino-licensing-guide-bitcoin-gaming-compliance-2024
References:
How is synthetic testosterone made
References:
https://www.arianesarahkoch.de/blog/index.php/;focus=STRATP_com_cm4all_wdn_Flatpress_37649217&frame=STRATP_com_cm4all_wdn_Flatpress_37649217?x=entry:entry230902-192725;comments:1
References:
Portugues online
References:
https://bandori.party/user/686241/fowlbelief0/
References:
Bodybuilding steroids side effects photos
References:
https://bantooplay.com/@sanggatliff246?page=about
References:
What effect does steroids have on your body
References:
http://101.43.95.130:3001/lottiem0264365
References:
Bad side effects of steroids
References:
https://buzz.gi/@dereksellers4?page=about
References:
Street names for anabolic steroids
References:
http://zzdgitea.stnav.com/maryjonix3696
I just could not leave your site prior to suggesting that I actually enjoyed the usual
info a person provide for your visitors? Is going to be back incessantly in order to check up on new
posts
References:
https://my.vipaist.ru/user/hoseslice0/
References:
Instant Casino Spielanbieter
References:
https://diego-maradona-ar.org/user/yearcloset88/
References:
Instant Casino App Android
References:
https://travelersqa.com/user/cattlebeech37
References:
Instant Casino App Android
References:
https://bbs.pku.edu.cn/v2/jump-to.php?url=https://de.trustpilot.com/review/hanffidel.de
References:
Instant Casino Jackpot Spiele
References:
https://g.clicgo.ru/user/timerramie3/
References:
Sun coast casino
References:
https://justbookmark.win/story.php?title=beyond-the-game-experience-over-6000-titles-exclusive-bonuses-at-winspirit-canada
References:
Fantasy springs casino
References:
https://aryba.kg/user/pololace19/
References:
2ahukewjkv_v5usvnahvlip4khu09akmq420oa3oecakqcq|the best steroids for muscle growth
References:
https://trade-britanica.trade/wiki/Winstrol_Erfahrungen_Zyklus_Winstrol_Kaufen_Kur_2026
References:
Scottsdale casinos
References:
https://www.udrpsearch.com/user/oceangram61
References:
How long is a steroid cycle
References:
https://mensvault.men/story.php?title=spiropent-100-st-mit-dem-e-rezept-kaufen
References:
Xtreme muscle pro review
References:
https://mahavet.org/2026/04/dianabol-review-side-effects-benefits-and-results-2026/
References:
Safe and legal steroids
References:
https://jobcop.ca/employer/buy-winstrol-10-mg-stanozolol-oral-tablets-by-dragon-pharma/
top selling muscle building supplements
References:
https://intensedebate.com/people/bottlesink89
References:
Online Casino Echtgeld Google Pay
References:
https://tvoyaskala.com/user/hairbrush82/
References:
Niagra falls casino
References:
https://diego-maradona-ar.org/user/refundcongo4/
References:
Mahjong time
References:
https://forum.issabel.org/u/potatodeath4
crazy mass legal steroids
References:
http://okprint.kz/user/dinghybun04/
buying online steroids
References:
https://postheaven.net/denimstream3/buy-1-andro-for-sale-99-pure-usa-made
References:
Rocketplay casino accept neosurf
References:
https://zenwriting.net/nylonband59/rocketplay-neosurf-deposit-guide-play-real-money
References:
High stake poker rooms
References:
https://undrtone.com/animalpain5
References:
Blue heron casino
References:
https://graph.org/Casino-Online-Australia-Top-Licensed-Sites–Reviews-04-20
References:
Best au internet casinos
References:
https://graph.org/Stay-Casino-04-20
References:
Casinos london
References:
https://casino-royale-filme.online-spielhallen.de/
References:
Calgary casinos
References:
https://casino-welcome-bonus.online-spielhallen.de/
References:
Mannheim
References:
https://gringo-casino.online-spielhallen.de/
References:
Heidelberg
References:
https://lasvegas-casino.online-spielhallen.de/
References:
Casino udbetalingsprocent sammenligningsside
References:
https://syq.im:2025/veroniquejcj48/8081udbetalingshastighed-og-procenter-casino/wiki/Sikkerhed-i-online-casinoer-s%C3%A5dan-tjekker-du-det
References:
Top online casinoer med høj udbetaling
References:
https://a-taxi.com.ua/user/thrillplot78/
References:
Bedste casinoer uden udbetalingsgrænse
References:
https://conexaempregos.com.br/companies/beste-online-casino-auszahlungsquoten-hohe-gewinnchancen/
References:
Sider med højeste udbetalingsprocent på slots
References:
https://precisionscans.net/employer/schnelle-auszahlung-casino-2026-sofort-gewinne-abheben/
References:
Slots of vegas no deposit bonus codes https://code.wemediacn.com/tanishahampton/tanisha2020/wiki/Best-Online-Casinos-Australia-for-2026%3A-80%2B-Sites-Ranked
References:
Tuscany suites and casino https://jobs.maanas.in/institution/new-microgaming-bonus-codes-for-may-2026/
References:
Sands regency casino reno nv https://mahalkita.ph/@amjmilo5222230
References:
Slot machine games for android http://dev-gitlab.dev.sww.com.cn/roycereeves06
The overall structure of this post feels very natural and organized, since the ideas connect naturally and the discussion stays interesting without becoming too difficult for readers to follow carefully.
Хуй как у коня