【问题标题】:Div height animation causes divs to change alignment in jQuerydiv 高度动画导致 div 在 jQuery 中改变对齐方式
【发布时间】:2014-10-23 02:04:23
【问题描述】:

我正在尝试通过单击按钮为 5 个 div 的高度设置动画。尽管这些 div 的大小确实会增加,但动画会导致 div 在顶部而不是从底部对齐。如果那有意义的话。 实际上我更希望它在从顶部向下移动时进行动画处理(因此从顶部对齐),但即使反过来,这个动画也会自行完成,然后更改 div 的位置。

$("button").click(function(){
  $("#f").css("display", "inline");
  $(".css").css("display", "inline");
  $(".html").css("display", "inline");
  $(".jQuery").css("display", "inline");
  $(".premiere").css("display", "inline");
  $(".photoshop").css("display", "inline");
  $(".css").animate({height:'300'}, 600);
  $(".html").animate({height:'300'}, 600);
  $(".jQuery").animate({height:'125'}, 600);
  $(".premiere").animate({height:'250'}, 600);
  $(".photoshop").animate({height:'325'}, 600);
});
p{
  transform: rotate(270deg);
  margin-top: 60px;
}

#f, .css, .html, .jQuery, .premiere, .photoshop{
  height: 0px;
  width: 30px;
  display: none;
  background-color: blue;
}

.css{ background-color: blue }

.html{ background-color: red }

.jQuery{ background-color: orange }

.premiere{ background-color: purple }

.photoshop{ background-color: yellow }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<button>Go</button>
<div></div>
<div class="f">
  <div class="html"><p>HTML</p></div>
  <div class="css"><p>CSS</p></div>
  <div class="jQuery"><p>jQuery</p></div>
  <div class="premiere"><p>Premiere</p></div>
  <div class="photoshop"><p>Photoshop</p></div>
</div>

【问题讨论】:

    标签: javascript jquery html jquery-animate height


    【解决方案1】:

    vertical-align: bottom 添加到.jQuery.html.premiere.photoshop.css

    更新Fiddle

    【讨论】:

    • 哎呀,不敢相信我错过了。谢谢你好先生:)
    【解决方案2】:

    这里,对于自上而下的动画版本,你会喜欢它(如果我理解你正确的话):

    我还为所有栏添加了一个通用类,并稍微更新了您的 JS 和 CSS,以减少您的代码冗余

    小提琴:http://jsfiddle.net/znxue2wg/


    JS、CSS、HTML:

    $("button").click(function(){
      $(".bar").css("display", "inline");
      $(".bar.html").animate({height:'300'}, 600);
      $(".bar.css").animate({height:'300'}, 600);
      $(".bar.jQuery").animate({height:'125'}, 600);
      $(".bar.premiere").animate({height:'250'}, 600);
      $(".bar.photoshop").animate({height:'325'}, 600);
    });
    .bar {
      display: none;
      height: 0px;
      width: 30px;
      vertical-align: top;
    }
    .bar.html {background-color: red;}
    .bar.css {background-color: blue;}
    .bar.jQuery {background-color: orange;}
    .bar.premiere {background-color: purple;}
    .bar.photoshop {background-color: yellow;}
    
    .bar p {
      transform: rotate(270deg);
      margin-top: 60px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <button>Go</button>
    <div>
      <div class="bar html"><p>HTML</p></div>
      <div class="bar css"><p>CSS</p></div>
      <div class="bar jQuery"><p>jQuery</p></div>
      <div class="bar premiere"><p>Premiere</p></div>
      <div class="bar photoshop"><p>Photoshop</p></div>
    </div>

    【讨论】:

    • 嘿,谢谢,我已经解决了我的问题,但感谢你让代码少了一点冗余:)
    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多