anselmh / object-fit

Polyfill (mostly IE) for CSS object-fit property to fill-in/fit-in images into containers.
MIT License
996 stars 92 forks source link

Object-fit replaces classes on image #42

Closed Maximilianos closed 9 years ago

Maximilianos commented 9 years ago

https://github.com/anselmh/object-fit/blob/master/src/polyfill.object-fit.core.js#L134 and https://github.com/anselmh/object-fit/blob/master/src/polyfill.object-fit.core.js#L143 mean that any classes on the default image get replaced completely by the object-fit polyfill.

My issue is that I am using @aFarkas's https://github.com/aFarkas/lazysizes which works with the class lazyload to watch for images to lazy-load. When the object-fit polyfill is applied on the image as below, the image never loads because the class lazyload on the element is replaced with x-object-fit-taller.

Shall I create a pull request? the change would be to change it to: replacedElement.className += ' x-object-fit-taller'; for example.

The code for reference. Webpack+Babel used to package modules.

<img alt="" data-object-fit="contain" class="lazyload" data-srcset="..." data-sizes="..." src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==">
/** Responsive Images Polyfill */
import 'picturefill';

/** Lazyload Images */
import '../../node_modules/lazysizes/plugins/bgset/ls.bgset.js';
import 'lazysizes';

/** Object-fit/Object-position Polyfill */
import objectFit from 'object-fit';

window.objectFit = objectFit; // because objectFit referenced on global object within polyfill code

let addEvent = () => window.addEventListener || window.attachEvent; 
// if above value and not fn IE complains about 'Invalid calling object' for some reason...
let event = ( window.addEventListener ? '' : 'on' ) + 'DOMContentLoaded';

addEvent()(event, () => {
    objectFit.polyfill({
        selector: '[data-object-fit="cover"]',
        fittype: 'cover',
        disableCrossDomain: true
    });

    objectFit.polyfill({
        selector: '[data-object-fit="contain"]',
        fittype: 'contain',
        disableCrossDomain: true
    });
});
anselmh commented 9 years ago

A pull request would be great. When doing it, can you please check if the visual tests still work as expected?

Maximilianos commented 9 years ago

sure on it :+1:

Maximilianos commented 9 years ago

Ok pushed in: https://github.com/anselmh/object-fit/pull/43 Can you bump the patch version so that the npm picks up the change, plz?

anselmh commented 9 years ago

@Maximilianos Pushed 0.4.3 now. Cheers!

Maximilianos commented 9 years ago

:tada: :smile: thanks!