简体   繁体   English

视网膜图像的延迟加载?

[英]Lazy loading with retina Images?

I have this basic lazy loading script with retina image @2x and @3x. 我有这个基本的延迟加载脚本,其中包含视网膜图像@ 2x和@ 3x。

 (function(doc) { var div = doc.querySelector('div'); var img = doc.querySelector('img[data-src]'); img.onload = function() { img.removeAttribute('data-src'); div.classList.add('done'); }; img.setAttribute('src', img.getAttribute('data-src')); })(document); 
 img { width: 300px; margin: 0 auto; display: block; } img { opacity: 1; transition: opacity 0.3s; } img[data-src] { opacity: 0; } div { position: relative; min-height: 222px; } div:after { content: ''; position: absolute; width: 50px; height: 50px; border-radius: 50%; border-top: 1px solid #9a9a9a; border-left: 1px solid #9a9a9a; top: 50%; left: 50%; margin-top: -25px; margin-left: -25px; animation: id 1s linear infinite both; } div.done:after { display: none; } @keyframes id { to { transform: rotateZ(360deg) } } 
 <div> <img src="https://s3-eu-west-1.amazonaws.com/cornerjob-cdn/background.png" data-src="https://s3-eu-west-1.amazonaws.com/cornerjob-cdn/background.png" srcset="https://s3-eu-west-1.amazonaws.com/cornerjob-cdn/background@2x.png 2x, https://s3-eu-west-1.amazonaws.com/cornerjob-cdn/background@3x.png 3x"> </div> 

The browser determines which image to load depending on the device pixel ratio. 浏览器根据设备像素比率确定要加载的图像。
According to this: 根据这个:

  • What's the best practice to recognize which image is the browser loading? 识别浏览器正在加载哪个图像的最佳实践是什么?
  • Do I have to set a custom attribute for each retina image I have? 我必须为我拥有的每个视网膜图像设置自定义属性吗?
  • In general what will be the best approach to solve using lazy loading images with retina images? 总的来说,什么是解决将延迟加载图像与视网膜图像结合使用的最佳方法?

1) To see which image the browser is loading, reference here: Is it possible to see which srcset image a browser is using with browser developer tools 1)要查看浏览器正在加载哪个图像,请参考此处: 是否可以查看浏览器正在使用浏览器开发人员工具使用哪个srcset图像

2) You will need the custom attribute for any image which comes from the server or static HTML, when lazy loading you could set the property in the function after detecting if the user is HiDPI, see here: https://coderwall.com/p/q2z2uw/detect-hidpi-retina-displays-in-javascript 2)您将需要来自服务器或静态HTML的任何图像的custom属性,当延迟加载时,您可以在检测到用户是否为HiDPI后在函数中设置该属性,请参见: https ://coderwall.com/ p / q2z2uw / detect-hidpi-retina-displays-in-javascript

3) Personally I would use a library like bLazy: https://github.com/dinbror/blazy/ 3)我个人会使用像bLazy这样的库: https : //github.com/dinbror/blazy/

This might help you checkout a very nice lazy loading plugin Compatible with All Browsers and IE7+. 这可以帮助您签出与所有浏览器和IE7 +兼容的非常不错的延迟加载插件。

http://luis-almeida.github.io/unveil/ http://luis-almeida.github.io/unveil/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM