Is my code hacky? If so, I'd love some help
I am creating a grid of thumbnails. When a user hovers over a thumbnail, the image enlarges. The *trick* is, I want the "active area" to only be the size of the thumbnail. That is, the thumbnails are 180x120, and when the mouse leaves that area, I want the large image to shrink back to normal size. I have a working demo here:
http://www.norabrowndesign.com/testLayouts/expanding-photos-li.html
The way I accomplished this feels hacky though, and I's like to know if there's a simpler/more elegant way to accomplish it. The thumbs are an unordered list of images. Here is my javascript:
- $(document).ready(function(){
- $('li').append('<div class="test"></div>').mouseenter(function(){
- $(this).css('z-index',1000);
- $this_img = $(this).find('img');
- photo_pos= $this_img.position();
- $this_img.css({
- 'position': 'absolute',
- 'top': photo_pos.top,
- 'left': photo_pos.left,
- 'z-index': 1001
- }).stop().animate({
- width:600,
- height:400,
- marginLeft:-20,
- marginTop:-10,
- padding:10
- });
-
- $('.test',this).css({
- 'top': photo_pos.top,
- 'left': photo_pos.left
- }).mouseleave(function(){
- $(this).prev().stop().animate({
- width:180,
- height:120,
- marginLeft:0,
- marginTop:0,
- padding:0
- },function(){
- $(this).css('z-index','auto').parent().css('z-index','auto');
- });
- });
- });
- });
In essence, I add an overlay div over each thumbnail which acts as the trigger to shrink the enlarged image. I thought I could use the parent <li>, as it doesn't appear to expand with the image (since I've set its size in my CSS) but that didn't work.
Any feedback is much appreciated!