css之background-position屬性實現雪碧圖,backgroundposition

來源:互聯網
上載者:User

css之background-position屬性實現雪碧圖,backgroundposition

什麼是雪碧圖

雪碧圖就是CSS Sprite,也有人叫它CSS精靈,是一種CSS映像合并技術,就是把多張小表徵圖合并到一張圖片上,然後用css的background-position來顯示需要顯示的部分。

為什麼要用雪碧圖

可以減少載入網頁圖片時對伺服器的請求次數,提高頁面的載入速度,解決IE6滑鼠滑過時出現閃白的現象。

用雪碧圖有什麼弊端

個人認為如果你的雪碧圖不是很大,也不是很複雜基本沒什麼弊端。如果你的雪碧圖很大又複雜的話就有出現css代碼複查,網頁占記憶體大等各種問題。

執行個體

上面是一個按鈕第二個是它滑鼠經過時的樣子

這是兩個小表徵圖變合成的一個雪碧圖

<div class="user"><span></span>個人中心</div>
.user {
    width: 108px;
    height: 34px;
    border:1px #ddd solid;
    float: right;
    margin-top: 36px;
    line-height: 34px;
    border-radius: 3px;
    transition: all 0.3s;
    -moz-transition: all 0.3s;    
    -webkit-transition: all 0.3s;
    -ms-transition: all 0.3s;
}
.user span{
    display: inline-block;
    width: 20px;
    height: 22px;
    overflow:hidden;
    margin:5px 10px 5px 10px;
    float:left;
    background-image:url(../img/user.png);
    background-repeat: no-repeat;
    background-position: 0px -22px;    
    transition: all 0.3s;
    -moz-transition: all 0.3s;    
    -webkit-transition: all 0.3s;
    -ms-transition: all 0.3s;
}

.user:hover {
    border:1px #ff9600 solid;
    color:#f00;
}
.user:hover span{
    background-position: 0px 0px;
}

 

以上就是實現的代碼,請自行忽略這個過度效果(transition)。

可能剛開始有人還不會確定圖片位置。其實很簡單,你只要記住圖片是從左上方(0,0)開始的。

不知道這樣能否看的懂

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.