Skip to content

Support for CSS background-image - #37

Closed
dancek wants to merge 2 commits into
imazen:unstablefrom
dancek:background-image-support
Closed

dancek wants to merge 2 commits into
imazen:unstablefrom
dancek:background-image-support

Conversation

@dancek

@dancek dancek commented Dec 23, 2014

Copy link
Copy Markdown
Contributor

I needed resizable background-images with dynamic content, so I implemented this. The benefit over a CSS-only solution is that changing an image in code is much easier when you only output one data attribute. I'm not sure if you consider this to be in scope of Slimmage.js, but it made sense to implement it as a feature since Slimmage already has a lot of the required infrastructure.

I have written the code at work, so my employer (HiQ Finland) must be mentioned in the copyright notice. Hopefully this is not an issue.

Elements with [data-slimmage-bg] are handled similarly to
img[data-slimmage], but the resulting image url is set as their CSS
background-image property.

This is meant to be used for background images that are 100% width, i.e.
`background-size: 100% auto` is recommended. If you don't use that,
you get slightly cropped images on normal displays (because resolution
is rounded up in 160px increments by default) and blocky half images on
high-dpi displays.

The implementation requires querySelectorAll (IE8+). Note that
background-size doesn't work on IE8 so check the usual workarounds for
that.
@lilith

lilith commented Jan 7, 2015

Copy link
Copy Markdown
Member

v0.3.0 offers getImageInfo, similar to how you refactored getImageSrcWithWidth.

It also offers beforeAdjustSrc as a callback function, enabling you to include this functionality as a plugin with no downside. slimmage.background.js would be welcome in the repository.

@dancek

dancek commented Jan 7, 2015

Copy link
Copy Markdown
Contributor Author

Great! I'll try to find time to refactor this and make another pull request.

@dancek dancek closed this Jan 7, 2015
@lilith

lilith commented Jan 7, 2015

Copy link
Copy Markdown
Member

Ok, thanks. Please include any copyright notices, and make sure you have employer permission to license it as MIT/Apache.

@lilith

lilith commented Jan 9, 2015

Copy link
Copy Markdown
Member

Also, on 0.4.0+, you will want to push any DOM elements you change into the array window.slimmage.changed, so that readyCallback() can be fired accurately.

@dancek

dancek commented Jan 12, 2015

Copy link
Copy Markdown
Contributor Author

I wonder if beforeAdjustSrc should accept an array of functions instead of a single function. Otherwise plugins would be overwriting each other's callbacks. I'm not sure there will ever be more than one plugin, though -- maybe this can be considered when it actually happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants