2012-10-04 33 views
0

我是一組與css維度共享相同類名稱的變量。點擊一個特定的<div>
填充到屏幕的寬度和高度,並覆蓋其他<div>s。它工作正常。但問題是,當我再次點擊時,我已經失去了它的原始尺寸以恢復到原來的尺寸。下面是我的代碼:如何在jQuery中使用與其他語言中的靜態變量類似的變量

var isFullscreen = false; 
    $('.item').click(function(){ 
    var d = {}; 
    var speed = 900; 
    if(!isFullscreen){ // MAXIMIZATION 
     d.width = $(window).width();; 
     d.height = $(window).height();; 
     isFullscreen = true; 
     $('.item').hide(); 
     $(this).show(); 
     $(this).animate(d,speed)  
    } 
    else{ // MINIMIZATION    
     **d.width = $(this).css('width');** 
     **d.height = $(this).css('height');**    
     isFullscreen = false; 
     $(this).animate(d,speed); 
     $('.item').show(); 
    } 
    }) 

我試圖通過保存原來的尺寸,並使用他們在第二次點擊使用類似於靜態變量有什麼事。我看到JavaScript中沒有靜態變量,那麼如何實現這一點。

回答

0

我能夠救了我的高度和寬度之前我調整和以後使用它們來做到這一點:下面是我修改後的代碼:你可以做出來,我加入**那些線... 。

var isFullscreen = false; 
    var d = {}; 
    **var test = {};** 
    $('.item').click(function(){ 
    var speed = 900; 
    if(!isFullscreen){ // MAXIMIZATION 
     **test.width = $(this).css('width');** 
     **test.height = $(this).css('height');** 
     d.width = $(window).width(); 
     d.height = $(window).height(); 
     isFullscreen = true; 
     $('.item').hide(); 
     $(this).show(); 
     $(this).animate(d,speed)  
    } 
    else{ // MINIMIZATION    
     d.width = test.width; 
     d.height = test.height;    
     isFullscreen = false; 
     $(this).animate(d,speed); 
     $('.item').show(); 
    } 
}) 

謝謝你們的支持。

0

您當前每次單擊該項目時都會重新聲明d變量爲空對象。如果您將var d = {}聲明移到點擊功能之外,那麼您就可以做到這一點。

+0

我試過了,但它也一樣... – DomincJune

0

嘗試使用原型屬性。這確保它的添加實例具有與它相關的相同值。

function d(){ 
} 

d.prototype.width=$(window).width(); 
d.prototype.height=$(window).width(); 
+0

它沒有爲我工作... – DomincJune

+0

我想我試了錯誤的方式,你可以告訴我如何使用它.... – DomincJune

相關問題