I am using srcset for responsive images on my site:
<img src="images/house-1915.jpg"
srcset="images/house-544.jpg 544w, images/house-768.jpg 768w, images/house-992.jpg 992w, images/house-1200.jpg 1200w, images/house-1915.jpg 1915w"
alt="House" class="hero-image">
I don't particularly like how it renders images. For example, when i'm on a screen with a 400px width, the 768w image is displayed. I wrote the following code in order to handle manual resizing:
$(window).on('resize', function() {
changeSrcset();
});
function changeSrcset() {
var windowWidth = $(window).width();
if (windowWidth <= 544) {
$('.hero-image').attr('srcset', '/static/images/house-544.jpg');
} else if (windowWidth <= 768) {
$('.hero-image').attr('srcset', '/static/images/house-768.jpg');
} else if (windowWidth <= 992) {
$('.hero-image').attr('srcset', '/static/images/house-992.jpg');
} else if (windowWidth <= 1200) {
$('.hero-image').attr('srcset', '/static/images/house-1200.jpg');
} else {
$('.hero-image').attr('srcset', '/static/images/house-1915.jpg');
}
$('.hero-image').css('width', windowWidth);
}
I am curious if removing srcset and using the above code to set the img src on page load is efficient?