-
-
Save wushan/4eb6ee3aa045cdbd296f to your computer and use it in GitHub Desktop.
getBgImage: Simple Jquery Plugin to get the dimensions or other attributes from a css background-image
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
// Inspired by http://stackoverflow.com/questions/5106243/how-do-i-get-background-image-size-in-jquery, a simple jquery plugin who does the task | |
$.fn.getBgImage = function(callback) { | |
var height = 0; | |
var path = $(this).css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', ''); | |
var tempImg = $('<img />'); | |
tempImg.hide(); //hide image | |
tempImg.bind('load', callback); | |
$('body').append(tempImg); // add to DOM before </body> | |
tempImg.attr('src', path); | |
$('#tempImg').remove(); //remove from DOM | |
}; | |
// usage | |
$("#background").getBgImage(function() { | |
console.log($(this).height()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment