【问题标题】:jQuery Animate to auto height, close back up to set heightjQuery Animate 到自动高度,关闭到设置高度
【发布时间】:2015-05-11 16:43:41
【问题描述】:

我有一个向下滑动切换 div,其中包含标题、内容和切换打开按钮。我已经设置了一个小提琴,但 div 目前完全关闭,阻止你再次打开。我希望 div 接近的高度是标题的高度。我该如何解决这个问题?

FIDDLE

$(function(){

var open = 1;

    $(".toggle").click(function () {
        if (open == 1) {
            $(".box").animate({"height":"toggle"}, 1000);
            open = 0;
            $( ".toggle" ).addClass( "open" );
        }
        else if (open == 0) {
            $(".box").animate({"height":"toggle"}, 1000);
            open = 1;
            $( ".toggle" ).removeClass( "open" );
        }
    });

 });

【问题讨论】:

    标签: jquery jquery-animate toggle slidedown


    【解决方案1】:

    将按钮移出框外,请参阅fiddle

    $(function() {
    
      var open = 1;
    
      $(".toggle").click(function() {
        if (open == 1) {
          $(".box").animate({
            "height": "toggle"
          }, 1000);
          open = 0;
          $(".toggle").addClass("open");
        } else if (open == 0) {
          $(".box").animate({
            "height": "toggle"
          }, 1000);
          open = 1;
          $(".toggle").removeClass("open");
        }
      });
    
    });
    .box {
      position: fixed;
      z-index: 3000;
      left: 0;
      text-align: left;
      background: #fff;
      max-width: 250px;
      width: 100%;
      top: 15%;
    }
    .box h2 {
      /* fallback */
      /*background-color: #e9e9e9;
      background: url(images/linear_bg_2.png);
      background-repeat: repeat-x;*/
      /* Safari 4-5, Chrome 1-9 */
      background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#e9e9e9), to(#f6f6f6));
      /* Safari 5.1, Chrome 10+ */
      background: -webkit-linear-gradient(top, #e9e9e9, #f6f6f6);
      /* Firefox 3.6+ */
      background: -moz-linear-gradient(top, #e9e9e9, #f6f6f6);
      /* IE 10 */
      background: -ms-linear-gradient(top, #e9e9e9, #f6f6f6);
      /* Opera 11.10+ */
      background: -o-linear-gradient(top, #e9e9e9, #f6f6f6);
      line-height: 3rem;
      padding: 2rem;
      margin: 0;
      font-size: 1.6em;
    }
    .box {
      position: relative;
      z-index: 3000;
      left: 0;
      text-align: left;
      background: #fff;
      max-width: 100%;
      width: 100%;
      top: 0;
    }
    .toggle {
      width: 40px;
      height: 26px;
      outline: none;
      border: none!important;
      box-shadow: none;
      border-radius: 3px;
    }
    .toggle.open {}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="box">
    
    
    
      <h2>title here</h2>
    
      <div>
    
        Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
        sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
        incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
        velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
    
      </div>
    
    </div>
    <button class="toggle">open</button>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-05
      • 2012-05-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      相关资源
      最近更新 更多