views:

112

answers:

2

I have a div full of 60x60 images arranged in rows of 5. I want to show an enlarged version (at 300x300 max) of each thumbnail when you hover each image. I have a surrounding div that hides overflow, so if you hover over an image at one of the corners, the enlarged image will get cut off, so I need to flip the image to the other side. The code I have currently fails at addressing this clipping problem. Any ideas how to properly implement this, or are there any packaged solutions out there (preferably using jQuery)?

  $('a.thumb img').live('mouseover', showThumbnail);
  $('a.thumb img').live('mouseout', function() { $('#image-thumbnail').hide(); });
  $('#images a.thumb').live('click', function() { return false; }); /* disable clicks */

  function showThumbnail() {
    var pos = { left: this.offsetLeft, top: this.offsetTop }
    var css = { }

    css['left'] = pos.left + 60 + 5;
    if (css['left'] + 300 > this.offsetParent.offsetWidth)
    {
      css['right'] = pos.left - 5 + 'px';
      delete css['left'];
    } else {
      css['left'] += 'px';
    }

    css['top'] = pos.top + 60 + 5;
    if (css['top'] + 300 > this.offsetParent.offsetHeight+50)
    {
      css['bottom'] = pos.top - 5 + 'px';
      delete css['top'];
    } else {
      css['top'] += 'px';
    }

    $('#image-thumbnail').css(css);

    $('#image-thumbnail').html("<img style='max-width: 300px; max-height: 300px;' src='" + $(this).parent()[0].href + "' />").show();  
  }
A: 

I'm using the following to display tooltips over a chart generated by Flot, it has edge detection logic for the window...

function showTooltip(x, y, contents){
    $('<div id="tooltip">' + contents + '</div>').appendTo('body');

    var wx=$(window).width();
    var wy=$(window).height();

    var ox=$('#tooltip').width();
    var oy=$('#tooltip').height();

    var t, l, b, a;

    b=x-40; a=wx-x-40;
    if(b<ox && ox>a) l=20;
    else if(ox<a) l=x+20;
    else l=x-ox-20;

    b=y-40; a=wy-y-40;
    if(b<a) t=y+20;
    else t=y-oy-20;

    $('#tooltip').css({top:t,left:l,opacity:0.80}).fadeIn(200);
}
CptSkippy
A: 

Pop it up in a separate div that is absolutely positioned, with a higher z-index, and outside of the div that has the clipping. I generally use something like

$('#largerImageDiv').html(''); $('#largerImageDiv').show();

then on mouse out

$('#largerImageDiv').hide();

In the larger image div, you assign a style that makes it absolutely positioned, preferably in another portion of the page that they wouldn't be using at the same time as your thumbnail area. You set a z-index that is higher than any other div in that spot.

Russell Steen