【问题标题】:Css/jQuery animation for expanding divs to 100% of screen用于将 div 扩展到 100% 屏幕的 Css/jQuery 动画
【发布时间】:2014-07-12 23:37:07
【问题描述】:

我正在尝试为一些以象限格式布局的 html 元素创建动画。单击每个 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;
}

【问题讨论】:

  • 这很好!唯一的问题是绿色框似乎跳出框架,并且我想如果标题消失或重叠..
  • 给我一些时间来处理它。 :)
  • 谢谢,非常感谢。
  • 好的,这是怎么回事?我希望它运作良好,但不同浏览器的动画可能会有所不同。我正在使用 chrome,它工作得很好。我遇到的唯一问题是 div 不会从它们的原始点增长,但这需要大量的编码来修复。不过,跳跃的 div 不应该那么明显。 jsfiddle.net/48vh9/22
  • 另一个更新....jsfiddle.net/48vh9/24 这是今晚的我,如果这很好,请接受我的回答?我敢肯定,从这里开始,您可以轻松修复代码。

标签: jquery html css jquery-animate css-animations


【解决方案1】:

到目前为止,我有这个,有点乱,还没有动画,但我的只是裸露的,我很快就会发布带有动画的编辑。

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 类。

【讨论】:

  • 已更新小提琴:jsfiddle.net/bonatoc/LymTY/1 — 但也许您需要右侧的框才能正确设置动画?例如,由于“float:right;”,红色的会在黄色的下方增长。
  • 是的,理想情况下,其他盒子不会移动或被推开。我希望扩展的 div 在其他元素上展开,或者在展开之前隐藏其他元素。
【解决方案2】:

你的代码没有问题,如果它没有按预期工作,我会检查

 <div class="box-container clearfix">

当您将某些内容设置为 100% 时,即它的父级的 100%。所以看到这个父 div 实际上占据了整个屏幕

【讨论】:

  • 抱歉,我可能不够清楚。我在我的问题中添加了一个编辑部分,但我正在寻求帮助以使动画更加流畅和专业,例如当标题隐藏时没有黄色框跳转并且让黄色框移动到其他元素上所以它不会将它们推离屏幕。
  • @Chrisgozd - 你需要让它绝对定位,这样它就不会影响屏幕上的其他元素。另一种解决方案可能有效,但是您的一行代码 IMO 更好,只需更改 css 以使要扩展的 div 定位绝对
  • 如果我将其更改为绝对位置,其他元素会不会因为它们都浮动在彼此旁边而四处移动?
  • 嗯嗯 - 我的意思是你要找出最适合你的场景的设计。你得到了你的具体问题的答案。现在您可以将它们全部定位绝对,或者单击将它们更改为绝对并展开,然后变小并返回浮动。由你决定 。 ut 解决您原来的不良动画问题的方法是绝对位置。我的建议是让它们始终保持绝对,我总是避免浮动。
  • 我使用 float 的唯一原因是我可以动态更改框的数量,并且它们希望彼此相邻。我可以有 4、3 或 2 个宽度为 50% 或 100% 的框,但我不知道如何使用绝对位置动态地做到这一点。
【解决方案3】:

你需要的是窗口的视口,我有一个专门的功能

// 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”重命名为“width”和“height”,您应该得到您想要的。

如果你想了解更多信息,请看这里http://ryanve.com/lab/dimensions/,或者只是谷歌“viewport javascript”;)

-- 编辑: 只需“存储”原始大小以便将其重新设置。

【讨论】:

  • 不是我真正想要的,我可以将元素设置为 100% 的容器,我只是想让动画更流畅,这样元素就不会跳来跳去。
  • 所以使用 "transition-duration" css 属性,将其设置为秒数,这将是完美的:) 更多信息:w3schools.com/cssref/css3_pr_transition-duration.asp
  • 不确定该属性有何帮助,您能编辑我的 jsfiddle 并给我看吗?
【解决方案4】:

您可以像这样检查宽度和高度何时与100% 不同:

$(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

使用 jquery-ui 的另一种解决方案:

在你的 CSS 中添加这个类

css

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

js

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

fiddle

【讨论】:

  • 谢谢!你知道我怎样才能让它更平滑,这样当黄色框变小时元素就不会跳回原位吗?
  • 嗯,我建议另一种解决方案。请检查一下。
猜你喜欢
  • 2011-10-26
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 2015-04-19
  • 2014-05-16
相关资源
最近更新 更多