2012-01-17 109 views
0

我一直在試圖找到一個更優雅的方式來做這個功能,但還沒有搞清楚。我有2個div,我想調整一個函數的執行時間。一個會滑開(寬度:'0%'),另一個填充剩餘空間(寬度:'100%')。下面的代碼工作很好,但似乎有點沉重。任何建議使其更清潔?使用Jquery Animate調整2x div寬度 - 這是一個糟糕的方法嗎?

function testAct(){    
    $('#sideBar').animate({width:'0%'},500);   
    $('#map_canvas').animate({width:'100%'},500,function(){google.maps.event.trigger(map,'resize')});      
    if (document.getElementById('map_canvas').style.width == '100%') 
    { 
     $('#sideBar').animate({width:'25%'},500);  
     $('#map_canvas').animate({width:'75%'},500,function(){google.maps.event.trigger(map,'resize')});       
    } 
} 
+3

你應該考慮回到你以前的問題,選擇最佳答案,因爲接受的答案。 – 2012-01-17 22:16:36

+0

四個'.animate()'調用中的兩個不指定持續時間。 – Jasper 2012-01-17 22:19:20

+0

謝謝..我現在爲我的問題選擇最佳答案。我錯過了那個細節。我是新來stackoverflow – snowgage 2012-01-17 22:34:14

回答

1

我會做這樣的事情,所以你不要重複自己:

function testAct(){ 
    var newSideBarWidth,newMapCanvasWidth; 
    if(document.getElementById('map_canvas').style.width == '100%'){ 
     newSideBarWidth = "25%"; 
     newMapCanvasWidth = "75%"; 
    } 
    else{ 
     newSideBarWidth = "0%"; 
     newMapCanvasWidth = "100%";  
    } 
    $('#sideBar').animate({width:newSideBarWidth },500);  
    $('#map_canvas').animate({ 
     width: newMapCanvasWidth 
    }, 
    function(){ 
     google.maps.event.trigger(map,'resize') 
    }); 
} 
相關問題