1

我有点困惑为什么这不能正常工作。

我有一个<div>根据页面大小环绕的 s 列表。但是单击 div 它.animate()是单击 div 的宽度,并在 div 靠得更近并堆叠它们之后对其进行动画处理。

除了最后一个堆叠的 div 之外,这一切都有效,尽管有足够的空间仍然被撞到下一行。

请在 jsfiddle 上查看我的代码:http: //jsfiddle.net/ZHYRq/2/

$.each($('.employee-box'), function(i, el) {
$(el).addClass("top-" + (Math.round($(el).offset().top)));
return $(el).css('position', 'relative').attr('data-left', Math.round($(el).offset().left));
});

$('.employee-image').hover(function(e) {
    var $employee;
    $employee = $(e.target).parents('.employee-box');
    if (e.type === 'mouseenter') {
        return $($employee.find('a.bio')).addClass('highlight');
    } else {
        return $($employee.find('a.bio')).removeClass('highlight');
    }
});

$('.employee-image, a.bio').click(function(e) {
    var $employee, is_expanded, speed;
    speed = 150;
    $employee = $(e.target).parents('.employee-box');
    is_expanded = $employee.hasClass('bio-expanded');
    if ($('.bio-expanded').length > 0) {
        $.when(collapse_previous_bio(speed)).then(function() {
            if (!is_expanded) {
                return expand_bio_box($employee, speed);
            }
        });
    } else {
        expand_bio_box($employee, speed);
    }
    return false;
});


var collapse_previous_bio = function(speed) {
    var klass;
    klass = "." + $('.bio-expanded').attr('class').match(/top-\d{1,5}/)[0];
    $('.bio-expanded .bio-block').fadeOut(speed, function() {
      $('.bio-expanded').animate({
        width: "185px"
      }, speed);
      $(klass).animate({
        left: '0px'
      }, speed);
      $('.bio-expanded').removeClass('bio-expanded');
    });
  };

  var expand_bio_box = function($employee, speed) {
    var curr_left, klass;
    klass = "." + $employee.attr('class').match(/top-\d{1,5}/)[0];
    curr_left = parseInt($employee.data('left'));

// comment out the $.when block and un-comment out the collapse_others() to see the other elements collapse as they should
$.when(collapse_others(klass, curr_left)).then(function() {
  $employee.animate({
    width: "392px"
  }, speed, function() {
    $employee.find('.bio-block').fadeIn(speed);
    $employee.addClass('bio-expanded');
  });
});
// collapse_others(klass, curr_left)


};



var collapse_others = function(klass, curr_left) {
    var left_pos;
    left_pos = 0;
    $.each($(klass), function(i, el) {
      var el_left;
      el_left = parseInt($(el).data('left'));
      $(el).css({
        zIndex: 100 - i
      });
      if (el_left > curr_left) {
        $(el).animate({
          left: "-" + left_pos + "px"
        }, 100);
        left_pos += 100;
      }
    });
  };

我不确定这里有什么问题。有什么想法吗?

4

0 回答 0