css sprites圖片背景最佳化技術

來源:互聯網
上載者:User

轉自:藍色

撲克牌

介紹一種比較簡單的實現方法,這種方法不用一張圖片。
原理是:用四個特別字元的html標籤來實現。
1. & spades;表示黑桃
2. & hearts;表示紅心
3. & clubs;表示梅花
4. & diams;表示方塊
先寫結構:
<div class="card" style="left:10px;top:20px;">
  <div class="front">
    <b class="index">9<br />& spades;</b>
    <span class="A1">& spades;</span>
    <span class="A2">& spades;</span>
    <span class="A3">& spades;</span>
    <span class="A4">& spades;</span>
    <span class="B1">& spades;</span>
    <span class="C1">& spades;</span>
    <span class="C2">& spades;</span>
    <span class="C3">& spades;</span>
    <span class="C4">& spades;</span>
    <b class="end">& spades;<br/>9</b>
  </div>
</div>
再寫樣式:
<style type="text/css">
.card{width:125px;height:170px; position:absolute;overflow:hidden;border:1px #c0c0c0 solid;}
/*中間圖片通用設定*/
span{
display:block;
width:20px;
height:30px;
line-height:30px;
position:absolute;
font-size:26px;
left: 22px;
top: 130px;
}
/*小圖片通用設定*/
/*數字通用設定*/
b{display:block;width:15px;height:10px; position:absolute;font-size:10px;text-align:center;font-weight:bold;overflow:hidden;}
/*各座標點位置*/
.A1{left:21px;top:10px;}
.A2{left:21px;top:50px;}
.A3{left:21px;top:90px;}
.A4{left:21px;top:130px;}
.B1{left:51px;top:76px;}
.C1{left:83px;top:10px;}
.C2{left:83px;top:50px;}
.C3{left:83px;top:90px;}
.C4{left:83px;top:130px;}
.scroll{FILTERrogidXImageTransform.Microsoft.BasicImage(Rotation=2);height:1px}
.index {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  width:14px;
  height:36px;
  position: absolute;
  left:5px;
  top:0px;
}
.end {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  width:14px;
  height:36px;  
  position: absolute;
  right:5px;
  bottom:0px;
}
.red { color: #ff0000; }
.font{font-size:34px;}
</style>
注意:為了真實的還原一張撲克牌,我在第二張撲克牌中加入濾鏡功能,讓下面的圖案垂直翻轉,但這種方法會造成圖片不清晰,並且這種方法只有IE系列的瀏覽器才能識別,如果高手們有其它更好的辦法,請不吝賜教!

 

完整代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
.card{width:125px;height:170px; position:absolute;overflow:hidden;border:1px #c0c0c0 solid;}
/*中間圖片通用設定*/
span{
 display:block;
 width:20px;
 height:30px;
 line-height:30px;
 position:absolute;
 font-size:26px;
 left: 22px;
 top: 130px;
}
/*小圖片通用設定*/
/*數字通用設定*/
b{display:block;width:15px;height:10px; position:absolute;font-size:10px;text-align:center;font-weight:bold;overflow:hidden;}
/*各座標點位置*/
.A1{left:21px;top:10px;}
.A2{left:21px;top:50px;}
.A3{left:21px;top:90px;}
.A4{left:21px;top:130px;}
.B1{left:51px;top:76px;}
.C1{left:83px;top:10px;}
.C2{left:83px;top:50px;}
.C3{left:83px;top:90px;}
.C4{left:83px;top:130px;}
.scroll{FILTER:progid:DXImageTransform.Microsoft.BasicImage(Rotation=2);height:1px}
.index {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  width:14px;
  height:36px;
  position: absolute;
  left:5px;
  top:0px;
}
.end {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  width:14px;
  height:36px; 
  position: absolute;
  right:5px;
  bottom:0px;
}
.red { color: #ff0000; }
.font{font-size:34px;}
</style>
</head>
<body>
<!--9字元-->
<div class="card" style="left:10px;top:20px;">
  <div class="front">
    <b class="index">9<br /></b>
    <span class="A1"></span>
    <span class="A2"></span>
    <span class="A3"></span>
    <span class="A4"></span>
    <span class="B1"></span>
    <span class="C1"></span>
    <span class="C2"></span>
    <span class="C3"></span>
    <span class="C4"></span>
    <b class="end"><br/>9</b>
  </div>
</div>
<!--9字元-->
<div class="card" style="left:175px;top:20px;">
  <div class="front red">
    <b class="index">9<br /></b>
    <span class="A1"></span>
    <span class="A2"></span>
    <span class="A3"></span>
    <span class="A4"></span>
    <span class="B1"></span>
    <span class="C1"></span>
    <span class="C2"></span>
    <span class="C3"></span>
    <span class="C4"></span>
    <b class="end"><br/>9</b>
  </div>
</div>
<!--9字元-->
<div class="card" style="left:340px;top:20px;">
  <div class="front red">
    <b class="index">9<br /></b>
    <span class="A1"></span>
    <span class="A2"></span>
    <span class="A3 scroll"></span>
    <span class="A4 scroll"></span>
    <span class="B1"></span>
    <span class="C1"></span>
    <span class="C2"></span>
    <span class="C3 scroll"></span>
    <span class="C4 scroll"></span>
    <b class="end scroll">9<br/></b>
  </div>
</div>
<!--9字元-->
<div class="card" style="left:510px;top:20px;">
  <div class="front red">
    <b class="index">9<br /></b>
    <span class="A1 font"></span>
    <span class="A2 font"></span>
    <span class="A3 font"></span>
    <span class="A4 font"></span>
    <span class="B1 font"></span>
    <span class="C1 font"></span>
    <span class="C2 font"></span>
    <span class="C3 font"></span>
    <span class="C4 font"></span>
    <b class="end"><br/>9</b>
  </div>
</div>
</body>
</html>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.