简体   繁体   English

JavaScript中基于媒体查询/视口或'Retina'-fix的其他语句

[英]Else statement in JavaScript based upon media query / viewport OR 'Retina'-fix

I'm creating a website for a photographer. 我正在为摄影师创建一个网站。 Sadly, to maintain quality, pictures serving should be equal at least the viewport of the user. 可悲的是,为了保持质量,图片服务应至少等于用户的视口。 Therefor, on Retina devices such as the new Macbook Pros, there should be images of about 1800x2880px. 因此,在Retina设备(例如新的Macbook Pro)上,应该有大约1800x2880px的图像。 Too bad, these load very slowly. 太糟糕了,这些加载非常缓慢。

I've currently got a dynamic script that simply loads thumbs and the full picture when clicked on. 我目前有一个动态脚本,单击该脚本即可轻松加载缩略图和完整图片。

The HTML code, for a thumb (and when clicked on, immediately the full size): 拇指的HTML代码(单击后立即为完整大小):

<a  href="#"
    data-flare-title="All-my-Loving"
    data-flare-width="375"
    class="multiple color"></a> 

And the Javascript that automatically links the thumb src, and the A href, and some more tags: 和自动链接thumb src,A href和一些其他标签的Javascript:

$(function(){

$('#container #portfolioOverview a').each(function(){

var $link = $(this),
    title = $link.data('flare-title') + '.jpg';
    imgTooltip = $link.data('flare-title');
    imgTip = imgTooltip.replace(/-/g, ' ');
    imgWidth = $link.data('flare-width') + 'px';

$link.attr('href', 'images/portfolio/full/' + title);
$link.attr('data-flare-bw', 'images/portfolio/blackwhite/' + title);
$link.attr('data-flare-thumb', 'images/portfolio/thumbs/' + title);
$link.attr('data-flare-scale', 'fitmax');
$link.attr('data-target', 'flare');
$link.attr('data-flare-gallery', 'portfolio');
$link.addClass('tip');

$link.attr('data-tip', imgTip);

$link.append($('<img>', {
  src     : 'images/portfolio/thumbs/' + title,
  height  : '250px',
  width   : imgWidth
}));

});

});

This line: $link.attr('href', 'images/portfolio/full/' + title); 这行: $link.attr('href', 'images/portfolio/full/' + title); links the correct full picture to the thumb. 将正确的全图链接到拇指。 However, all these full images have a resolution of about 2800x1800px. 但是,所有这些完整图像的分辨率约为2800x1800px。 These will also load for screens with a resolution of 1024x768, which results in awful, unnecessary slow loading times for smaller screens 这些也将为分辨率为1024x768的屏幕加载,从而导致较小屏幕的可怕,不必要的缓慢加载时间

Solution wanted : 想要的解决方案
As far as I could think of, this could simply be either a Retina JS script or a Else statement . 据我所料,这可能只是Retina JS脚本Else语句

As the Retina JS script will automatically pick the "*@2x.jpg" image in the /images/portfolio/full/ folder, and ignore them for non-retina devices, While the ELSE statement will check if viewport is larger than 1920x1080. 由于Retina JS脚本会自动在/ images / portfolio / full /文件夹中选择“ *@2x.jpg”图像,对于非视网膜设备会忽略它们,因此ELSE语句将检查视口是否大于1920x1080。 If confirm: Serve images from the folder 'images/portfolio/2x' instead of 'full', while 'else' would serve from the default full folder. 如果确认:从文件夹“ images / portfolio / 2x”而不是“ full”中提供图像,而“ else”将从默认的完整文件夹中提供。

Please comment if I'm unclear. 如果不清楚,请发表评论。 Thanks in advance! 提前致谢!

Note: This should apply to ALL devices running a screen resolution of that above 1920x1080, not only Apple's devices 注意:这应该适用于所有屏幕分辨率高于1920x1080的设备,不仅是Apple的设备

You can find the solution to your problem here. 您可以在这里找到解决问题的方法。 I will not copy and paste the solution. 我不会复制并粘贴解决方案。 The basic idea is to use a custom attribute for serve a high res image on high res display like retina. 基本思想是使用自定义属性在高分辨率显示器(如视网膜)上提供高分辨率图像。

http://drew.roon.io/retina-images-that-respond http://drew.roon.io/retina-images-that-respond

Why you don't use any approach of responsive image techniques? 为什么不使用任何响应式图像技术? More information you can find on http://responsiveimages.org/ If you use a polyfill like https://github.com/JimBobSquarePants/srcset-polyfill you are ready for the future. 更多信息,你可以找到http://responsiveimages.org/如果你使用像填充工具https://github.com/JimBobSquarePants/srcset-polyfill你是为未来做好准备。

Another approach is to minimized the image quality by increasing the dimensions of the images. 另一种方法是通过增加图像的尺寸来最小化图像质量。 Sounds strange, but it works: http://www.netvlies.nl/blog/design-interactie/retina-revolution I used this technique sometimes and in the result images are sharp even at retina displays. 听起来很奇怪,但是它起作用: http : //www.netvlies.nl/blog/design-interactie/retina-revolution有时我使用了这种技术,即使在视网膜显示器上,结果图像也清晰。

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

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