vue.js - 如何为图像制作预加载器?

标签 vue.js

我有照片

<img class="block" :src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/1024px-Google_%22G%22_Logo.svg.png" />

但是如何在主图片未加载时显示其他图片?

最佳答案

    <template> <div>
       <img class="loading" v-if="pending" alt="image" /> <!-- This is a beautiful animation -->
       <img src="your-image.png" v-if="pending" alt="image" /> <!-- If you want to put another optional picture you will do so -->
       <img v-else :src="img" class="block" alt="image" /> </div>
    </template>

<script>
export default {
  data() {
    return {
      pending: false,
      img: ''
    };
  },
  created() {
      this.pending = false
      // your get request
       fetch("https://example-api.com/image")
      .then((data) => (this.img = data.img))
      .finally(() => {
       this.pending = false;
      });
  },
};
</script>

<style lang="scss">
 .loading {
  position: relative;
  user-select: none;
  cursor: wait;
  transition: all 0.2s;
  border-radius: 4px !important;
  border: none;
  background: transparent;
  overflow: hidden;
  color: transparent !important;

  &:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 100%;
    background-color: #e8ecf1 !important;
    cursor: wait;
    -webkit-mask-image: radial-gradient(white, black);
    transition: all 0.2s;
  }

  &:after {
    animation: loading 1.5s infinite;
    content: "";
    height: 100%;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    transform: translateX(-100%);
    z-index: 1;
    background: linear-gradient(
      90deg,
      rgba(255, 255, 255, 0),
      rgba(255, 255, 255, 0.5),
      rgba(255, 255, 255, 0)
    );
  }

  svg {
    opacity: 0;
  }
}
</style>

关于vue.js - 如何为图像制作预加载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72313544/

相关文章:

javascript - vue js table 加新行时类似Stack Overflow的高亮效果

vue.js - 在子项 vueJS.2 之间切换 "active"类

vue.js - 如何将 Vuetify 注入(inject)我的自定义 vue 插件

vue.js - Microsoft Graph 在身份验证后记住用户

vue.js - 如何解决vue中避免冗余导航到当前位置错误?

javascript - 如何让 Vue.js 对动态添加的数组属性的更改使用react?

javascript - 从 Vue 应用程序在终端中运行本地外部 python 脚本

javascript - 转到Vuejs 2中的特定路线

vue.js - Vue.component 不显示子组件

javascript - 为什么 Vue 没有初始化?