Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit 3390e66

Browse files
committed
- 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
1 parent 3f08999 commit 3390e66

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

js/lazy-load.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717

1818
function lazy_load_image( img ) {
1919
var $img = jQuery( img ),
20-
src = $img.attr( 'data-lazy-src' );
20+
src = $img.attr( 'data-lazy-src' ),
21+
srcset = $img.attr( 'data-lazy-srcset' ),
22+
sizes = $img.attr( 'data-lazy-sizes' );
2123

2224
if ( ! src || 'undefined' === typeof( src ) )
2325
return;
2426

27+
if ( 'undefined' !== typeof( srcset ) && srcset ) {
28+
$img.removeAttr( 'data-lazy-srcset' );
29+
$img.removeAttr( 'data-lazy-sizes' );
30+
img.sizes = sizes;
31+
img.srcset = srcset;
32+
}
33+
2534
$img.unbind( 'scrollin' ) // remove event binding
26-
.hide()
35+
// .hide() // Flickering looks bad
2736
.removeAttr( 'data-lazy-src' )
2837
.attr( 'data-lazy-loaded', 'true' );
2938

lazy-load.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static function setup_filters() {
3535
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
3636
add_filter( 'post_thumbnail_html', array( __CLASS__, 'add_image_placeholders' ), 11 );
3737
add_filter( 'get_avatar', array( __CLASS__, 'add_image_placeholders' ), 11 );
38+
?>
39+
<noscript><style type="text/css">.lazy { display:none; }</style></noscript>
40+
<?php
3841
}
3942

4043
static function add_scripts() {
@@ -62,7 +65,7 @@ static function add_image_placeholders( $content ) {
6265

6366
static function process_image( $matches ) {
6467
// In case you want to change the placeholder image
65-
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ) );
68+
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ), $matches );
6669

6770
$old_attributes_str = $matches[2];
6871
$old_attributes = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() );
@@ -73,13 +76,17 @@ static function process_image( $matches ) {
7376

7477
$image_src = $old_attributes['src']['value'];
7578

76-
// Remove src and lazy-src since we manually add them
77-
$new_attributes = $old_attributes;
78-
unset( $new_attributes['src'], $new_attributes['data-lazy-src'] );
79+
// Also retain the srcset, and sizes if present. The latter is to keep w3c validator happy (sizes requires srcset)
80+
$lazy_srcset_attribute = ( ! empty( $old_attributes['srcset'] ) ) ? 'data-lazy-srcset="' . $old_attributes['srcset']['value'] . '"' : '';
81+
$lazy_sizes_attribute = ( ! empty( $old_attributes['sizes'] ) ) ? 'data-lazy-sizes="' . $old_attributes['sizes']['value'] . '"' : '';
82+
// Remove src, lazy-src, srcset, lazy-srcset, sizes and data-lazy-sizes since we manually add them
83+
$new_attributes = $old_attributes;
84+
$css_class = $new_attributes['class'] ? $new_attributes['class']['value'].' lazy' : 'lazy';
85+
unset( $new_attributes['src'], $new_attributes['data-lazy-src'], $new_attributes['srcset'], $new_attributes['data-lazy-srcset'], $new_attributes['sizes'], $new_attributes['data-lazy-sizes'], $new_attributes['class'] );
7986

8087
$new_attributes_str = self::build_attributes_string( $new_attributes );
81-
82-
return sprintf( '<img src="%1$s" data-lazy-src="%2$s" %3$s><noscript>%4$s</noscript>', esc_url( $placeholder_image ), esc_url( $image_src ), $new_attributes_str, $matches[0] );
88+
$placeholder_url = strpos($placeholder_image, 'data:') == 0 ? $placeholder_image : esc_url( $placeholder_image );
89+
return sprintf( '<img src="%1$s" class="%2$s" data-lazy-src="%3$s" %4$s %5$s %6$s><noscript>%7$s</noscript>', $placeholder_url , $css_class, esc_url( $image_src ), $lazy_srcset_attribute, $lazy_sizes_attribute, $new_attributes_str, $matches[0] );
8390
}
8491

8592
private static function build_attributes_string( $attributes ) {

0 commit comments

Comments
 (0)