您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在vue中如何实现图片loading组件

2024/8/21 21:08:14发布157次查看
下面我就为大家分享一篇vue实现图片加载完成前的loading组件,具有很好的参考价值,希望对大家有所帮助。
如下所示:
<template> <img :src="url"></template><script> export default { props: ['src'], // 父组件传过来所需的url data() { return { url: 'http://www.86y.org/images/loading.gif' // 先加载loading.gif } }, mounted() { var newimg = new image() newimg.src = this.src newimg.onerror = () => { // 图片加载错误时的替换图片 newimg.src = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489486509807&di=22213343ba71ad6436b561b5df999ff7&imgtype=0&src=http%3a%2f%2fa0.att.hudong.com%2f77%2f31%2f20300542906611142174319458811.jpg' } newimg.onload = () => { // 图片加载成功后把地址给原来的img this.url = newimg.src } } }</script>
以下为纯js代码
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>loading</title></head><body> <img id="img"> <script> window.onload = () => { var img = document.queryselector('#img'); img.src = 'http://www.86y.org/images/loading.gif'; // 先加载loading.gif var newimg = new image(); newimg.src = 'https://avatars3.githubusercontent.com/u/1?v=3'; newimg.onerror = () => { // 图片加载错误时的替换图片 newimg.src = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489486509807&di=22213343ba71ad6436b561b5df999ff7&imgtype=0&src=http%3a%2f%2fa0.att.hudong.com%2f77%2f31%2f20300542906611142174319458811.jpg'; } newimg.onload = () => { // 图片加载成功后把地址给原来的img img.src = newimg.src } } </script></body></html>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
如何实现ajax前台后台跨域请求
自定义ajax支持跨域组件(详细教程)
通过微信小程序如何实现验证码获取倒计时效果
以上就是在vue中如何实现图片loading组件的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product