【问题标题】:JQuery: image gallery slide image down as well as fade in srcJQuery:图片库向下滑动图像以及淡入 src
【发布时间】:2012-10-27 01:39:24
【问题描述】:

我有以下代码在单击缩略图时交换主图像:

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

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

这里有一个工作示例:http://clarkeconstructions.com.au/newsite/index.php?id=8

有些图片是纵向的,有些是横向的(如果您点击示例中的第二个缩略图)。

我希望能够将图像向下滑动以使横向到纵向的过渡更平滑,但我不知道如何使用我当前的代码来实现它。

谁能给我任何关于我将使用什么语法来完成此任务的提示?

谢谢

【问题讨论】:

    标签: javascript jquery html image animation


    【解决方案1】:

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

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

    jsBin demo

    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();
    });
    

    【讨论】:

    • 不错的答案队友+1。猜猜,我误读了关于图像扩展的部分(:
    【解决方案2】:

    试试这个,这里是工作示例: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-04-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多