【问题标题】:Using jquery, Need to hide a div and animate the width of another div at the same time使用 jquery,需要隐藏一个 div 并同时为另一个 div 的宽度设置动画
【发布时间】:2013-03-27 21:12:51
【问题描述】:

我正在创建一个页面,其中嵌入了谷歌地图和右侧的图例。单击“隐藏图例”按钮时,我想隐藏图例 div 并将地图 div 的宽度从 720 像素增加以填充页面宽度(1000 像素)。

我还隐藏了“隐藏图例”按钮并显示“显示图例”按钮。

它基本上可以工作,但地图 div 闪烁并且动画不流畅。它以 720 像素开始;图例滑得很好,很流畅,然后 mp div 出现在 1000 像素处填充页面。

这快把我逼疯了……我已经搜索过,但找不到我的确切情况。提前感谢您的任何指点。

这是我的 jquery:

<script>
$(document).ready(function() {

$(".hideLegend").click(function () {
   $(this).hide(); 
   $("#legend").hide("slide", { direction: "left" }, 500);
   $("#map").animate({ width: "1000"}, 500); 
   $(".showLegend").show(); 


});

  });
  </script>

【问题讨论】:

  • 我猜这些元素是相对放置的,当hide() 通过将display 设置为none 来从文档流中删除元素时,事情开始发生变化??

标签: jquery jquery-ui


【解决方案1】:

hideanimate 在动画完成时执行一个函数。使用它来开始下一个动画,然后在完成后,执行show。比如:

$("#legend").hide("slide", { direction: "left" }, 500, function () {
    $("#map").animate({ width: "1000"}, 500, function () {
        $(".showLegend").show();
    });
}); 

编辑:从 cmets 结合您的 Fiddle,这会产生所需的结果

$(".hideLegend").click(function () {
    $(this).hide(); 
    $("#legend").animate({ width: "0"}, 500);
    $("#map").animate({ width: "1000"}, 500, function () {
       $(".showLegend").show();
       $("#tabs").hide();
    });
});

$(".showLegend").click(function () {
    $(this).hide(); 
    $("#tabs").show();
    $("#legend").animate({ width: "280" }, 500);
    $("#map").animate({ width: "720"}, 500, function () {
        $(".hideLegend").show();
    });
});

演示:http://jsfiddle.net/2Mpdc/3/

【讨论】:

  • 这有帮助,但我希望它同时发生,以便图例在地图填充页面宽度时移动。我之前有过,但它使动画一个接一个地发生,而不是同时发生。
  • @user2217431 使用现有代码但将.showlegend show 包装在地图动画的回调函数中是不可能的吗?你能用你的标记发布一个 jsfiddle 吗?
  • 第一次使用jfiddle;希望我做得对:jsfiddle.net/ltdesign/2Mpdc
  • @user2217431 我已经为您修复了,您不需要在 JavaScript 部分中使用 &lt;script&gt; 标记,使用 jQuery 时您需要从左侧的下拉列表中选择它。这是你固定的:jsfiddle.net/2Mpdc/1
  • @user2217431 如何调用 Fiddle 上的动画?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多