Lazysizes: Blur-up: Copy style attribute from original <img> tag

Created on 14 Feb 2020  路  5Comments  路  Source: aFarkas/lazysizes

We render each img tag server-side with a style attribute, specifying object-position: xx% yy% which is specific to each individual image. Since the object position is specific to each single image, it cannot be specified in css, but we need to do it in style attributes.

However, this does not work together with blur-up. The blurred image is positioned differently since its img tag is rendered without the object-position style attribute.

Right now I am using a js hack for this to copy the styles, but I would like to avoid this.

The blur-up plugin should copy the style attribute of the original img tag. I am happy to prepare a pull-request.

Thank you for an awesome package!

Mikael

Most helpful comment

:-) Unfortunately, I am capable of stuff like this every day...

All 5 comments

Right now I am using a js hack for this to copy the styles, but I would like to avoid this.

Sorry for not answering. Can you show me your hack?

Below is my js code. I am using Initialize.js for it.

$(function () {

    /* work-around for Lazysizes.blurup :
     * copy style tag from original img tag to blur-up img tag
     * 
     * Uses Initialize library
     * */
    $.initialize(".ls-blur-up-img", function () {
        $(this).attr("style", $(this).prev("img").attr("style"));
    });
});

The change to ls.blur-up.js does not work as intended. Line 87:

blurImg.style = img.style;

needs to be replaced with with

blurImg.style.cssText = img.style.cssText;

then it works as expected.

what an embarrassment ;-). [1,2] + [3,4] = ???

:-) Unfortunately, I am capable of stuff like this every day...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

obzenner picture obzenner  路  5Comments

ymantijunk picture ymantijunk  路  5Comments

jpw123 picture jpw123  路  6Comments

nerijusgood picture nerijusgood  路  7Comments

Undistraction picture Undistraction  路  5Comments