I have a section with a fullscreen background image that is loaded via a JavaScript data-attribute. As this is a responsive site, the same background image (1920 x 1080px) is scaled up and down depending on the screen size, which is not very performance-friendly, e.g. on mobile phones with smaller screen sizes.
HTML:
<!-- BEGIN PAGE TITLE -->
<section id="page-title" class="page-title-lg" data-bg-img="nature/full-20.jpg">
<div class="container">
<div class="page-title-wrapper">
<div class="page-title-txt">
<h1>Large Page Title</h1>
</div>
</div>
</div>
</section>
<!-- END PAGE TITLE -->
JS:
// Image Background
$('[data-bg-img]').each(function() {
var dataImg = $(this).attr('data-bg-img');
$(this).css('background-image', 'url(assets/img/' + dataImg + ')');
});
Is there a way to dynamically detect the window size and load the appropriate image via different data-attributes (e.g. 2560 x 1440, 1920 x 1440, 1280 x 800, 640 x 960, etc.) ?