1. 程式人生 > 其它 >前端筆記記錄---精靈圖的實現

前端筆記記錄---精靈圖的實現

技術標籤:htmlcsshtmlcss

宣告:以下內容為個人學習總結,初衷是方便自己學習複習記錄。如有錯誤之處,煩請多多指正!

為什麼使用精靈圖?

一個網頁中往往會應用很多小的背景影象作為修飾,當網頁中的影象過多時,伺服器就會頻繁地接受和傳送請求,這將大大降低頁面的載入速度。所以,為了有效地減少伺服器接受和傳送請求的次數,提高頁面的載入速度

原理

將網頁中的一些背景影象整合到一張大圖中(精靈圖),當網頁元素需要使用時,定位到精靈圖中不同位置的某個小圖即可。
如:我們使用這樣一張精靈圖作為背景圖:在這裡插入圖片描述

實現

效果:圖示橫排顯示在頁面,當點選圖示後,可進入相應的頁面
在這裡插入圖片描述
程式碼:

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .list { /*使列表顯示在螢幕右上方*/ position: absolute; right: 100px; top
: 5px; } .list li { /*使列表水平排列*/ list-style: none; width: 30px; height: 30px; float: left; margin-right: 20px; } .list li a { display: inline-block; width: 30px; height: 30px; } .list .qq { background: url(imgs/sprite.png) no-repeat -20px -15px; } .list .post { background
: url(imgs/sprite.png) no-repeat -20px -80px; } .list .wechat { background: url(imgs/sprite.png) no-repeat -20px -145px; } .list .sina { background: url(imgs/sprite.png) no-repeat -20px -208px; }
</style> </head> <body> <ul class="list"> <li class="qq"><a href="https://www.qq.com/"></a></li> <li class="post"><a href="https://tieba.baidu.com/"></a></li> <li class="wechat"><a href="https://web.wechat.com/"></a></li> <li class="sina"><a href="https://www.sina.com.cn/"></a></li> </ul> </body> </html>