Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

15.5. 图片优化

15.5.1. onMouseOver/onMouseOut

我们在网站冲浪常常看会看到很多图片按钮,当鼠标入上去或鼠标移开图片会随之改变,这个的按钮至少需要三张小图片才能实现这样的功能。

我先说说这样做的缺点

  • 三张图片,你的浏览器会启动三个线程链接你的图片服务器,不划算。

  • 一旦其中一幅图片下载过程中中断,用户当把鼠标放到按钮上时,可能会出现一个红叉叉。

  • 图片太多不好维护,易产生垃圾,占用磁盘空间,linux ext3一个空文件占用2048

最优方法是使用一张图片,将三幅图片平行或垂直排开,放到一幅图片中,然后使用CSS控制显示你需要的部分。

15.5.2. 使用一幅图片处理BLOCK四角

corner.gif

stylesheet

			
<style type="text/css">
<!--

.clear { clear: both; height: 0; font-size: 0; line-height: 0; zoom: 1 }

.containerPlain {
	background-color: #fff;
	border-right: 1px solid #cacaca;
	border-left: 1px solid #cacaca;
	padding: 0 3px;
}

.left_top_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: top left;
	float: left;
	font-size: 0;
}

.right_top_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: top right;
	float: right;
	font-size: 0;
}

.left_bottom_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: bottom left;
	float: left;
	font-size: 0;
}


.right_bottom_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: bottom right;
	float: right;
	font-size: 0;
}
.left_bottom_corner, .right_bottom_corner ,
.left_top_corner, .right_top_corner{
	background-image: url(corners/corner.gif);
}

.middle_top_line {
	display: block;
	float: left;
	height: 3px;
	line-height: 0;
	border-top: 1px solid #cacaca;
}

.middle_bottom_line {
	display: block;
	float: left;
	height: 3px;
	border-bottom: 1px solid #cacaca;
	font-size: 0;
}

.middle_top_line, .middle_bottom_line {
		width: 167px;
}

-->
</style>
			
			

HTML

			
<div style="width:175px;">
	<span class="left_top_corner"></span> <span class="middle_top_line"></span> <span class="right_top_corner"></span>
	<div class="containerPlain">
    	You Content
	</div>
	<span class="left_bottom_corner"></span> <span class="middle_bottom_line"></span> <span class="right_bottom_corner"></span>
</div>
			
			

下面是一个更复杂的例子

stylesheet

			
<style type="text/css">
<!--

.clear { clear: both; height: 0; font-size: 0; line-height: 0; zoom: 1 }

.containerPlain {
	background-color: #fff;
	border-right: 1px solid #cacaca;
	border-left: 1px solid #cacaca;
	padding: 0 3px;
	clear: both;
}

.left_top_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: top left;
	float: left;
	font-size: 0;
}

.right_top_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: top right;
	float: right;
	font-size: 0;
}

.left_bottom_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: bottom left;
	float: left;
	font-size: 0;
}


.right_bottom_corner {
	display: block;
	width: 4px;
	height: 4px;
	background-position: bottom right;
	float: right;
	font-size: 0;
}
.left_bottom_corner, .right_bottom_corner ,
.left_top_corner, .right_top_corner{
	background-image: url(corners/corner.gif);
}

.middle_top_line {
	display: block;
	float: left;
	height: 3px;
	line-height: 0;
	border-top: 1px solid #cacaca;
}

.middle_bottom_line {
	display: block;
	float: left;
	height: 3px;
	border-bottom: 1px solid #cacaca;
	font-size: 0;
}

.middle_top_line, .middle_bottom_line {
		width: 167px;
}


.block_title {
	line-height: 26px;
	height: 26px;
	background-image: url(corners/block_title_left.png);
	background-repeat: no-repeat;
	padding-left: 10px;
	font-size: 13px;
	font-weight: bold;
	background-color: #dddbdc;
}

.block_title_right {
	display: block;
	background-image: url(corners/block_title_right.png);
	background-repeat: no-repeat;
	background-postition: right;
	float: right;
	width: 4px;
	height: 26px;
}
-->
</style>
			
			

HTML

			
<div style="width:175px;">
  <span class="left_top_corner"></span> <span class="middle_top_line"></span> <span class="right_top_corner"></span>
  <div class="containerPlain">
    <div class="block_title">
		<span class="block_title_right"></span> Title
    </div>
    <div style="padding: 10px 7px 7px 7px">
		Content
	</div>
  </div>
  <span class="left_bottom_corner"></span> <span class="middle_bottom_line"></span> <span class="right_bottom_corner"></span>
</div>
			
			

15.5.3. 图片用背景图代替 img 标记

			
图片用背景图代替<img src="">
			
			

15.5.4. 合并图片

下面是摘取LinkedIn网页,作为例子.

