Table of Contents
6.1 How elves work
6.2 The importance of sprites
6.2.3 Compass processing sprite solution
6.3 Making sprites with Compass
6.3.1 Creating a sprite map
6.3 .2 Introduce the sprite and generate CSS
6.4 Configure the Compass sprite
6.4.1 Customized sprite map
6.4.2 Custom sprite CSS
6.5 Controlling Elf Assistant
6.5.1 Creating Elf Map
6.5.2 Writing the CSS of the sprites
6.6 Summary
Home Web Front-end HTML Tutorial (3) Sass and Compass create sprite pictures_html/css_WEB-ITnose

(3) Sass and Compass create sprite pictures_html/css_WEB-ITnose

Jun 24, 2016 am 11:44 AM

6.1 How elves work

// Merge various images into one image, and change the position of the background image in different states;

6.2 The importance of sprites

// Compress image memory;

// Reduce HTTP requests

6.2.3 Compass processing sprite solution

/// 1. Let Compass point to a sprite’s folder;

// 2. Tell Compass to write the sprite CSS;

// 3. Compile the style sheet;

6.3 Making sprites with Compass

6.3.1 Creating a sprite map

1 @import "compass/utilities/sprites";    // 精灵图片控件;2 @import "Icon/*.png";                   //<strong> Icon文件夹位于images文件夹内并包含所有精灵图片;</strong>3 4 //<strong> 生成精灵图片位于images文件夹内</strong>;并生成编译后的CSS;5 CSS: 6     .Icon-sprite {             // 自动生成的类命名; 7       background-image: url('/images/Icon-s040daee5af.png'); 8       background-repeat: no-repeat; 9     }
Copy after login

6.3 .2 Introduce the sprite and generate CSS

 1 1.<strong>all-</strong><strong>sprites精灵混合器</strong>  2 Sass:  3     <strong>@include all-Icon-sprites; // 为整个精灵地图撰写所有必要的CSS;</strong> 4 CSS:                     // 生成内容;  5     .Icon-sprite, .Icon-index1, .Icon-index2, .Icon-index3, .Icon-index4, .Icon-index5 {  6         background-image: url('/images/Icon-s040daee5af.png');  // <strong>所有的相关类都引用此图片; </strong> 7         background-repeat: no-repeat;  8  }  9     .Icon-index1 { 10         background-position: 0 0; 11  } 12  ... 13     .Icon-index5 { 14         background-position: 0 -560px; 15  } 16 17 // 使用@extend继承精灵的样式18 Sass: 19     .add-button { @extend .Icon-index1} //<strong> 这种方法会生成更少的CSS;</strong>20 CSS: 21     .Icon-index1, .add-button {     //<strong> 直接叠加了一个类,比较方便; </strong>22         background-position: 0 0; 23     }
Copy after login

 1 <strong>2.single-sprite精灵混合器 </strong> 2 Icon-sprite($name);                     //<strong> 输出一个独立命名精灵的CSS</strong>; $name:单独图片的名字(index1); 3 Sass:  4     .add-button2 {  5         @include Icon-sprite(index5);  6  }  7 CSS:  8     .add-button2 {  9         background-position: 0 -560px; // 与index5对应的图片的位置; 10     }
Copy after login

6.4 Configure the Compass sprite

6.4.1 Customized sprite map

// You can customize a sprite map or define the sprite specifically through its configuration variables ;

// :The name of the file that stores the sprite;

// :The name of a separate sprite image;

// The following configuration All variables need to be defined before importing sprites ("@include all-Icon-sprites");

1 <strong>1.配置精灵图之间的间距 </strong>2 $<map>-spacing:Xpx;                     // 设置所有精灵图之间的间距为Xpx;3 $<map>-<sprite>-spacing:Xpx;            // 设置某个精灵图的间距;4 Sass: 5     <strong>$Icon-spacing:4px;</strong>                  //<strong> 所有图片的间距为4px;</strong>6     <strong>$Icon-index1-spacing:12px</strong>;          //<strong> index1图片的间距为12px;</strong>
Copy after login

1 <strong>2.设置精灵的重复性 </strong>2 $<map>-repeat:no-repeat/repeat-x;       // 默认值是no-repeat;设置为repeat-x可以使其X轴平铺; 3 $<map>-<sprite>-repeat:no-repeat/repeat-x;  // 设置单个图片的重复属性;4 Sass: 5     <strong>$Icon-index2-repeat:repeat-x;</strong>       // index2图片在精灵图片里边横向平铺;
Copy after login

<strong>3</strong><strong>.设置精灵的位置</strong> $<map>-position:Xpx;                      //<strong> 垂直向右(水平向下)移动Xpx距离;</strong>$<map>-<sprite>-position:Xpx; Sass: <strong>$Icon</strong><strong>-positioin: 10px;         // 所有图片向右10px;</strong>
Copy after login

1<strong> 4.设置精灵地图的布局 </strong>2 $<mpa>-layout: <strong>vertical(垂直)</strong>/<strong>horizontal(水平)</strong>/diagonal(对角线)/<strong>smart(产生最小的空白区域)</strong>;  // 默认布局是vertical;  $Icon-layout:horizontal;        // 在引入sprites模块之前定义;
Copy after login

1 <strong>5.清除过期的精灵地图 </strong>2 $<map>-clean-up:true/false;
Copy after login

6.4.2 Custom sprite CSS

 1 <strong>1.输出精灵的尺寸 </strong> 2 $<map>-sprite-height($name);            // 得到某个精灵图片的高度; 3 $<map>-sprite-width($name);  4 $<map>-sprite-dimensions:true/false;    // 为精灵地图中的每个精灵自动输出尺寸;  5 Sass:  6  .next {  7         @include Icon-sprite(index2);  8         width:<strong>Icon-sprite-width(index2)</strong>;// 得到精灵图片的大小; 9  } 10 CSS: 11  .next { 12         background-position: -10px -150px; 13  width: 140px;           // 生成的图片大小; 14     }
