Home Web Front-end HTML Tutorial css sprites_html/css_WEB-ITnose

css sprites_html/css_WEB-ITnose

Jun 24, 2016 am 11:25 AM

css sprites是什么呢?

简而言之,它就是将网页中的许多小图片,按照约定好的规则,将这些小图片绘制到一张大图片上,然后利用background中的background-position将需要的图片,扣出来。

可能你会问,这样做是为什么?

其实就是减少http请求,提高网站性能。其实因为每一张图片去服务端下载,都需要一次完整的HTTP请求,当许多图片同时下载时,那么就会发出相应数量的http请求,然而,不同的浏览器处理图片下载的数量是有限制的,一般在1-10范围中,所以,倘若你网站中的图片很多,那么不管你网速有多快,其势必影响网站性能。这就是为什么需要css sprites技术。

好了,我们来一起写个demo,举个例子看看。

倘若我要实现一个控制按钮器,如下图所示:

 

每当我鼠标移到图中的每个控制器按钮时,都会触发一个事件,让其变成红色区域,那么可见,每个按钮都是独立且有功能的,你可能会将每个区域都设置成一个张小图片。

但,那得向后台请求多少次呢!!!

优化网站性能的其中之一,就是减少http请求。

所以这里需要图片合并减少http请求,我们就用上了css sprites了。

首先,我们按照一定的规律,将需要的所有小图片合并成一张大图,制作出我们需要的图片。

下图为鼠标未移到每个按钮上时的状态:

下图为鼠标移到每个按钮时的状态:

然后,我们再将每个按钮区域的大小设置为宽31px,高29px以及背景图片定义为正常情况下的图片,及上述图一。

你可能会问,为什么是宽31高29?

因为每张小图片,即每个按钮的宽高是这么多嘛。

接下来,我们就利用background-position属性来将我们需要的部分图片,扣下来,如我需要,那么我就将background-position设置为0 0,就ok了。

最后,当鼠标移到相应按钮时,其变成浅红色状态的图案时,也是如此,利用 background加载大图,background-position扣下需要的小图片。

实现后的详细代码,见下

<!DOCTYPE html>    <head>        <title>js_test</title>        <style type="text/css">            .ptz {                width:31px;                height:29px;                margin-right:1px;                margin-bottom:6px;                cursor:pointer;                float:left;                background:url(ptz.gif) no-repeat;            }            .tmain, .mmain, .bmain {                clear:left;            }            .tl {                background-position:0 0;             }            .tm {                background-position:-34px 0;             }            .tr {                background-position:-68px 0;             }            .add {                background-position:-102px 0;             }            .search {                background-position:-136px 0;             }            .subtract {                background-position:-170px 0;            }            .ml {                background-position:0 -32px;            }            .mm {                background-position:-34px -32px;                }            .mr {                background-position:-68px -32px;             }            .focus {                background-position:-136px -32px;             }            .bl {                background-position:0 -64px;            }            .bm {                background-position:-34px -64px;            }            .br {                background-position:-68px -64px;            }            .iris {                background-position:-136px -64px;            }            .slide {                width:192px;                height:18px;                clear:left;                background-position:0 -96px;            }            .slideBtt {                width:14px;                height:16px;                margin-left:22px;                background-position:0 -121px;            }            .indicate {                clear:left;                width:189px;                height:29px;                border:1px solid #C1C0C0;                 border-radius:3px;            }            .outterBtt {                float:left;                border-right:1px solid #C1C0C0;                width:37px;                height:100%;            }            .lastOut {                border:0;            }            .btt1 {                width:14px;                height:14px;                margin:8px 0 0 12px;                background-position:-28px -127px;            }            .btt2 {                width:20px;                height:20px;                background-position:-57px -127px;                margin:7px 0 0 11px;            }            .btt3 {                width:12px;                height:16px;                background-position:-90px -128px;                margin:8px 0 0 10px;            }            .btt4 {                width:20px;                height:18px;                background-position:-118px -128px;                margin:9px 0 0 9px;            }            .btt5 {                width:15px;                height:15px;                background-position:-152px -128px;                margin:8px 0 0 12px;            }            .tmain div:hover,             .mmain div:hover,             .bmain div:hover,             .outterBtt div:hover {                background-image:url(ptzon.gif);              }                    </style>    </head>    <body>                <div class="tmain">            <div class="ptz tl"></div>             <div class="ptz tm"></div>              <div class="ptz tr"></div>              <div class="ptz add"></div>             <div class="ptz search"></div>            <div class="ptz subtract"></div>        </div>                <div class="mmain">            <div class="ptz ml"></div>            <div class="ptz mm"></div>              <div class="ptz mr"></div>              <div class="ptz add"></div>             <div class="ptz focus"></div>            <div class="ptz subtract"></div>        </div>                <div class="bmain">            <div class="ptz bl"></div>            <div class="ptz bm"></div>              <div class="ptz br"></div>              <div class="ptz add"></div>             <div class="ptz iris"></div>            <div class="ptz subtract"></div>        </div>                <div class="ptz slide">            <div class="ptz slideBtt"></div>        </div>                <div class="indicate">            <div class="outterBtt">                <div class="ptz btt1"></div>            </div>            <div class="outterBtt">                <div class="ptz btt2"></div>            </div>            <div class="outterBtt">                <div class="ptz btt3"></div>            </div>            <div class="outterBtt">                <div class="ptz btt4"></div>            </div>            <div class="outterBtt lastOut">                <div class="ptz btt5"></div>            </div>        </div>    </body></html>
Copy after login

 

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
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
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