2

I have the following code that swaps a primary image when a thumbnail is clicked:

$('#thumbs img').click(function(){
    var imgsrc = $(this).attr('src');

    $('#mainphoto img').fadeOut(function(){
       $(this).attr('src', imgsrc).fadeIn();
    });
});

There is a working example here: http://clarkeconstructions.com.au/newsite/index.php?id=8

Some of the images are portrait shots and some are landscape (if you click the 2nd thumbnail in the example).

I want to be able to slide the image down to made the transition of landscape to portrait smoother but I am not sure how to go about it with my current code.

Would anyone be able to give me any hints as to what syntax I would use to accomplish this?

Thanks

4

2 回答 2

2

您可以使用的一个技巧是:

  • 将所有原始图像大小存储在每个图像data属性中
  • 将内部图像设置为 H: 100% 和 W: 100% 时为容器设置 W/H 动画

jsBin 演示

HTML:

<div id="projphotos">
  
  <div id="mainphoto"><img src="" /></div><!--MAINPHOTO-->
  
  <div id="projthumbs">
    <img src="images/image_1.jpg" /> 
    <img src="images/image_2.jpg" /> 
    <img src="images/image_3.jpg" /> 
  </div><!--PROJTHUMBS-->
  
</div>

CSS:

#mainphoto{ /* roXon */
   border-radius: 5px 5px 5px 5px;
   box-shadow: 0 0 5px #888888;
   padding: 10px;
   display:inline-block;
   *dispaly:inline;
   zoom:1;
   width:0;
   height:0;
}
#mainphoto img{
   position:relative;
   margin:0 auto;
   vertical-align:middle;
   width:100%;
   height:100%;
}
#projphotos img {
    /* removed properties */
    background-image: url("tmpimages/billie_holiday.png");
}

jQuery(存储/使用原始图像大小来动画主图像父:)

$(window).load(function(){
  
  var imgs = $('#projthumbs img');

  $.each(imgs, function(i, el){
    $('<img>', {"src": this.src}).load(function(){
      $(el).data({'h': this.height, 'w': this.width});
      if(i===0){
        $(el).click(); 
      }
    });
  });
    
  $('#projthumbs img').click(function(){ 
      var imgsrc = this.src;
      var orgSize = { h: $(this).data('h'), w: $(this).data('w') };
  
      $('#mainphoto img').stop().fadeTo(400,0,function(){
         $(this).attr('src', imgsrc).fadeTo(400,1);
         $('#mainphoto').animate({height: orgSize.h, width: orgSize.w },400);
      });
  });
  
  $('#projthumbs img').eq(0).click();
});
于 2012-11-07T03:03:10.850 回答
0

试试这个,这里是工作示例:http: //jsfiddle.net/mjaA3/216/

jQuery:

var thumbs = $('ul.thumbHolder li');
var bigImg = $('.mask img');
//You need to mask your image for creating sliding it down effect.

thumbs.first().addClass('selected');
thumbs.click(function() {

    var imgsrc = $(this).children('img').attr('src');

    bigImg.animate({'top':'266px'},300,function() {
       bigImg.attr('src', imgsrc ).animate({'top':'0px'},300);
    });

    thumbs.removeClass('selected');
    $(this).addClass('selected');
});

html:

<div class="mask">
      <img width="270" src="http://sample1.jpg" />
</div>
<ul class="thumbHolder">
   <li>
      <img width="20" src="http://sample1.jpg" />
   </li>
   <li>
      <img width="20" src="http://sample2.jpg" />
   </li>
   <li>
      <img width="20" src="http://sample3.jpg" />
   </li>
</ul>

CSS:

.mask {
        overflow:hidden; position:relative; left:0; top:0;
        float:left; margin:40px; width:400px; height:266px;
}
       .mask img { position:absolute; top:0px;left:0px; }

不使用 src 的替代方式:http: //jsfiddle.net/mjaA3/197/

于 2012-11-07T02:57:49.493 回答