jQuery 动画填充为零

发布于 2024-09-30 06:16:50 字数 578 浏览 0 评论 0原文

我有以下代码片段,可以在悬停时切换填充(请参见示例此处

<div id="outer"><div id="inner"></div></div> 
<script> 
  $("#inner").mouseenter(function () {
    $("#outer").animate({ 'padding' : '20px' }, "slow");
  });
  $("#inner").mouseleave(function () {
    $("#outer").animate({ 'padding' : '0px' }, "slow");
  });
</script>

:目标是让填充动画进入和退出,但是目前没有动画出现用于动画退出。我做了一些测试,如果我将离开填充更改为 10 像素(从 0 像素),它会运行动画,但从零开始并向外动画。我正在运行 jQuery 1.4.3。有办法解决这个问题吗?

I have the following snippet that toggles padding when hovering (see example here):

<div id="outer"><div id="inner"></div></div> 
<script> 
  $("#inner").mouseenter(function () {
    $("#outer").animate({ 'padding' : '20px' }, "slow");
  });
  $("#inner").mouseleave(function () {
    $("#outer").animate({ 'padding' : '0px' }, "slow");
  });
</script>

The goal is to have the padding animate both in and out, however currently no animation appears for animating out. I did some tests, and if I change the leave padding to 10 pixels (from 0 pixels) it runs an animation, but starts at zero and animates outwards. I'm running jQuery 1.4.3. Any way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卖梦商人 2024-10-07 06:16:51

绝对是 1.4.3 中的动画错误,现在您可以通过为各个属性设置动画来解决此问题,如下所示:

$("#inner").mouseleave(function () {
  $("#outer").animate({ 
    'padding-top' : 0,
    'padding-right' : 0,
    'padding-bottom' : 0,
    'padding-left' : 0,
  }, "slow");
});

您可以对其进行测试在这里

Definitely an animation bug in 1.4.3, for now you can work-around by animating the individual properties like this:

$("#inner").mouseleave(function () {
  $("#outer").animate({ 
    'padding-top' : 0,
    'padding-right' : 0,
    'padding-bottom' : 0,
    'padding-left' : 0,
  }, "slow");
});

You can test it out here.

烟沫凡尘 2024-10-07 06:16:51

看起来像 1.4.3 中的一个错误(重写了 css 部分)。 1.4.2 工作正常:

http://www.jsfiddle.net/YjC6y /44/

我将进一步调查并更新这篇文章。

Looks like a bug in 1.4.3 (rewritten css part). 1.4.2 works fine:

http://www.jsfiddle.net/YjC6y/44/

I will investigate it further and update this post.

离笑几人歌 2024-10-07 06:16:51

另一种解决方案是使用 cssHook。 Brandon Aarons jquery-cssHooks 浮现在脑海中(在本例中是 padding 挂钩 marginpadding.js)

您可以在这里测试

Another solution would be to use a cssHook. Brandon Aarons jquery-cssHooks come to mind (in this case the padding hook in marginpadding.js)

You can test it here

云巢 2024-10-07 06:16:51

我刚刚意识到 jquery 对动画中的连字符“-”反应不是很好,但是通过使用连字符并将其后的第一个字母大写,您会得到相同的结果。所以你会得到这样的东西:

    $("#inner").mouseleave(function () {
       $("#outer").animate({
     paddingTop : 0,
         paddingRight : 0,
         paddingBottom : 0,
         paddingLeft : 0,
         borderLeftWidth: 0,
         borderTopWidth: 0,
         borderRightWidth: 0,
         borderBottomWidth: 0,

 }, slow);

I just realized jquery is not reacting very well to hyphens "-" within animation but you get the same result by getting ride of the hyphen and capitalizing the first letter after. So for you will have something like this:

    $("#inner").mouseleave(function () {
       $("#outer").animate({
     paddingTop : 0,
         paddingRight : 0,
         paddingBottom : 0,
         paddingLeft : 0,
         borderLeftWidth: 0,
         borderTopWidth: 0,
         borderRightWidth: 0,
         borderBottomWidth: 0,

 }, slow);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文