遊戲陪玩系統原始碼中懶載入的實現方式有哪幾種?
遊戲陪玩系統原始碼採用懶載入方式的優勢
img的懶載入實現
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>event</title> <style> img { background: #F1F1FA; width: 400px; height: 300px; display: block; margin: 10px auto; border: 0; } </style></head><body> <img src="https://ik.imagekit.io/demo/img/image1.jpeg?tr=w-400,h-300" /> <img src="https://ik.imagekit.io/demo/img/image2.jpeg?tr=w-400,h-300" /> <img src="https://ik.imagekit.io/demo/img/image3.jpg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image2.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image3.jpg?tr=w-400,h-300" /> --> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image4.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image5.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image6.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image7.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image8.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image9.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image10.jpeg?tr=w-400,h-300" /> <script> document.addEventListener("DOMContentLoaded", function() { var lazyloadImages = document.querySelectorAll("img.lazy"); var lazyloadThrottleTimeout; function lazyload () { if(lazyloadThrottleTimeout) { clearTimeout(lazyloadThrottleTimeout); } lazyloadThrottleTimeout = setTimeout(function() { var scrollTop = window.pageYOffset; lazyloadImages.forEach(function(img) { if(img.offsetTop < (window.innerHeight + scrollTop)) { img.src = img.dataset.src; img.classList.remove('lazy'); } }); if(lazyloadImages.length == 0) { document.removeEventListener("scroll", lazyload); window.removeEventListener("resize", lazyload); window.removeEventListener("orientationChange", lazyload); } }, 20); } document.addEventListener("scroll", lazyload); window.addEventListener("resize", lazyload); window.addEventListener("orientationChange", lazyload); }); </script></body></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>observer</title> <style> img { background: #F1F1FA; width: 400px; height: 300px; display: block; margin: 10px auto; border: 0; } </style></head><body> <img src="https://ik.imagekit.io/demo/img/image2.jpeg?tr=w-400,h-300" /> <img src="https://ik.imagekit.io/demo/img/image3.jpg?tr=w-400,h-300" /> --> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image1.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image2.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image3.jpg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image4.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image5.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image6.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image7.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image8.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image9.jpeg?tr=w-400,h-300" /> <img class="lazy" data-src="https://ik.imagekit.io/demo/img/image10.jpeg?tr=w-400,h-300" /> <script> document.addEventListener("DOMContentLoaded", function() { var lazyloadImages = document.querySelectorAll(".lazy"); var imageObserver = new IntersectionObserver(function(entries, observer) { entries.forEach(function(entry) { if (entry.isIntersecting) { var image = entry.target; image.src = image.dataset.src; image.classList.remove("lazy"); imageObserver.unobserve(image); } }); }); lazyloadImages.forEach(function(image) { imageObserver.observe(image); }); }); </script></body></html>
background-image的實現
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background</title> <style> body { margin: 0; } .bg { height: 200px; } #bg-image.lazy { background-image: none; background-color: #F1F1FA; } #bg-image { background-image: url("https://ik.imagekit.io/demo/img/image1.jpeg?tr=w-400,h-300"); background-size: 100%; } </style></head><body> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <div id="bg-image" class="bg lazy"></div> <script> document.addEventListener("DOMContentLoaded", function() { var lazyloadImages = document.querySelectorAll(".lazy"); var imageObserver = new IntersectionObserver(function(entries, observer) { entries.forEach(function(entry) { if (entry.isIntersecting) { var image = entry.target; image.classList.remove("lazy"); imageObserver.unobserve(image); } }); }); lazyloadImages.forEach(function(image) { imageObserver.observe(image); }); }); </script></body></html>
漸進式懶載入
<a href="full.jpg" class="progressive replace"> <img src="tiny.jpg" class="preview" alt="image" /></a>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>progressive</title> <style> a.progressive { position: relative; display: block; overflow: hidden; outline: none; } a.progressive:not(.replace) { cursor: default; } a.progressive img { display: block; width: 100%; max-width: none; height: auto; border: 0 none; } a.progressive img.preview { filter: blur(2vw); transform: scale(1.05); } a.progressive img.reveal { position: absolute; left: 0; top: 0; will-change: transform, opacity; animation: reveal 1s ease-out; } @keyframes reveal { 0% {transform: scale(1.05); opacity: 0;} 100% {transform: scale(1); opacity: 1;} } </style></head><body> <a href=" <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//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QEAYABgAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gNzAK/9sAQwAKBwcIBwYKCAgICwoKCw4YEA4NDQ4dFRYRGCMfJSQiHyIhJis3LyYpNCkhIjBBMTQ5Oz4+PiUuRElDPEg3PT47/9sAQwEKCwsODQ4cEBAcOygiKDs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7/8AAEQgABQAUAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A5yC3e2S3gM7OEQHdgA57VTuLAPqUoaUk7yM7R2BPSiivS5VN8stjmk2ldGVM3lyshG7bxk0UUV4skk2jdN2P/9k=" class="preview" alt="palm trees" /> </a> <a href=" <img src=" </a> <a href=" <img src=" </a> <script> window.addEventListener('load', function() { var pItem = document.getElementsByClassName('progressive replace'), timer; window.addEventListener('scroll', scroller, false); window.addEventListener('resize', scroller, false); inView(); function scroller(e) { timer = timer || setTimeout(function() { timer = null; requestAnimationFrame(inView); }, 300); } function inView() { var scrollTop = window.pageYOffset; var innerHeight = window.innerHeight; var p = 0; while (p < pItem.length) { var offsetTop = pItem[p].offsetTop; if (offsetTop < (scrollTop + innerHeight)) { loadFullImage(pItem[p]); pItem[p].classList.remove('replace'); } else p++; } } function loadFullImage(item) { var img = new Image(); if (item.dataset) { img.srcset = item.dataset.srcset || ''; img.sizes = item.dataset.sizes || ''; } img.src = item.href; img.className = 'reveal'; if (img.complete) addImg(); else img.onload = addImg; function addImg() { item.addEventListener('click', function(e) { e.preventDefault(); }, false); item.appendChild(img).addEventListener('animationend', function(e) { var pImg = item.querySelector('img.preview'); if (pImg) { e.target.alt = pImg.alt || ''; item.removeChild(pImg); e.target.classList.remove('reveal'); } }); } } }, false); </script></body></html>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69996194/viewspace-2837935/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 遊戲陪玩系統原始碼中不同排序演算法的實現方式遊戲原始碼排序演算法
- 遊戲陪玩原始碼的登入方式,簡訊驗證碼登入的實現遊戲原始碼
- 如何在遊戲陪玩系統原始碼中實現“刮刮樂”效果?遊戲原始碼
- 如何實現遊戲陪玩系統原始碼前端效能監控?遊戲原始碼前端
- 在遊戲陪玩原始碼開發中,兩種清空陣列的方式遊戲原始碼陣列
- 遊戲陪玩原始碼前端圖片載入優化的各種技巧遊戲原始碼前端優化
- 遊戲陪玩系統實現自適應負載均衡演算法的方式遊戲負載演算法
- 如何在遊戲陪玩app原始碼中實現簡訊驗證碼登入?遊戲APP原始碼
- 帶你瞭解遊戲陪玩系統原始碼前端常用的儲存方式遊戲原始碼前端
- 如何在遊戲陪玩系統原始碼中聊天室內實現一個禮物系統?遊戲原始碼
- 實現圖片懶載入的三種方式
- 遊戲陪玩系統原始碼開發,如何實現圖片和動畫的優化?遊戲原始碼動畫優化
- 如何實現遊戲陪玩系統中語音的錄製與播放?遊戲
- 遊戲陪玩系統原始碼的許可權設計,如何基於位運算實現?遊戲原始碼
- 遊戲陪玩系統開發,音視訊混流的實現程式碼遊戲
- 小說APP原始碼的圖片載入方式,懶載入和預載入的實現APP原始碼
- 透過websocket,實現遊戲陪玩系統的聊天室Web遊戲
- IPTV系統有哪幾種運營方式?
- 陪玩系統原始碼實現音訊編碼的相關步驟原始碼音訊
- 陪玩系統原始碼中陣列去重的實現程式碼,簡單卻重要原始碼陣列
- 陪玩系統原始碼中mysql資料庫備份還原的實現程式碼原始碼MySql資料庫
- 關於遊戲陪玩系統原始碼後臺管理系統,需要思考的二三事遊戲原始碼
- 遊戲陪玩原始碼的移動端適配,應該如何實現?遊戲原始碼
- 直播平臺搭建,vue中實現圖片懶載入的幾種方法Vue
- 陪玩系統原始碼開發,H5頁面中呼叫支付功能的實現原始碼H5
- 遊戲陪玩原始碼開發中,頁面跳轉方式及生命週期分析遊戲原始碼
- 遊戲陪玩系統開發,Java怎樣實現流合併?遊戲Java
- 如何開發陪玩系統原始碼的列表頁面,相關實現程式碼原始碼
- 遊戲陪玩平臺原始碼開發,依賴收集和觸發的實現遊戲原始碼
- 遊戲陪玩app原始碼開發,常用的倒數計時功能如何實現?遊戲APP原始碼
- 遊戲陪玩系統開發,日期時間選擇介面的實現遊戲
- 如何進行遊戲陪玩系統原始碼中音視訊的自動化測試?遊戲原始碼
- AOP 有幾種實現方式?
- 來玩遊戲嗎?能線上陪玩的那種。遊戲
- 遊戲陪玩系統,生成證書和金鑰庫的程式碼分析遊戲
- 遊戲陪玩系統開發,業務程式碼判斷生產/開發環境的方式遊戲開發環境
- 實現登入態的幾種方式
- 直播系統原始碼,MAP的幾種取資料的方式原始碼