jQuery每个宽度问题

时间:2010-01-29 00:58:01

标签: javascript jquery

我将以下功能设置为在鼠标悬停在图像上时运行:

function() {
  $('.slides .slide h3').each(function(i){
    var owidth = $(this).width()
    $(this).animate({"right":730 - owidth - 16}, 500);
  });
}

您可以查看页面here。在图像上单击图像右下角的下一个图标。由于某种原因,该函数正确地计算了第一个h3的宽度,但是它认为所有其他h3的宽度都是0.任何人都可以提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

function() {
  var owidth  = 0;
  $('.slides .slide h3').each(function(i){
    owidth = $(this).width();
    $(this).animate({"right":(730 - owidth - 16) + 'px'}, 500);
  });
}

更好的方法:

function() {
  $('.slides .slide h3').each(function(i){
    $(this).stop().animate({
      "right": (714 - $(this).width()) + 'px'
    }, 500);
  });
}