【问题标题】:jquery expand from (and collapse into) a small squarejquery 从(并折叠成)一个小方块展开
【发布时间】:2012-06-16 05:05:48
【问题描述】:

我想创建一个 16 像素的小方形图标,单击该图标时,它会“展开”成一个 600 像素 x 100 像素的大横幅,显示覆盖部分页面的其他内容。然后用户可以单击横幅上的关闭按钮,它将“折叠”回图标中。

本质上,我正在寻找一种能够顺利制作动画的能力:

------------------------------------------------------------------
|                                                                |
|                                                       [icon]   |
|                                                                |
|                                                                |
|          Page content here                                     |
|          more page content here                                |
|          even more page content here                           |
|          yet more more page content here                       |
|          another line of page content                          |
|          and another.                                          |
|                                                                |
|                                                                |

到这里:

------------------------------------------------------------------
|                                                                |
|   ----------------------------------------------------------   |
|   |                                                    [x] |   |
|   |                                                        |   |
|   |      Content inside banner                             |   |
|   |      goes here                                         |   |
|   |                                                        |   |
|   ----------------------------------------------------------   |
|          another line of page content                          |
|          and another.                                          |
|                                                                |
|                                                                |

然后当用户单击关闭按钮时再次动画回来。

用 jquery 来做这件事的好方法是什么,具有良好的性能和浏览器兼容性?

我的应用程序的大多数用户都使用低级浏览器,因此任何解决方案都应该适用于 IE7+、iPad2+ 以及现代桌面和移动浏览器——尽管如果旧浏览器的性能很糟糕,我可能会放弃那里的动画。

【问题讨论】:

  • 你看过.animate()吗?
  • 我猜你写代码可能比制作漂亮的 ascii 艺术更快!
  • 您使用什么标记?
  • @Greg - 是的,我打算通过使用 animate() 改变 top/left/width/height 来开始编码,但是在遇到有趣的问题之前我已经使用过动画(视频性能,具有浏览器兼容性,尤其是旧 IE 等),所以我希望有一个现存的插件或代码示例已经考虑过这些问题,特别是因为这似乎是一个很常见的情况。
  • @David Thomas - 如果您要问的话,不要使用 HTML5。该解决方案需要至少可以追溯到 IE7。 IE6 很好,但不是必需的。

标签: jquery jquery-animate jquery-effects


【解决方案1】:

jsFiddle:http://jsfiddle.net/ZAJN4/48/

<div id="1" class="toggle" style="width:50px;height:50px;background:green;">
    <div id="icon1" style="background:blue;height:100%;width:100%;">Icon</div>
    <div id="content1" style="display:none;background:red;height:100%;width:100%;">Content</div>
</div>​

$(".toggle").click( function()
{
    var icon =  $("#icon" + $(this).attr("id"));
    var content = $("#content" + $(this).attr("id"));

    if ( icon.css("display") == "none" )
    {
        $(this).animate(
        {
            height: "50px",
            width: "50px"
        }, function() {});
    }
    else
    {
        $(this).animate(
        {
            height: "250px",
            width: "250px"
        }, function() {});
    }

    $(icon).animate(
    {
        height: 'toggle'
    }, function() {});

    $(content).animate(
    {
        height: 'toggle'
    }, function() {});
});​

【讨论】:

    【解决方案2】:

    对于这种特定情况,我会选择 absolutely 定位 div,它从 16 像素的正方形扩展到 600x100 像素的矩形。

    您可以使用 CSS3 过渡来制作动画:

    .foobar {
      width: 16px;
      height: 16px;
      position: absolute;
      top: 0;
      right: 0;
      transition: all 0.4s ease-out;
    }
    
    .foobar.expanded {
      width: 600px;
      height: 100px;
    }
    

    这样当你点击方块时你只需要添加expanded类,然后移除它以使div折叠,就像这样(使用jQuery来管理事件):

    $('.foobar').on('click', function (event) {
      event.preventDefault();
      $(this).toggleClass('expanded');
    });
    

    或者用jQuery的.animate():

    $('.foobar').on('click', function (event) {
      event.preventDefault();
      var $this = $(this);
      if ($this.width() === 600) {
        $this.animate({
          width : 16,
          heigth : 16
        }, 400);
      } else {
        $this.animate({
          width : 600,
          height : 100
        }, 400);
      }
    });
    

    使用它作为基础 CSS:

    .foobar {
      width: 16px;
      height: 16px;
      position: absolute;
      top: 0;
      right: 0;
    }
    

    如您所见,实现非常简单。

    一般来说,CSS 过渡比使用 javascript 制作的动画要好得多;缺点是旧浏览器不支持它们。 就个人而言,我会使用 CSS 过渡。

    【讨论】:

    • 好答案,我会试试这个。不幸的是,这个应用程序的至少一半用户使用的是 IE7 和 IE8,所以我现在可能会使用 javascript 解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 2017-10-02
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2014-03-12
    • 1970-01-01
    相关资源
    最近更新 更多