Last active
December 14, 2015 10:18
-
-
Save epoberezkin/5070642 to your computer and use it in GitHub Desktop.
Resizing elements backgrounds with jQuery is simple!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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