听到雪碧图是不是跟我一样,啥?雪碧图?不知所以。
如果一张大图由许多小图构成,那么每张小图都需要进行网络请求,这样一来就会有很多请求,为了减少网络请求,雪碧图就出现了。
雪碧图(Sprite)是一种在Web开发中常用的图像合并技术,将多个小图标或图片合并成一张大图,然后通过CSS来显示其不同部分。这种技术可以减少HTTP请求数量,提高网页加载速度。
以下是本人自己写的例子,大图的图片是在百度找的https://img2.baidu.com/it/u=2480900908,627122939&fm=253&fmt=auto&app=138&f=JPEG?w=602&h=231
原图长这样
代码运行长这样:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
}
.icon1{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -5px 1px;
/* border: 1px solid #000; */
}
.icon2{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -95px 1px;
}
.icon3{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -185px 1px;
}
.icon4{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -275px 1px;
}
.icon5{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -365px 1px;
}
.icon6{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -455px 1px;
}
.icon7{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -545px 1px;
}
.icon2-1{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -5px -90px;
}
.icon2-2{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -95px -90px;
}
.icon2-3{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -185px -90px;
}
.icon2-4{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -275px -90px;
}
.icon2-5{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -365px -90px;
}
.icon2-6{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -455px -90px;
}
.icon2-7{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -545px -90px;
}
.icon3-1{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -5px -179px;
}
.icon3-2{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -95px -179px;
}
.icon3-3{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -185px -179px;
}
.icon3-4{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -275px -179px;
}
.icon3-5{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -365px -179px;
}
.icon3-6{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -455px -179px;
}
.icon3-7{
display: block;
background-image: url("@/../xuebitu.jpg");
width:50px;
height:55px;
background-position: -545px -179px;
}
</style>
</head>
<body>
<div>
<span class="icon1"></span>
<span class="icon2"></span>
<span class="icon3"></span>
<span class="icon4"></span>
<span class="icon5"></span>
<span class="icon6"></span>
<span class="icon7"></span>
</div>
<div>
<span class="icon2-1"></span>
<span class="icon2-2"></span>
<span class="icon2-3"></span>
<span class="icon2-4"></span>
<span class="icon2-5"></span>
<span class="icon2-6"></span>
<span class="icon2-7"></span>
</div>
<div>
<span class="icon3-1"></span>
<span class="icon3-2"></span>
<span class="icon3-3"></span>
<span class="icon3-4"></span>
<span class="icon3-5"></span>
<span class="icon3-6"></span>
<span class="icon3-7"></span>
</div>
<!-- <span class="icon2"></span> -->
</body>
</html>