【问题标题】:Stop one function in jQuery and run another在 jQuery 中停止一个函数并运行另一个函数
【发布时间】:2014-01-16 14:56:30
【问题描述】:

我正在尝试在页面向下滚动时弹出的 div 上创建最小化效果。一切都很好,只有一个问题。如果最小化功能完成并且我向上滚动页面,它将恢复其原始值。我希望它在单击最小化按钮并向上滚动一点后,它应该保持最小化并且不恢复。

这是我正在使用的

$(function () {
    $(window).scroll(function () {
        var height = $(window).height() /2;
        if ($(this).scrollTop() >= height) {
            $("#box").animate({'bottom':'0px'},300);
        }
        else {
            $("#box").stop(true).animate({'bottom':'-150px'},100);
        }
}); });

$('.minimize').toggle(function() {
  $('#box').animate({'bottom':'-80px'},200);},
  function() { $('#box').animate({'bottom':'0px'},200);
});

查看演示fiddle

【问题讨论】:

  • 添加一个开关/状态管理器,添加一个类来通知用户已经点击了最小化,如果为真 - 不要动画。

标签: jquery html minimization minimized


【解决方案1】:

我同意Machiee的评论。

你可以这样做:

$(function () {
    $(window).scroll(function (e) {
        if($('#box').hasClass('minimized'))
            return;
        var height = $(window).height() /2;
        if ($(this).scrollTop() >= height) {
            $("#box").animate({'bottom':'0px'},300);
        }
        else {
            $("#box").stop(true).animate({'bottom':'-150px'},100);
        }
}); });

$('.minimize').toggle(function() {
  $('#box').animate({'bottom':'-80px'},200);
  $('#box').addClass('minimized');
},
  function() { $('#box').animate({'bottom':'0px'},200);
              $('#box').removeClass('minimized');
});

查看演示fiddle

【讨论】:

  • 我也在考虑removeClass(),但代码与我之前制作的结构不匹配。这个工作正常。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
相关资源
最近更新 更多