2015-09-29 210 views
1

我想爲我會需要使用javascript或JQuery的背景圖像尺寸(寬,高)這樣的設計響應。有任何想法嗎 ?得到背景圖像尺寸

<div class="container"> 
    <div id="result" class="result">&nbsp</div> 
    <div class="slot-container"> 
      <div id="slot1" class="slot"></div> 
      <div id="slot2" class="slot"></div> 
      <div id="slot3" class="slot"></div> 
     <div><button id="control" class="btn">START</button></div> 
     <div id="try" class="result try">TRY 0</div> 
    </div> 

</div> 

https://jsfiddle.net/h0fbha33/

回答

0

您可以使用此:

var img = new Image; 
img.src = $('body').css('background-image').replace(/url\(|\)$/ig, ""); 
var bgImgWidth = img.width; 
var bgImgHeight = img.height; 
+0

你可以更新您的解決方案小提琴? –

+0

我想.slot背景圖像的不background-image屬性維度 –

+0

https://jsfiddle.net/h0fbha33/ –

1
$(window).on("load", function() { 
    var img = new Image(); 
    img.src = $('.slot').css('background-image').replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, ''); 
    alert(img.width + ' and ' + img.height); 
}); 

這裏是小提琴鏈接:https://jsfiddle.net/beroza/a01y1me4/

+0

感謝您的答覆,但它是從這個鏈接中鉻返回0 0 –

+0

嘗試:https://開頭的jsfiddle .NET/beroza/a01y1me4/ –

+0

它工作在Safari上沒有鍍鉻MAC –