0

我有以下代码允许缩略图 src attr 替换主图像窗口中的图像。

        $(document).ready(function(){   
            var originalimg = $('#imageMain img').attr('src');
                $(".subImage").hover(function(){
                        var currentimg = $(this).attr('src');
                    $('.mainImage').fadeOut(function () {
                    $('.mainImage').attr('src', currentimg).fadeIn();
                        });
                    },function(){
                            $('.mainImage').fadeOut(function() {
                                $('.mainImage').attr('src', originalimg).fadeIn();
                            })
                            });
            });

目前的行为如下: 1. 悬停在 - 主图像淡入白色,然后淡入子图像。2. 鼠标移出 - 主图像淡化为白色,然后替换为原始图像。

我真正需要的不是两个图像状态之间的“白色”过渡,而是希望它们有点重叠(所以一个淡出而另一个淡入) -

这可能吗?

谢谢

4

1 回答 1

0

您不能使用淡出/淡入顺序旋转图像。您必须以另一种方式进行转换。我已经为您编写了整个代码:

    <head>
    <title>jQuery Image Rotator</title>
   <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
      $("#photoshow").hover(function() {
        setInterval("rotateImages()", 2000); // set the interval time as your wish
    });

    function rotateImages() {
        var oCurPhoto = $('#photoShow div.current');
        var oNxtPhoto = oCurPhoto.next();
        if (oNxtPhoto.length == 0)
            oNxtPhoto = $('#photoShow div:first');

        oCurPhoto.removeClass('current').addClass('previous');
        oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 1000,
            function() {
                oCurPhoto.removeClass('previous');
            });
    }
</script>
       <style type="text/css">
         #photoShow {
        height:400px;
         width:400px;
         }
      #photoShow div {
        position:absolute;
        z-index: 0;
         }
      #photoShow div.previous {
        z-index: 1;
         }
         #photoShow div.current {
          z-index: 2;
           }
</style>
    </head>
  <body>
    <div id="photoShow">
       <div class="current">
        <img src="images/Grass.jpg" alt="Photo Gallery" width="400" height="400" class="gallery" />
    </div>
    <div>
        <img src="images/Leaf.jpg" alt="Photo Gallery" width="400" height="400" class="gallery" />
    </div>
    <div>
        <img src="images/Spring.jpg" alt="Photo Gallery" width="400" height="400" class="gallery" />
    </div>
    <div>
        <img src="images/Water.jpg" alt="Photo Gallery" width="400" height="400" class="gallery" />
    </div>
     </div>
   </body>
    </html>

它将一张一张地转换图像。替换为适当的类、id 名称和图像 url。希望它有所帮助!

于 2012-05-22T13:52:14.443 回答