Skip to content

Instantly share code, notes, and snippets.

@epoberezkin
Last active December 14, 2015 10:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epoberezkin/5070642 to your computer and use it in GitHub Desktop.
Save epoberezkin/5070642 to your computer and use it in GitHub Desktop.
Resizing elements backgrounds with jQuery is simple!
function resizeBackground(elementsOrSelector, sizeMultiplier) {
$(elementsOrSelector).each(function(){
var el = $(this);
if (el.css('background-image') != 'none') {
resizeBackgroundProperty(el, 'background-size', sizeMultiplier);
resizeBackgroundProperty(el, 'background-position', sizeMultiplier);
}
});
function resizeBackgroundProperty(element, property, multiplier) {
var bgSizes = element.css(property);
bgSizes = bgSizes.split(/\s*px\s*/);
bgSizes = bgSizes[0]*multiplier + 'px ' + bgSizes[1]*multiplier + 'px';
element.css(property, bgSizes);
}
}
Why use plugin in a separate file for something that takes less than 20 lines of code?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment