The DIVI Post Slider Module is a wonderful way to showcase our posts and articles, but unfortunately it can be very harmful to our page load time. Because it uses the original size of the featured images, the megabytes can quickly accumulate, especially if a client with little tech knowledge uploads his blog’s featured images directly as he gets them from his camera or high-resolution download. But even besides such an extreme situation of having a few 5 MB giants bloating our website, the difference of a 500kb image compared with 100kb is still summing up significantly in a post slider. The described method might not fit for the parallax effect that requires bigger source files by default, and the 1080px version might not deliver sufficient quality for using the post slider in a fullwidth row – but for a regular standard slider on a site with regular content width, anything bigger would simply be a waste.
Choosing smaller background images for the post slider module
After contacting the Elegant Themes support, Eduard was so friendly to figure out an excellent JQuery solution that can simply be used within a code module, placed on the same page above the post slider. In this way the other pages are not affected. Perfect integration. So with the following code we can make use of two of DIVI’s pre-rendered thumbnail sizes: 1080×675 and 510×382. They are cropped so the filename will always have the same addition and can therefore be added by the script to the image name. So instead of the original full size image, e.g. original-image.jpg, the post slider will now load the most likely much smaller original-image-1080×675.jpg for screen sizes above 600px (the set threshold) and original-image-510×382.jpg for screen sizes below 600px. An immense saving of bytes and therefore pageload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<script> (function($) { $(document).ready(function() { var postSlider = $('.et_pb_post_slider'), threshold = 600, browserWidth = $(window).width(); function changeImgURL(){ if ( postSlider.length ) { var slide = postSlider.find('.et_pb_slide'); slide.each(function(){ var el = $(this), backgoundProp = el.css("background-image"), backgroundImg = backgoundProp.slice(5).slice(0,-6); if ( browserWidth >= threshold ) { var bg = backgroundImg + '-1080x675.jpg'; el.css('background-image', 'url('+bg+')'); } if ( browserWidth < threshold ) { var bg = backgroundImg + '-510x382.jpg'; el.css('background-image', 'url('+bg+')'); } }); } } changeImgURL(); }); })(jQuery) </script> |
One size fits all
For me personally the 510px version was even sufficient for all sizes. Overlaid by a strong color as I used on yogaonlineacademy.com, they simply look like they have been blurred by purpose as a nice effect. No pixelation is bespeaking the low resolution, and with an average of only 50kb per image my aim to show up to ten posts is still staying in a decent frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<script> (function($) { $(document).ready(function() { var postSlider = $('.et_pb_post_slider'); function changeImgURL(){ if ( postSlider.length ) { var slide = postSlider.find('.et_pb_slide'); slide.each(function(){ var el = $(this), backgoundProp = el.css("background-image"), backgroundImg = backgoundProp.slice(5).slice(0,-6); var bg = backgroundImg + '-510x382.jpg'; el.css('background-image', 'url('+bg+')'); }); } } changeImgURL(); }); })(jQuery) </script> |
By the way, if you use more than one post slider on a page but only want the script to work on one of them, you could assign a custom class to the one you want to adapt, and then you just need to replace .et_pb_post_slider in the code with that class. In this way the script will only influence the specific module(s).
Note: On one of my websites, out of so far unexplainable reasons, the slice method needs to be set to backgroundImg = backgoundProp.slice(4).slice(0,-5);
It doesn’t make sense at all, but with (5) and (0, -6) it was suddenly cutting of the h of http and the last letter of the filename. Update: it happened again after a while to behave different and other values would be needed. I copied the code now into the Theme Options / Integration / Header area with the original values. Let’s see if the code module is causing the irregularity.
I am not coding in JQuery, so if you find something to reduce or optimize in the code, or where the above mentioned strange behaviour could come from, let me know in the comments. All the kudos and my thanks go to Eduard from the Elegant Themes support who figured out how to fulfill my needs for a slimmer post slider module.
This solution is exactly what I am looking for and what DIVI should have working out of the box.
I tried both versions of the above code. In the page itself in a code module and in the DIVI integration section, but unfortunately it does not remove the original image from the code, so the page file size actually gets larger. It does create and use the thumbnail image, but it does not stop the original image from loading.
Does this happen for you, or does it work as it should?
Dear Pete, unfortunately I must say you are right, it is still loading the originals first before the JS is replacing them. Need to find another solution. Do you have any idea or found other solutions meanwhile?