合并多张小图片为一张图片,然后通过偏移量取其中一部分显示,这样做的目的是,加快浏览器下载速度,降低与服务器建立连接的开销.

			
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>LinkedIn Blog</title>
	<style type="text/css">
/*
Theme Name:  LinkedIn Blog
Theme URI:   http://blog.linkedin.com/
Description: LinkedIn's main blog theme
Author:      Prajakta Godbole
Author URI:  http://linkedin.com/
Version:     2.0
*/

/*
Reset styles
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 2.9.0
*/
html{color:#000;background:#FFF}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,select,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,button,caption,cite,code,dfn,em,input,optgroup,option,select,strong,textarea,th,var{font:inherit}del,ins{text-decoration:none}li{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0;font-variant:normal}sup{vertical-align:baseline}sub{vertical-align:baseline}legend{color:#000}

/* Colors and fonts */
html { background-color: #F5F5F5; }
body { font-family: Arial, Helvetica, "Nimbus Sans L", sans-serif; padding-top: 20px;}
a { color: #006fb3; text-decoration: none; }
a:hover { color: #006fb3; text-decoration: underline;}

/* Sidebar */
#sidebar { width: 312px; float: left; margin-left: 20px;}
#sidebar .widgets { border: 1px solid #ddd; background-color: #FFF; margin-bottom: 50px;
	-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;}
#sidebar .widgets h2 { color: #4d4e54; font-size: 14px; clear: both; margin-bottom: 13px;}
#sidebar .widgets ul li { font-size: 11.5px; }
#sidebar .widgets ul li a { color: #4d4e54; }
#sidebar .widgets .widget-bg { position: absolute; top: -13px; right: 15px; width: 35px; height: 40px; }

/* Follow us list */
#sidebar .follow-us-widget { overflow: hidden; padding-bottom: 35px; border-bottom: 1px solid #ccc; }
#sidebar .follow-us-widget .widget-wrapper { padding: 15px;}
#sidebar ul#follow-list li { float: left; position: relative; margin-right: 17px; zoom: 1; display: inline;}
#sidebar ul#follow-list li:last-child { margin-right: 0;}
#sidebar ul#follow-list li .follow-div { margin:0; padding:0; width: 33px; height: 38px; }
#sidebar ul#follow-list li a { display: block; width: 33px; height: 38px; text-indent: -9999px; background: url('http://blog.linkedin.com/wp-content/themes/linkedin/images/sprite3.png');}
#sidebar ul#follow-list li a#follow-lnkd { background-position: 0 0; }
#sidebar ul#follow-list li a#follow-twtr { background-position: -33px 0; }
#sidebar ul#follow-list li a#follow-fb { background-position: -66px 0; width: 32px; }
#sidebar ul#follow-list li a#follow-flickr { background-position: -130px 0; width: 32px;}
#sidebar ul#follow-list li a#follow-youtube { background-position: -98px 0; width: 32px;}
#sidebar ul#follow-list li a#follow-rss { background-position: -162px 0; width: 32px; }
#sidebar .widgets ul#follow-list li.last { margin-right: 0;}

/* Flickr */
#sidebar .flickr-widget { position: relative; border-bottom: 1px solid #ccc; }
#sidebar .flickr-widget .widget-wrapper { padding: 15px;}
#sidebar .flickr-widget h2 { margin-bottom: 20px; }
#sidebar .flickr-widget .widget-bg { background: url('images/sprite3.png') -267px 0 no-repeat;}
#sidebar #flickr-img-grp { margin-bottom: 10px; overflow:hidden; }
#sidebar #flickr-img-grp .flickr-img { float: left; margin: 0 15px 15px 0; }
	</style>
</head>
<body>
	<div id="sidebar">

		<div class="widgets">
			<div class="follow-us-widget">
				<div class="widget-wrapper">
					<h2>Follow Us Links</h2>

					<ul id="follow-list">
						<li><a id="follow-lnkd"
							href="https://www.linkedin.com/company/linkedin" target="_blank">LinkedIn</a>
						</li>
						<li><a id="follow-twtr" href="http://twitter.com/LinkedIn"
							target="_blank">Twitter</a></li>
						<li><a id="follow-fb"
							href="https://www.facebook.com/LinkedIn" target="_blank">Facebook</a>
						</li>
						<li><a id="follow-youtube"
							href="http://www.youtube.com/user/LinkedIn" target="_blank">YouTube</a>
						</li>
						<li><a id="follow-flickr"
							href="http://www.flickr.com/groups/linkedin/pool/"
							target="_blank">Flickr</a></li>
						<li class="last"><a id="follow-rss"
							href="http://feeds.feedburner.com/LinkedInBlog" target="_blank">RSS</a>
						</li>
					</ul>

				</div>
			</div>

		</div>
	</div>

</body>
</html>