我有以下簡單的代碼來獲取背景圖像的尺寸,但它抓取原始圖像的大小,而不是我在div中縮放的大小。我想在縮放後獲得像素尺寸,有沒有辦法做到這一點?使用jQuery進行縮放後檢索背景圖像的大小?
var actualImage = new Image();
actualImage.src = $("#chBox").css('background-image').replace(/"/g, "").replace(/url\(|\)$/ig, "");
actualImage.onload = function() {
width = this.width;
height = this.height;
}
編輯:
規模背景圖像的CSS:
#chBox {
height:100%;
width:100%;
background-repeat:no-repeat;
background-image: url(../content/frog/1.jpg);
background-position: center;
-webkit-background-size: contain; /*for webKit*/
-moz-background-size: contain; /*Mozilla*/
-o-background-size: contain; /*opera*/
background-size: contain; /*generic*/
}
我可能會在這裏丟失一些東西,但如果圖像縮放到元素的寬度,爲什麼不只是獲取元素的寬度? $(「#chBox」)。width() –
包含bg圖像的div大於圖像 – prismspecs