2016-06-17 60 views
2

如何獲取將被轉換爲像素數量的元素寬度或高度值?所有我通過使用.css('width')得到的表達式,甚至不是百分比數字,如calc(-25px + 50%)用像素獲取css值

編輯

我的代碼在這裏。 (Chrome)

var bars_width = element.css('width'); 
$('.histgram-content').css('width', bars_width); 
bars_width = parseInt(bars_width.replace('px', '')) * 0.85; 
$('.histgram-bar').css('width', (bars_width/data.length).toFixed(0).toString() + 'px'); 
/*** average Y ***/ 
var graph_height = $('.histgram-graph').css('height'); 
graph_height = parseInt(graph_height.replace('px', '')); 

var average_height = $('.average-line').css('top'); 
average_height = graph_height - parseInt(average_height.replace('px', '')); 

average_height返回我說的表達式。最後一行得到'NaN'的結果。

回答

2

你可以使用任何的jQuery的尺寸相關的方法:

Working example

+0

我不知道爲什麼這不是爲我工作,以及其他像'頂'的CSS值如何?我試過.css('top'),它直接返回表達式。 – donle

+0

您正在使用哪種瀏覽器?看到你的實際代碼會有很大的幫助。 –

+0

我更新了我的文章 – donle

0

可以高度

$('#element-id').height(); 

可以寬

$('#element-id').width(); 
+0

返回百分比值。我需要像素值 – donle

0

使用window.getComputedStyle(),例如: window.getComputedStyle($( '格')) '寬度']