0x00 为什么要用精灵图sprites
网页所使用的小图标很多,如果分开储存,则会多次向服务器发送请求,增大服务器的负载,降低页面的加载速度。
0x01 精灵图原理
精灵图主要针对背景图片,将多个背景图片整合到一张图片中
精灵图的本质就是通过bgc-position来移动图片位置,达到显示想要的背景图片的效果
因此,使用精灵图需要精确测量各背景图片位置
0x02 实例
使用精灵图显示自己的名字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="box">
<span class="j"></span>
<span class="w"></span>
<span class="t"></span>
</div>
</body>
<style>
.box span {
display: inline-block;
}
.j {
background: url("abcd.jpg") no-repeat -389px -141px;
width: 102px;
height: 112px;
}
.w {
background: url("abcd.jpg") no-repeat -117px -560px;
width: 135px;
height: 112px;
}
.t {
background: url("abcd.jpg") no-repeat -368px -417px;
width: 102px;
height: 112px;
}
</style>
</html>