简体   繁体   English

如何在新窗口中从srcset打开正确的img?

[英]How to open a correct img from srcset in a new window?

I am trying to open an image in a new window and load the correct image based on srcset in order to open the right image size based on resolution 我试图在新窗口中打开图像并基于srcset加载正确的图像,以便根据分辨率打开正确的图像尺寸

html: 的HTML:

<div class="gallery-cell">
   <img alt="100%x200" data-srcset="https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg 240w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_n.jpg 320w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34.jpg 500w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_z.jpg 640w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_b.jpg 1920w" data-sizes="100%" sizes="100%" srcset="https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg 240w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_n.jpg 320w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34.jpg 500w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_z.jpg 640w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_b.jpg 1920w">
</div>

jQuery jQuery的

$(".gallery-cell").on("click", function(e){
   e.preventDefault();
   var img_to_load = $(this).find('img').attr('data-srcset');
   var $str = img_to_load.split(' ');
   var $retina = $str[0];
   imgWindow = window.open(img_to_load, 'imgWindow');
});

The above doesn't open different images based on resolutions but this https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg 上面没有根据分辨率打开不同的图像,但这是https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg

I believe is because of $str[0]; 我相信是因为$str[0]; yet I have tried not to insert an index [0] and that takes me to a not found image 但我尝试不插入索引[0],这将我带到未找到的图像

Example: http://codepen.io/aFarkas/pen/OVoavw 示例: http//codepen.io/aFarkas/pen/OVoavw

The solution is to use .currentSrc 解决方法是使用.currentSrc

    $(".gallery-cell").on("click", function(e){
       e.preventDefault();
       var src = $(this).find('img').get(0).currentSrc; // get DOM element's currentSrc
       imgWindow = window.open(src, 'imgWindow');
    });

When you perform these 2 steps: 当您执行以下两个步骤时:

var $str = img_to_load.split(' ');
var $retina = $str[0];

you will always end up with https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg based on the string in data-srcset . 您将根据data-srcset的字符串始终以https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg data-srcset This is because in the first step you create an array by splitting the string by spaces and in the second step you access the first element in the array. 这是因为在第一步中,您通过将字符串按空格分割来创建数组,而在第二步中,您访问了数组中的第一个元素。 So $str[0] is https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg , $str[1] is 240w, and so on. 因此$str[0]https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg $str[1]240w,依此类推。

To get the required part of this string you would have to do some complex string operations, so I suggest you use a different approach. 要获得此字符串的必需部分,您必须执行一些复杂的字符串操作,因此建议您使用其他方法。 Instead of having 而不是

data-srcset="https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg 240w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_n.jpg 320w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34.jpg 500w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_z.jpg 640w, https://farm4.staticflickr.com/3059/2835191823_e3636abb34_b.jpg 1920w"

why not split it up into separate parts like this: 为什么不将它分成这样的独立部分:

data-srcset-240w="https://farm4.staticflickr.com/3059/2835191823_e3636abb34_m.jpg"
data-srcset-320w="https://farm4.staticflickr.com/3059/2835191823_e3636abb34_n.jpg"

and so on. 等等。 Once you've done that you can just access the required part like this, for example if you need the 240w image: 完成后,您就可以像这样访问所需的部分,例如,如果需要240w图像:

var img_to_load = $(this).find('img').attr('data-srcset-240w');

Now of course you would also have to define some rules to see in which case you would load which image. 当然,现在您还必须定义一些规则,以查看在哪种情况下可以加载哪个图像。 For instance to determine the screen resolution you could use something like this https://stackoverflow.com/a/2242100/3353542 , and then check against those values to serve the correct images. 例如,要确定屏幕分辨率,您可以使用类似https://stackoverflow.com/a/2242100/3353542的内容 ,然后对照这些值进行检查以提供正确的图像。

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

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