2014-01-09 91 views
0

我正在尋找簡單地移動divs寬度,因爲某些圖片在我的應用程序中加載。由於某種原因,我讀Div的寬度加128,然後使用.css()更改div的新寬度。出於某種原因,.css()函數在進行時不會更新寬度。它只會在最後一張加載的圖片上更新。我真的不確定爲什麼會發生這種情況。這裏是一些代碼:.css()不設置寬度值

document.getElementById('back').onload = function(){ 
    var width = $("#LoadingBar").css("width").replace(/[^-\d\.]/g, ''); 
    width = parseInt(width); 
    var newwidth = width + increment; 
    newwidth = newwidth +"px"; 
    $('#LoadingBar').animate({width:newwidth},"fast"); 
    if(width == 1280) { 
     //This is another div that I show once all the pictures load    
     $("#everything").show(); 
    } 
} 

任何想法,我什麼打破?

+0

在jQuery API中有一個'$()。width()'函數我認爲 – Kerstomaat

+2

你必須更好地解釋這個,你沒有使用'css()'來更新任何東西,當你忽略第二個參數時,它只會得到寬度? – adeneo

+0

oooo touche ...所以使用.animate()不會設置值,我從來沒有實際再次用新值更新css!謝謝你指出這一點 – user481610

回答

0

使用jquery width屬性更新元素寬度。

css是爲您的HTML元素更深奧的屬性。

$("#LoadingBar").css("width") 

變爲:

$("#LoadingBar").width() 

您可以設置寬度也

$("#LoadingBar").width(MyWidthValue); 
0

只是這樣做來改變寬度

$(<your element>).width(function (a, w) {return w+=128}); 

,這如果需要動畫

$(<your element>).animate({width:"+=128"},'fast'); 
0

而不是使用css width使用:var width = $("#LoadingBar").width()獲取數值。然後增加它並追加單元