css第一次使用@keyframes加载图片时,会出现白屏(splashscreen)
时间:2023-03-30 17:53:02
CSS
问题解释:使用css的animations属性时,第一次加载循环会出现白色间隙。看起来有点像页面在刷新,以后每次循环都不会出现这个问题。演示//html
//less.container{width:100vw;高度:100%;}.container.first{宽度:100vw;高度:100vh;animation:bgmove10sinfinite;}@keyframesbgmove{0%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1.jpg")无重复中心中心;背景尺寸:封面;不透明度:1;}15%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1_1.jpg")无重复中心center;不透明度:1;背景尺寸:封面;}30%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-2_1.jpg")no-repeatcentercenter;不透明度:1;背景尺寸:封面;}45%{背景:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-4_1.jpg")无重复中心center;opacity:1;background-size:cover;}60%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-5_1.jpg")无重复中心center;opacity:1;background-size:cover;}80%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firscreen_img/BG-1_1.jpg")无重复中心center;opacity:1;background-size:cover;}100%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firscreen_img/BG-1.jpg")无重复中心center;background-size:cover;opacity:1;}}解决方法//less.container{width:100vw;height:100%;}.container.first{width:100vw;height:100vh;background:url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-1.jpg"),url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-1\_1.jpg"),url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-2\_1.jpg"),url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-4\_1.jpg"),url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-5\_1.jpg"),url("https://sqimg.qq.com/qq\_product\_operations/im/pcqq/9.0/firstscreen\_img/BG-1\_1.jpg");animation:bgmove10sinfinite;}@keyframesbgmove{0%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1.jpg")否-repeatcentercenter;background-size:cover;opacity:1;}15%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1_1.jpg")no-repeatcentercenter;opacity:1;background-size:cover;}30%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-2_1.jpg")无重复中心center;op城市:1;背景尺寸:封面;}45%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-4_1.jpg")无重复中心center;不透明度:1;背景尺寸:封面;}60%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-5_1.jpg")无重复中心center;不透明度:1;背景尺寸:封面;}80%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1_1.jpg")无重复中心center;不透明度:1;背景尺寸:封面;}100%{background:url("https://sqimg.qq.com/qq_product_operations/im/pcqq/9.0/firstscreen_img/BG-1.jpg")无重复中心center;背景尺寸:封面;不透明度:1;}}最终效果问题分析图片的加载一般有两种情况,图片预加载和图片延迟加载,这里涉及到图片的预加载问题,当页面图片过多时,对服务器造成压力会很高,而且加载的图片一次会显示的不连贯,所以第一次加载所有图片,这样后面用到这些图片的时候即使资源缓存在本地,加载速度也会快很多,不会出现白色故障