【问题标题】:Change slideDown to Animate将 slideDown 更改为动画
【发布时间】:2015-11-14 18:59:33
【问题描述】:

我想改变这个 slideDown 效果:

$(document).ready(function () {
    $('.slide').not("#n1").hide();
    $('.slide').click(function () {
        var $this = $(this);
        var $next = $this.next();
        if (!$next.length) {
            $next = $this.siblings(".slide").first();
        }
        $this.css("z-index","-1"); //make sure $next can slide OVER $this
        $next.slideDown(2000, function () {
            $this.hide().css("z-index","0");
        });
    });
});

到这个动画效果:

$(document).ready(function () {
    $('.slide').not("#n1").hide();
    $('.slide').click(function () {
        var $this = $(this);
        var $next = $this.next();
        if (!$next.length) {
            $next = $this.siblings(".slide").first();
        }
        $this.css("z-index","-1"); //make sure $next can slide OVER $this
        $next.animate({marginTop: "400px"}, 2000, function () {
            $this.hide().css("z-index","0");
        });
    });
});

目标是从边缘上方向下滑动“下一个”div,以覆盖可见的 div。

.slide {height:500px;width:100%;background-color:#f2f2f2;border-bottom:5px solid #f2f2f2;position:absolute;}}

<div id="n1" class="slide" style="display:inline">div one</div>
<div id="n2" class="slide" style="display:block;margin-top:-400px">div two</div>

动画功能根本不起作用。任何人都可以帮助解决它吗?我是否正确使用动画: $next.animate({marginTop: "400px"}, 2000, function () {...

【问题讨论】:

  • 好的,你的问题是什么?
  • 动画根本不起作用。谁能推荐一个解决方案?我会编辑帖子。

标签: jquery jquery-animate


【解决方案1】:

animate 功能正在运行,但您无法看到它,因为您已将其隐藏在 $next 元素中

 $('.slide').not("#n1").hide();

slideDown() 方法将元素设置为:

 display="block"

它正在为这个元素设置动画。

为了实现与 animate() 类似的效果,您必须在动画之前显示()您的元素。

我还假设您希望将 $next div 设置为页面顶部的动画,以便您可以将 marginTop 值设置为 0:

$next.show().animate({marginTop: 0}, 2000, function () {
      $this.hide().css("z-index","0");
});

工作fiddle

【讨论】:

  • 问题。原始代码设置为“循环”所有 div 并重置回第一个。我如何将旧 div 返回到默认的 -500px margin-top?我希望效果循环,这需要在某个时候将它们全部重置。
  • 要将所有 div 重置为边距 -500px,您可以执行以下操作:$('.slide').css("margin-top", "-500px")
猜你喜欢
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
相关资源
最近更新 更多