【问题标题】:Animate the width of a block element as it changes with jQuery?用jQuery改变块元素的宽度?
【发布时间】:2015-01-07 07:00:20
【问题描述】:

我有一个块元素,它会根据其所处的状态改变其宽度。

Here's a fiddle 告诉你我的意思。

单击黑色按钮时,您会看到该按钮内的文本发生变化,从而减小了整体宽度。我想知道是否有一种方法可以通过 CSS 或 JS 让我在单击按钮时获得平滑动画的宽度。

HTML

<a id="contact-button" href="#">Get in touch</a>

<div id="primary"></div>
<div id="contact-keebs"></div>

CSS

#contact-button {
    background: #000;
    background-size: 24px;
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
    font-weight: bold;
    padding: 10px 20px;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 1;
}
#primary {
    background: red;
    height: 200px;
    position: absolute;
    width: 200px;
}
#contact-keebs {
    background: blue;
    display: none;
    height: 200px;
    position: absolute;
    width: 200px;
}

JS

var c = 0;
$('#contact-button').click(function (e) {

    e.preventDefault();

    if (c++ % 2 == 0) {

        $(this).addClass('work-button').text('Work');
        $('body').addClass('contact-content');
        $('#contact-keebs').finish().stop().fadeIn(500);

    } else {

        $(this).removeClass('work-button').text('Get in touch');
        $('body').removeClass('contact-content');
        $('#contact-keebs').finish().stop().fadeOut(500);

    }
});

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    您可以使用transitionwidth 设置动画。

    var c = 0;
    $('#contact-button').click(function(e) {
      e.preventDefault();
      if (c++ % 2 == 0) {
        $(this).addClass('work-button').text('Work');
        $('body').addClass('contact-content');
        $('#contact-button').css('width', '30px');
        $('#contact-keebs').finish().stop().fadeIn(500);
      } else {
        $(this).removeClass('work-button').text('Get in touch');
        $('#contact-button').css('width', '85px');
        $('body').removeClass('contact-content');
        $('#contact-keebs').finish().stop().fadeOut(500);
      }
    });
    #contact-button {
      background: #000;
      background-size: 24px;
      color: #fff;
      width: 85px;
      text-decoration: none;
      text-transform: uppercase;
      font-weight: bold;
      padding: 10px 20px;
      position: absolute;
      right: 0;
      top: 0;
      z-index: 1;
      transition: all 0.4s cubic-bezier(.09,1.31,1,1.28);
      white-space: pre;
      overflow: hidden;
    }
    #primary {
      background: red;
      height: 200px;
      position: absolute;
      width: 200px;
    }
    #contact-keebs {
      background: blue;
      display: none;
      height: 200px;
      position: absolute;
      width: 200px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <a id="contact-button" href="#">Get in touch</a>
    <div id="primary"></div>
    <div id="contact-keebs"></div>

    【讨论】:

    • 我认为我的帖子并不清楚。我正在尝试让黑色按钮的宽度进行动画处理。该按钮会根据其中的文本(“工作”、“联系”)更改宽度,我希望宽度能够平滑地设置动画。其他框(红色、蓝色)都可以。
    • @J82 - 更新了答案。 :)
    • @J82 - 不客气。 :)
    【解决方案2】:

    在#contact-button 上使用此 CSS

    过渡:宽度2s线性;

    【讨论】:

      【解决方案3】:

      也许不是最好的解决方案,但您可以使用 CSS 过渡并指定宽度来做到这一点(这是不好的部分)。像这样:http://jsfiddle.net/ddac5391/12/

      该代码非常简单,在 CSS 中您只需将其添加到 #contact-button 样式:

      overflow:hidden;
      width:150px;
      height:18px;
      transition: width 0.5s linear;
      -webkit-transition:width 0.5s linear;
      

      (其中一些像 heightoverflow 并不是真正需要的)

      然后在JS中,在更改文本内容之前/之后指定框的width

      $(this).addClass('work-button').text('Work').css({width:"70px"})
      ...
      $(this).removeClass('work-button').text('Get in touch').css({width:"140px"});
      

      然后单击按钮并查看它的增长/收缩。

      【讨论】:

      • 您可以添加一些额外的样式 (white-space: nowrap;),这样内容就不会分成两行:jsfiddle.net/ddac5391/14
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多