网易的同事整理了一套在移动端使用雪碧图的近乎完美解决方案,特此整理,方便日后使用。
推导过程
注重原理的同学可以看看 柏林大牛的文章,里面有详细的推导过程。我就不再重复了。
使用方法
首先需要配合移动端适配的rem来:
/*移动端适配 width|height|font-size = 视觉稿量出来的值 / 100rem;@lbl*/@media screen and (max-width:359px) and (min-width:320px) { html,body{ font-size: 50px !important; }}@media screen and (max-width:374px) and (min-width:360px) { html,body{ font-size: 56.25px !important; }}@media screen and (max-width:413px) and (min-width:375px) { html,body{ font-size: 58.594px !important; }}@media screen and (max-width:639px) and (min-width:414px){ html,body{ font-size: 64.687px !important; }}@media screen and (min-width:640px) { html,body{ font-size: 100px !important; }}
然后加上css雪碧图的mixin:
//$spriteWidth 雪碧图的宽度px//$spriteHeight 雪碧图的高度px//$iconWidth 需要显示icon的宽度px//$iconHeight 需要显示icon的高度px//$iconX icon的原始x坐标,也就是background-posotion中的负值//$iconY icon的原始y坐标//使用@include bgPositionSameSpriteAndWidth(80, 10);@mixin bgPosition($spriteWidth, $spriteHeight, $iconWidth, $iconHeight, $iconX, $iconY){ background-position: (($iconX / ($spriteWidth - $iconWidth)) * 100% ($iconY / ($spriteHeight - $iconHeight)) * 100%); }
在此基础上可以在某些特定情况下简化:
//同一张sprite图并且每个icon的大小相同@mixin bgPositionSameSpriteAndWidth($iconX, $iconY){ $spriteWidth : 220; $spriteHeight : 220; $iconWidth : 61; $iconHeight : 61; @include bgPosition($spriteWidth, $spriteHeight, $iconWidth, $iconHeight, $iconX, $iconY);}
另外,需要注意给背景图设置 background-size属性,也是rem单位。
使用案例
最后来个栗子:
//同一张sprite图并且每个icon的大小相同@mixin bgPositionSameSpriteAndWidth($iconX, $iconY){ $spriteWidth : 220; $spriteHeight : 220; $iconWidth : 61; $iconHeight : 61; @include bgPosition($spriteWidth, $spriteHeight, $iconWidth, $iconHeight, $iconX, $iconY);}//使用i{ padding-top: 100%; width: 100%; display: block; background: url(http://nos.netease.com/edu-image/3A65D313376F13CE75CE01C2593BD1CE.png) 0 0 no-repeat; background-size: 2.2rem 2.2rem;}.i-sina{ @include bgPositionSameSpriteAndWidth(10, 10);}.i-qzone{ @include bgPositionSameSpriteAndWidth(80, 10);}.i-qq{ @include bgPositionSameSpriteAndWidth(150, 10);}.i-douban{ @include bgPositionSameSpriteAndWidth(10, 80);}.i-yixin{ @include bgPositionSameSpriteAndWidth(80, 80);}.i-renren{ @include bgPositionSameSpriteAndWidth(150, 80);}
完美解决问题,是不是很嗨。

HTML的未來將朝著更加語義化、功能化和模塊化的方向發展。 1)語義化將使標籤更明確地描述內容,提升SEO和無障礙訪問。 2)功能化將引入新元素和屬性,滿足用戶需求。 3)模塊化將支持組件化開發,提高代碼復用性。

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

alt屬性是HTML中標籤的重要部分,用於提供圖片的替代文本。 1.當圖片無法加載時,alt屬性中的文本會顯示,提升用戶體驗。 2.屏幕閱讀器使用alt屬性幫助視障用戶理解圖片內容。 3.搜索引擎索引alt屬性中的文本,提高網頁的SEO排名。

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

HTML用於構建結構清晰的網站。 1)使用標籤如、、定義網站結構。 2)示例展示了博客和電商網站的結構。 3)避免常見錯誤如標籤嵌套不正確。 4)優化性能通過減少HTTP請求和使用語義化標籤。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器