luis-almeida / unveil

A very lightweight jQuery plugin to lazy load images
4.16k stars 676 forks source link

Unveil to work on Print event #110

Open ghost opened 8 years ago

ghost commented 8 years ago

I'd like to be able to print webpages with the lazyloaded images without having to scroll all the way down to load them first – my understanding is that this can be done by triggering unveil with "window.onbeforeprint," though it is not well supported...

here's the code I have:

var mediaQueryList = window.matchMedia('print');
    mediaQueryList.addListener(function(mql) {
    if (mql.matches) {
        $("img").trigger("unveil");
    } else {
        window.onbeforeprint = function () { 
            $("img").trigger("unveil");
        }
    }
  });

Still not seeing all the images on the print screen, though. Any ideas on how to get unveil to work for printing?

fsasvari commented 6 years ago

The following code is working and tested on Google Chrome, Microsoft Edge and Internet Explorer 11

jQuery(document).ready(function($) {
    var beforePrint = function() {
        $('img').trigger('unveil');
    };

    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
            if (mql.matches) {
                beforePrint();
            }
        });
    }

    window.onbeforeprint = beforePrint;
});