views:

39

answers:

1

I have the following function set to run on a hover over an image:

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

You can view the page here. Over the image and click the next icon on the lower right of the image. For some reason, the function is calculating the first h3's width correctly, but then it thinks all other h3s have a width of 0. Can anyone offer a solution?

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

A better way:

function() {
  $('.slides .slide h3').each(function(i){
    $(this).stop().animate({
      "right": (714 - $(this).width()) + 'px'
    }, 500);
  });
}
TiuTalk
+1 for scope answer
Jason
An explanation of why this works and the OP's function doesn't would be great.
musicfreak
thank. I got it fixed.
Josh
There was a ; missing on the 5th line and I think you cant use "var owidth" multiple times, just initiate the var before and then use it.. :)
TiuTalk