2014-07-12 41 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