From 3390e668ee84060c7fcf1395a14204bbc241263a Mon Sep 17 00:00:00 2001 From: Andreas Steffan Date: Wed, 10 Aug 2016 13:46:59 +0200 Subject: [PATCH] - Lazy juggling of srcset/sizes attributes introduced - Pass matches to lazyload_images_placeholder_image filter allowing it to generate individually sized images - Allow data:image placeholder URLs - Removed .hide() flickering - Added css/noscript to head so only one image appears w/o JavaScript enabled --- js/lazy-load.js | 13 +++++++++++-- lazy-load.php | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/js/lazy-load.js b/js/lazy-load.js index b7457ca..2182a79 100644 --- a/js/lazy-load.js +++ b/js/lazy-load.js @@ -17,13 +17,22 @@ function lazy_load_image( img ) { var $img = jQuery( img ), - src = $img.attr( 'data-lazy-src' ); + src = $img.attr( 'data-lazy-src' ), + srcset = $img.attr( 'data-lazy-srcset' ), + sizes = $img.attr( 'data-lazy-sizes' ); if ( ! src || 'undefined' === typeof( src ) ) return; + if ( 'undefined' !== typeof( srcset ) && srcset ) { + $img.removeAttr( 'data-lazy-srcset' ); + $img.removeAttr( 'data-lazy-sizes' ); + img.sizes = sizes; + img.srcset = srcset; + } + $img.unbind( 'scrollin' ) // remove event binding - .hide() + // .hide() // Flickering looks bad .removeAttr( 'data-lazy-src' ) .attr( 'data-lazy-loaded', 'true' ); diff --git a/lazy-load.php b/lazy-load.php index b80398b..c63e3c1 100644 --- a/lazy-load.php +++ b/lazy-load.php @@ -35,6 +35,9 @@ static function setup_filters() { add_filter( 'the_content', array( __CLASS__, 'add_image_placeholders' ), 99 ); // run this later, so other content filters have run, including image_add_wh on WP.com add_filter( 'post_thumbnail_html', array( __CLASS__, 'add_image_placeholders' ), 11 ); add_filter( 'get_avatar', array( __CLASS__, 'add_image_placeholders' ), 11 ); + ?> + + ', esc_url( $placeholder_image ), esc_url( $image_src ), $new_attributes_str, $matches[0] ); + $placeholder_url = strpos($placeholder_image, 'data:') == 0 ? $placeholder_image : esc_url( $placeholder_image ); + return sprintf( '', $placeholder_url , $css_class, esc_url( $image_src ), $lazy_srcset_attribute, $lazy_sizes_attribute, $new_attributes_str, $matches[0] ); } private static function build_attributes_string( $attributes ) {