使用預設 src 的<img>元素實作滾屏加載效果,但是,就有可能存在這樣一個體驗問題:如果我們的 JavaScript 加載比較慢,我們的頁面就很有可能出現一塊一塊白色的圖片區域,純白色的,沒有任何資訊,用戶完全不知道這里的內容是什么, 雖然 alt 屬性可以提供描述資訊,但由于視覺效果不好,被隱藏掉了,我們可以在圖片還沒加載時就把 alt 資訊呈現出來:
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>CSS世界--代碼實踐--圖片alt資訊呈現</title> 9 <style> 10 /* 滾屏加載效果CSS */ 11 /* img { 12 visibility: hidden; 13 } 14 15 img[src] { 16 visibility: visible; 17 } */ 18 19 img { 20 display: inline-block; 21 width: 180px; 22 height: 100px; 23 /* 隱藏Firefox alt文字 */ 24 color: transparent; 25 position: relative; 26 overflow: hidden; 27 } 28 29 img:not([src]) { 30 /* 隱藏Chrome alt文字以及銀色邊框 */ 31 visibility: hidden; 32 } 33 34 img::before { 35 /* 淡藍色占位背景 */ 36 content: ""; 37 position: absolute; 38 left: 0; 39 width: 100%; 40 height: 100%; 41 background-color: #f0f3f9; 42 visibility: visible; 43 } 44 45 img::after { 46 /* 黑色alt資訊條 */ 47 content: attr(alt); 48 position: absolute; 49 left: 0; 50 bottom: 0; 51 width: 100%; 52 line-height: 30px; 53 background-color: rgba(0, 0, 0, .5); 54 color: white; 55 font-size: 14px; 56 transform: translateY(100%); 57 /* 來點過渡影片效果 */ 58 transition: transform .2s; 59 visibility: visible; 60 } 61 62 63 img:hover::after { 64 transform: translateY(0); 65 } 66 67 </style> 68 </head> 69 70 <body> 71 <div style="width: 200px;height: 200px;background: blanchedalmond;overflow: auto;"> 72 <!-- 滾屏加載效果HTML: --> 73 <!-- <img> --> 74 <img alt="圖1" src="https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3791918726,2864900975&fm=26&gp=0.jpg"> 75 <img alt="美女沉思圖" data-src="1.jpg"> 76 <img alt="圖3" src="https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2853553659,1775735885&fm=26&gp=0.jpg"> 77 <img alt="沉思圖" data-src="1.jpg"> 78 </div> 79 </body> 80 <script> 81 82 </script> 83 84 </html>
運行效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/251549.html
標籤:Html/Css
上一篇:深入淺出瀏覽器渲染原理
下一篇:網頁跳轉微信小程式的方案
