Auto Swap Background Images Based On Screen Size - swapBackground

Auto Swap Background Images Based On Screen Size - swapBackground
File Size: 3.58 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

swapBackground is a jQuery plugin for responsive, cross-platform background images that automatically swaps the background image sources on different screen resolutions.

How to use it:

1. Add the latest version of jQuery library and the jQuery swapBackground plugin's JavaScript to the webpage.

<script src="//code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="swapBackground.js"></script>

2. Initialize the plugin on the target container and specify the image sources on different screen resolutions.

<div class="container"></div>
$('.container').swapBackground({
  srcSm: 'sm.jpg',
  srcLg: 'lg.jpg'
});

3. In this case, the plugin will load the 'lg.jpg' when the screen size is larger than 1024px and change the background image to 'sm.jpg' when the screen size is smaller than 1024px. You can specify the default breakpoint in the JavaScript as this:

$('.container').swapBackground({
  srcSm: 'sm.jpg',
  srcLg: 'lg.jpg',
  mediaQuery:1023
});

4. Alternatively you can apply the same options with 'data' attributes on the container element.

<div class="container" 
     data-lg="lg.jpg" 
     data-sm="sm.jpg">
</div>
$('.container').swapBackground({
  mediaQuery:1023
});

This awesome jQuery plugin is developed by chaddugas. For more Advanced Usages, please check the demo page or visit the official website.