admin管理员组文章数量:1419168
I have a srcset on my page
<img src="bg__x-large.png" srcset="bg__small.png 900w, bg__med.png 1200w, bg__large.png 1920w">
This is used by a parallax script, which right now just reads the src attribute
$(this).css('background-image', 'url(' + $(this).attr('src') + ')' );
to set this as the background image.
How can I read the correct src from the srcset with jquery?
I have a srcset on my page
<img src="bg__x-large.png" srcset="bg__small.png 900w, bg__med.png 1200w, bg__large.png 1920w">
This is used by a parallax script, which right now just reads the src attribute
$(this).css('background-image', 'url(' + $(this).attr('src') + ')' );
to set this as the background image.
How can I read the correct src from the srcset with jquery?
Share Improve this question asked Jun 21, 2016 at 8:50 tommuellertommueller 2,4963 gold badges33 silver badges49 bronze badges 1-
@newnoise there are multiple images in
srcset
which one you want to get; ? – Mairaj Ahmad Commented Jun 21, 2016 at 8:59
1 Answer
Reset to default 6Use the .currentSrc
property (as opposed to .src
) of the image to determine the active "src":
jq:
$(this).prop('currentSrc')
vanilla:
this.currentSrc
see mdc's HTMLMediaElement.currentSrc docs for more info.
本文标签: jqueryHow to select the correct src from srcset with javascriptStack Overflow
版权声明:本文标题:jquery - How to select the correct src from srcset with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304469a2652562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论