2014-07-12 43 views
0

我正在嘗試爲以象限格式佈置的某些html元素創建動畫。點擊每個div後,我希望它們擴展到容器高度和寬度的100%,然後再次單擊摺疊回初始大小。這與瀏覽器的動畫類似,最大化並摺疊回原始大小。Css/jQuery動畫用於將div擴展爲屏幕的100%

我現在擁有的工作正常,我只想讓動畫不會將其他元素從屏幕上移開,也不會在標題消失時跳轉。

我該怎麼做?隨意使用jQuery動畫或css3動畫。謝謝!

這裏是什麼,我現在有一個演示:JSFIDDLE 的Javascript

$(document).ready(function() { 

    $('.box1').on('click', function() { 

     $(this).animate({height: "100%", width: "100%"}, 500); 

    }); 

}); 

HTML

<div id="page-wrapper"> 

    <div class="header"></div> 
    <div class="box-container clearfix"> 
     <div class="box1"></div> 
     <div class="box2"></div> 
     <div class="box3"></div> 
     <div class="box4"></div> 
    </div> 

</div> 

CSS

#page-wrapper { 
    width: 400px; 
    height: 480px; 
    border: 1px solid lightblue; 
} 

.box-container { 
    width: 100%; 
    height: 100%; 
} 

.header { 
    background: black; 
    height: 40px; 
    width: 100%; 
} 

.box1{ 
    background: yellow; 
    height: 190px; 
    width: 170px; 
    margin: 10px; 
    float: left; 
} 

.box2 { 
    background: red; 
    height: 190px; 
    width: 180px; 
     margin: 10px; 
    float: right; 
} 

.box3 { 
    background: green; 
    height: 190px; 
    width: 170px; 
    margin: 10px; 
    float: left; 
} 

.box4 { 
    background: blue; 
    height: 190px; 
    width: 180px; 
    float: right; 
    margin: 10px; 
} 
+0

這很好!唯一的事情是綠框似乎跳出框架,並且我還希望如果標題消失或重疊.. – Chrisgozd

+0

給我一些時間來處理它。 :) –

+0

謝謝,我真的很感激它。 – Chrisgozd

回答

0

你的代碼是好的事情是這樣的,如果如預期,當你設置的東西100%是它100%的父母我會檢查的

<div class="box-container clearfix"> 

的deminsions它不工作。所以看到這個父DIV實際上是在佔用整個屏幕

+0

對不起,也許我不夠清楚。我在我的問題中添加了「編輯」部分,但我一直在尋找幫助使動畫更加流暢和專業化,例如在標題隱藏時沒有黃色框跳轉,並且黃色框移動到其他元素上時不會跳動將它們推出屏幕。 – Chrisgozd

+0

@Chrisgozd - 你需要將它定位絕對,這樣它不會影響屏幕上的其他元素。另一種解決方案可能會工作,但你的一行代碼IMO更好,只需要改變CSS來使div將被擴展定位絕對 –

+0

如果我將它改爲絕對位置,不會讓其他元素移動,因爲它們都在彼此旁邊漂浮? – Chrisgozd

0

您可以檢查時寬度&高度不同於100%這樣的:使用jQuery UI的

$(document).ready(function() { 

    $('.box1').on('click', function() { 
     if($(this).attr("style") != "height: 100%; width: 100%;") 
      $(this).animate({height: "100%", width: "100%"}, 500); 
     else 
      $(this).animate({height: "190px", width: "170px"}, 500); 
    }); 

}); 

fiddle

另一個sollution :

添加這個類在你的CSS

個CSS

.fullsize{ 
    height:100%; 
    width:100%; 
    position:absolute; 
} 

JS

$('.box1').on('click', function() { 
    $(this).toggleClass("fullsize", 1000) 
}); 

fiddle

+0

謝謝!你知道我怎樣才能使它更平滑,這樣當黃色框變小時,元素不會跳回原位。 – Chrisgozd

+0

嗯,我建議另一種溶劑。請檢查它。 –

2

我有這個迄今爲止,這是一個有點亂,沒有動畫還沒有,但裸與我和我會後即將進行的動畫編輯。

http://jsfiddle.net/48vh9/24/

jQuery的

$(document).ready(function() { 

    var full = 0; 
    var thisBox; 
    var d = {}; 
    var speed = 900; 
    $('.box').on('click', function() { 
     thisBox = $(this); 
     console.log($(thisBox)[0]); 
     if(full === 0){ 
      $('.box').each(function(){ 
       if($(this)[0] != $(thisBox)[0]){ 
        $(this).fadeOut(1);      
       }     
      }); 
      d.width = "100%"; 
      d.height = "100%"; 
      $(this).animate(d,speed,function(){ 
       $(this).css("top","0px"); 
       $(this).css("left","0px"); 
       $(this).css("margin-left","0px"); 
       $(this).css("margin-top","0px"); 
       $(this).css("position","absolute"); 
       }); 
      full = 1; 
     } 
     else{ 

      d.width = "170px"; 
      d.height = "190px"; 
      $(this).css("position",""); 
      $(this).css("margin-left","10px"); 
      $(this).css("margin-top","10px"); 
      $(this).animate(d,speed,function(){ 
             $('.box').each(function(){ 
       if($(this)[0] != $(thisBox)[0]){ 
        $(this).fadeIn("fast");      
       }     
      }); }); 


      full = 0; 
     } 

    }); 

}); 

我也改變了HTML,使每個盒子有.box類。

+0

更新小提琴:http://jsfiddle.net/bonatoc/LymTY/1/ - 但也許你需要右邊的框來正確地設置動畫?例如,由於「浮動:正確」,紅色在黃色下生長。 –

+0

是的,理想情況下其他盒子不會移動或被推開。我想要擴展div擴展其他元素或其他元素在展開之前隱藏。 – Chrisgozd

0

你需要的是窗口的視口,我對那個

// Return visible bounds of window 
function viewport() 
{ 
    var e = window, a = 'inner'; 
    if (!('innerWidth' in window)) { 
     a = 'client'; 
     e = document.documentElement || document.body; 
    } 
    return {w: e[ a + 'Width' ], h: e[ a + 'Height' ]}; 
} 

可以重命名功能中庸之道「W」和「H」到「寬度」和「高度」,你應該得到的只是什麼你要。

看吧http://ryanve.com/lab/dimensions/如果您想了解更多的信息,或者只是谷歌「視口的javascript」)

- 編輯: 只是「賣場」,以設置回原來的大小。

+0

不是我真正在尋找的東西,我可以將元素動畫化爲其容器的100%,我只是想讓動畫更平滑,以便元素不會跳躍。 – Chrisgozd

+0

因此,使用「過渡期」的CSS屬性,將其設置爲幾秒鐘,它將是完美:) 更多信息:http://www.w3schools.com/cssref/css3_pr_transition-duration.asp –

+0

不確定這個屬性如何幫助,你能編輯我的jsfiddle並顯示我嗎? – Chrisgozd