简体   繁体   English

动画 svg 加载图标直到图像加载

[英]animated svg loading icon until image loaded

I am trying to achieve the behavior with CSS (or js if needed) for a svg icon .我正在尝试使用 CSS(或 js,如果需要)实现svg 图标的行为。 The structure of my HTML is something like this:我的 HTML 的结构是这样的:

<picture>
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg" class="img load">
</picture> 

I found this simple CSS solution :我找到了这个简单的 CSS 解决方案

.load {
  background: transparent url('loading-icon.svg') no-repeat center;
}

But, I need spin effect on the icon.但是,我需要对图标进行旋转效果。 I can't use gif image as the original icon is custom icon.我不能使用 gif 图像,因为原始图标是自定义图标。 So, I needed to put the icon in pseudo element :所以,我需要将图标放在伪元素中

picture:before {
  width: 50px;
  height: 50px;
  content: '';
  position: absolute;
  background: url('loading-icon.svg');
  /* top: 50%; */
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: -1;
  animation: spin 2s linear infinite;
}

Now, to be honest, I am not sure I have implemented it correctly or not as I can't see the icon loading.现在,老实说,我不确定我是否正确实施了它,因为我看不到图标正在加载。 I saw icon loading when I used png image to test;当我使用png图像进行测试时,我看到图标正在加载; but after using svg image, I can't see it loading.但是在使用svg图片后,我看不到它正在加载。 Can you please take a look and let me know if there are any issues or need any improvements?您能否看一下,如果有任何问题或需要改进,请告诉我?

Fiddle Demo小提琴演示

add background-size: 100%;添加background-size: 100%; in picture:before .picture:before because your loading picture is bigger than box size.因为您的加载图片大于盒子尺寸。

I am trying to achieve the behavior with CSS (or js if needed) for a svg icon .我正在尝试使用 CSS(或 js,如果需要)来实现svg 图标的行为。 The structure of my HTML is something like this:我的 HTML 的结构是这样的:

<picture>
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg" class="img load">
</picture> 

I found this simple CSS solution :我找到了这个简单的 CSS 解决方案

.load {
  background: transparent url('loading-icon.svg') no-repeat center;
}

But, I need spin effect on the icon.但是,我需要对图标产生旋转效果。 I can't use gif image as the original icon is custom icon.我不能使用 gif 图像,因为原始图标是自定义图标。 So, I needed to put the icon in pseudo element :所以,我需要把图标放在伪元素中

picture:before {
  width: 50px;
  height: 50px;
  content: '';
  position: absolute;
  background: url('loading-icon.svg');
  /* top: 50%; */
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: -1;
  animation: spin 2s linear infinite;
}

Now, to be honest, I am not sure I have implemented it correctly or not as I can't see the icon loading.现在,老实说,我不确定我是否正确实现了它,因为我看不到图标加载。 I saw icon loading when I used png image to test;当我使用png图像进行测试时,我看到了图标加载; but after using svg image, I can't see it loading.但在使用svg图像后,我看不到它正在加载。 Can you please take a look and let me know if there are any issues or need any improvements?如果有任何问题或需要任何改进,请您看一下,让我知道吗?

Fiddle Demo小提琴演示

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

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