css实现图片未加载完成时占位显示
作者:程序员11 时间:2020-06-13 人气:689 QQ交流群\邮箱:1003265987@qq.com原理:通过在img标签的after伪元素上添加一张占位图,并且img都设置为position:relative;after设置position:absolute;img标签的src为网络图片,这样加载的时候由于网络图片没加载完成,就会显示本地图片,下面案例中的js是为了效果明显而故意延时设置img的src属性。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Documenttitle>
<style>
img {
position: relative;
}
img::after {
content: "";
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
background: url(iphonex.png ) no-repeat center;
}
style>
head>
<body>
<img src="">
body>
<script>
setTimeout(function() {
document.querySelectorAll("img")[0].src = 'http://upload-images.jianshu.io/upload_images/7450593-65067eb4cf76d882.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240';
}, 3000);
script>
html>
欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。
- 上一篇:获取DOM元素到页面顶部的距离
- 下一篇:img.onload 实现图片预加载方法
HTML5-热门文章
活跃用户