Copy after login

 1 <strong>2.精灵的基础类 </strong> 2 // Compass可以方便地通过生成一个基础类为每个精灵应用普通样式; 3 $<map>-sprite-base-class:".class-name";  4 // 当使用全部精灵或单独精灵的混合器时,Compass会输出一个精灵的基础类;并且其<strong>选择器还会串联所有设置了background-image属性的选择器</strong>; 5 //<strong> 每个精灵地图的基础类都以其文件夹的名字命名</strong>; 6 Sass:  7     <strong>$Icon-sprite-base-class: ".Icon"; </strong> 8     .Icon {                             //<strong> 设置精灵的CSS基础类;</strong> 9  overflow: hidden; 10         width:Icon-sprite-width(index1); 11  } 12 CSS: 13     .Icon, .Icon-index1, .Icon-index2, .Icon-index3, .Icon-index4, .Icon-index5 { 14  overflow: hidden; 15  width: 140px; 16     }
Copy after login

 1 <strong>3.魔术精灵选择器 </strong> 2 $disable-magic-sprite-selectors:true/false; 3 // 魔术精灵选择器是默认开启的,也就是说Compass在精灵时会<strong>根据以"_hover"/"_active"或"_target"结尾的名字自动输出CSS的:hover/:active和:target伪选择器;</strong> 4 // 在Icon文件夹内添加<strong>index4_hover.png</strong>之后会自动<strong>生成关于index-4:hover的类及相关代码</strong>; 5 CSS:  6     .Icon-index4 {  7         background-position: 0 -420px;  8  }  9     <strong>.Icon-index4:hover</strong>, .Icon-index4.<strong>index4-</strong><strong>hover</strong> { 10         background-position: 0 -560px; 11     }
Copy after login

6.5 Controlling Elf Assistant

6.5.1 Creating Elf Map

// Previous "@import ' Icon/*.png'", not only creates a sprite map, but also sets mixers and variables for the sprite map and each sprite ;

1 <strong>1.sprite-map辅助器 </strong>2 $Icon:<strong>sprite-map</strong>("Icon/*.png",<strong>$layout:smart</strong>);   // 它会创建一个智能布局的精灵地图,并把精灵地图的图片URL赋值给$Icon变量;
Copy after login

1 <strong>2.sprite-map辅助器--设置单个精灵 </strong>2 $Icon:sprite-map("Icon/*.png",$index2-spacing:5px);
Copy after login

6.5.2 Writing the CSS of the sprites

// After Compass generates the sprite map for you, you still need to write the CSS of each sprite;

 1 <strong>1.sprite辅助器 </strong> 2 <strong>sprite</strong>($map,$sprite,[$offset-x],[$offset-y]);  // $map:精灵基础类; $sprite:单个图片名,用于定位背景图片;  3 // sprite辅助器需要精灵地图/精灵的名字以及可选的偏移坐标; 4 Sass:  5     $Icon:sprite-map("Icon/*.png",$layout:smart);  6     //<strong> 精灵基础类的一个优点就是只需要赋值一次背景图片(把路径复制到变量中)</strong>; 7  .next {  8         background:<strong>sprite($Icon,index2) no-</strong><strong>repeat</strong>;  9  } 10 //<strong> 这仅仅会输出背景属性,而不会成为一个精灵的基础类或其他任何不需要的CSS</strong>;11 CSS: 12  .next { 13         background: url('/images/Icon-s6558f78e4f.png') 0 -140px no-repeat; 14     }
Copy after login

 1 <strong>2.设置精灵的位置 </strong> 2 // 为了移除重复的背景图片,你可以用sprite-position辅助器或sprite-background-position混合器取代sprite辅助器; 3 Sass:  4     $Icon:sprite-map("Icon/*.png");             //<strong> 辅助器创建精灵地图</strong>; 5     .sprite-base { background:$Icon no-repeat; }// 引入精灵地图; 6  .next {  7         <strong>@extend .sprite-base</strong>;                   // @extend引用; 8         background-position:sprite-position($Icon, index2);  9         // 设置background-position属性;10         // sprite-position:辅助器,用于定位图片位置;11         // $Icon:变量,引入精灵图片路径;12         // index2:定位精灵图片index2位置的参数;13  } 14 CSS: 15     <strong>.sprite-</strong><strong>base, .next</strong> { 16         background: url('/images/Icon-sb501daeae5.png') no-repeat; 17  } 18  .next { 19         background-position: 0 -140px; 20     }
Copy after login

 1 <strong>3.设置精灵的尺寸--sprite-dimensisons混合器 </strong> 2 //<strong> 它需要精灵地图和精灵的名字,并输出经过测量的尺寸;</strong> 3 Sass:  4     $Icon:sprite-map("Icon/*.png");             // 辅助器创建精灵地图; 5     .sprite-base { background:$Icon no-repeat; }// 引入精灵地图; 6  .add {  7         @extend .sprite-base;  8         @include sprite-background-position($Icon,index3);  // 精灵图片定位辅助器; 9         @include sprite-dimensions($Icon,index3); 10     }
Copy after login

6.6 Summary

// 1. Load a large amount of data from the remote server The impact of images on performance and how sprite images are an important method to solve high-traffic website problems;

// 2. How Compass completely automates processing of sprites , and explored configuration and control Several ways for Compass to generate sprite maps and CSS ;

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles