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

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

Sep 11, 2018 pm 03:31 PM

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>
Copy after login

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>
Copy after login

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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles