Home  >  Article  >  Web Front-end  >  How to implement image cutting technology through Sprites in CSS (code attached)

How to implement image cutting technology through Sprites in CSS (code attached)

yulia
yuliaOriginal
2018-09-11 15:31:341746browse

CSS Sprites Advantages and Disadvantages

Advantages

1. Using CSS Sprites can greatly reduce the http requests of web pages, thereby greatly Improving the performance of the page is also the biggest advantage of CSS Sprites and the main reason why it is widely spread and applied;
2. CSS Sprites can reduce the bytes of images. I have compared three images and merged them into one. The bytes of the picture are always smaller than the sum of the bytes of these three pictures.
3. It solves the problem of web designers in naming pictures. They only need to name a collection of pictures. There is no need to name every small element, thus improving the efficiency of web page production.
4. It is easy to change the style. You only need to modify the color or style of one or a few pictures, and the style of the entire web page can be changed. Maintenance is more convenient.

Disadvantages

It is true that CSS Sprites are so powerful, but there are also some shortcomings that cannot be ignored, as follows:
1. When merging images, you Multiple pictures must be merged into one picture in an orderly and reasonable manner, and enough space must be left to prevent unnecessary backgrounds from appearing in the section; these are okay, but the most painful thing is to do it on a widescreen, high-resolution screen. For adaptive pages, if your picture is not wide enough, it is easy for the background to break;
2. CSS Sprites are more troublesome during development. You have to measure and calculate the precise position of each background unit through photoshop or other tools. This is Needlework is not difficult, but it is very tedious; fortunately, Tencent's Gui Ge developed a CSS Sprites style generation tool using ADOBE AIR. Although it is still somewhat inflexible in use, it is much more convenient than photoshop measurement, and the style Directly generate, copy, and copy is OK!
3. CSS Sprites are more troublesome to maintain. If there is a slight change in the page background, you usually need to change the merged image. It is best not to change the parts that do not need to be changed, so as to avoid changing more css. If in The original place cannot fit, and the only option (preferably) is to add the image down, which will increase the bytes of the image and require changes to the css.
4.CSS Sprites are very worth learning and applying, especially if the page has a bunch of icos (icons). In short, many times we have to weigh the pros and cons before deciding whether to use CSS Sprites.

HTML code:

<body>
<!-- ul.sprite>li*5>s.s-icon+a{CSS Sprite} -->
<!-- 以上是Sublime Text快速拼写 -->
<ul class="sprite">
<li><s class="s-icon"></s><a href="">CSS Sprite</a></li>
<li><s class="s-icon"></s><a href="">CSS Sprite</a></li>
<li><s class="s-icon"></s><a href="">CSS Sprite</a></li>
<li><s class="s-icon"></s><a href="">CSS Sprite</a></li>
<li><s class="s-icon"></s><a href="">CSS Sprite</a></li>
<div class="clear"></div>
</ul>
</body>

CSS code:

<style>
body { background-color: #fc0; color:#333;}
* {margin:0; padding:0;}
ul,li {list-style: none;}
a { color:#f00; font-weight: bold; text-decoration: none;}
.sprite {margin:0 auto; width:1000px; padding-top: 50px;}
.sprite li {float:left; margin-left: 50px; text-align: center; cursor:pointer; }
.sprite li s { display: block; width:132px; height:112px; background:url(all.png) no-repeat; }
.clear { clear:both;}
</style>

JS code:

<script src="<a href="http://code.jquery.com/jquery-latest.js"></script">http://code.jquery.com/jquery-latest.js"></script</a>>
<script>
$(function () {
var iconH = $(".sprite").find("s").height(),
//找出存放图片的容器的高度;
triggerLi = $(".sprite").children("li");
//找出每一个li,放到一个数组中;
//console.log(iconH);
//在控制台打印出容器的高度;
triggerLi.each(function () {
//遍历数组中的每一个li
var $this = $(this),
//声明变量赋值当前的li;
$index = $this.index();
//声明变量保存当前li的index值;
//console.log($index);
//在控制台打印出每一个li的index值;
//console.log(iconH*$index);
//得出每一个图片对应的position值;
$this.children("s").css("background-position","0 -"+iconH*$index+"px");
//利用js遍历出每一个s标签的背景图片;
$this.hover(function() {
//鼠标移入
$this.children("s").css("background-position","-132px -"+iconH*$index+"px");
}, function() {
//鼠标移出
$this.children("s").css("background-position","0 -"+iconH*$index+"px");
});
})
})
</script>

The above is the detailed content of How to implement image cutting technology through Sprites in CSS (code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn