【问题标题】:Fading between images图像之间的淡入淡出
【发布时间】:2017-07-04 22:31:17
【问题描述】:

如何使用 jQuery 在两个图像之间进行淡化旋转? 举个例子吧。

<img src="image01.jpg" />
<img src="image02.jpg" />

我喜欢图像在每张 2 秒之间淡入/淡出。

谢谢!非常感激。 :)

【问题讨论】:

    标签: jquery fadein fadeout


    【解决方案1】:

    我敲了一个简单的例子。

    <div id="imageSwap">
    </div>  
    
    
     <script language="javascript" type="text/javascript">
    
        $(document).ready(function () {
          setImageOne();
        });
    
        function setImageOne() {
          $('#imageSwap').fadeIn(500).html('<img src="image01.jpg" />').delay(2000).fadeOut(500, function () { setImageTwo(); });
        }
    
        function setImageTwo() {
          $('#imageSwap').fadeIn(500).html('<img src="image02.jpg" />').delay(2000).fadeOut(500, function () { setImageOne(); });
        }
    
      </script>
    

    【讨论】:

      【解决方案2】:

      将你的 html 重写为

      <div>
      <img class="current slide" src="image01.jpg" style="position:absolute;"/>
      <img class="slide" src="image02.jpg" style="position:absolute;display:none"/>
      <img class="slide" src="image03.jpg" style="position:absolute;display:none"/>
      <img class="dummy" src="image01.jpg" style="visibility:none;"/>
      </div>
      

      为了清晰起见,我添加了第三张图片。 添加一个类似

      的 jquery 函数
      function nextSlide() {
          jQuery('.slide.current').each(function() {
              var next = jQuery(this).next('.slide');
              if (!next.size()) next = jQuery(this).siblings('.slide').eq(0);
              jQuery(this).fadeOut().removeClass('current');
              next.fadeIn().addClass('current');
          }); 
          slidetimer=setTimeout('nextSlide()',slidespeed);
      }
      

      在你的基础 javascript 中,添加

      var slidespeed = 2000;
      var slidetimer = null;
      jQuery(document).ready(function() {
          nextSlide()
      });
      

      我没有完全像这样测试这个,所以可能有错别字。 周围的 div 确保所有幻灯片图像都位于 div 所在的位置,因为它们的位置:绝对。虚拟图像不可见,但确实填充了使周围 div 实际环绕所有图像所需的空间。你可能不需要这个。

      你的图片应该有相似的大小,或者至少虚拟应该和最大的图片一样大。

      $2c, *-派克

      【讨论】:

      • 我刚刚得到了这方面的更新?谢谢,但是嘿,这是 2015 年,不要使用 javascript 执行此操作。使用 css 动画。可选地,使用 javascript 在 setInterval 上切换类,并使用 css 过渡淡化这些类。
      【解决方案3】:

      你可以在没有 jquery 的情况下做到这一点。仅使用 css : 以及图像之间的凉爽淡入淡出

      .fade img {
        transition: all .8s;
        max-width: 200px;
      }
      .fade:hover img:nth-child(2) { 
      	opacity: 0;
      }
      .fade img:first-child { 
        position: absolute;
      	z-index: -1;
      }
      <div class='fade'>
        <img src="https://image.freepik.com/free-vector/software-logo_1103-316.jpg">
        <img src='https://image.freepik.com/free-vector/m-letter-logo-template_1061-150.jpg'>
      </div>

      【讨论】:

        【解决方案4】:

        我一直在寻找一种在网络摄像头图像之间淡入淡出的方法,而不会在显示的图像之间出现那个时刻。我正在做的是将新图像设置为背景图像,淡出封闭图像,将背景图像设置为封闭图像,然后使封闭图像再次可见。

        你可以做点什么

        我想出了这个。

        <style type="text/css">
            #webcamImageDiv {
                text-align: center;
                background-image: url('webcam.jpg');
                background-position: top center;
                background-repeat: no-repeat;
            }
        </style>
        
        <div id="webcamImageDiv">
            <img id="webcamImageImg" src="webcam.jpg" alt="Image Not Available" />
        </div>
        
        <script type="text/javascript">
        
            var refreshMillis = 10000;
        
            function refreshWebcam() {
        
              // set the div to the height of the image
              $("#webcamImageDiv").height( $("#webcamImageImg").height() );
        
              // get the path to the new image
              // (in your case, your other image)
              var newImg = "webcam.jpg?t=" + new Date().getTime();
        
              // set the background of the div
              $("#webcamImageDiv").css("background-image","url('" + newImg + "')");
        
              // fade out the image
              $("#webcamImageImg").fadeOut(500, function() {
        
                // update the img tag, show the image
                $("#webcamImageImg").removeAttr("src").attr("src", newImg).show();
        
                // do it again
                window.setTimeout(getWebcam, refreshMillis);
              });
            }
        
            $(document).ready( function() {
              window.setTimeout(refreshWebcam, refreshMillis);
            });
        </script>
        

        【讨论】:

          【解决方案5】:

          改用 .animate 和 .css 更好地控制时间

          这将创建一个软交叉淡入淡出的卷轴。

          在每个循环中,它会将不可见图像移动到前面,然后将动画不透明度设置为 100。完成后,将前一个可见图像设置为不透明度 0 并移动到前面,等等。

          <div>
                  <div id="imageSwapA"  style="display:block; z-index:100; opacity:0;">
                          <img src="imagea.png"/>
                  </div>
                  <div id="imageSwapB" style="display:block; z-index:101; opacity:0;">
                          <img src="imagea.png"/>
                  </div>
          </div>
          
          <script>
              $(document).ready(function () {
          
                  setRollOne(); // <- animation can be triggered 
          
                  function setRollOne() {
                      $('#imageSwapA').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
                              $('#imageSwapB').css({'opacity':0});
                              $('#imageSwapA').css({'z-index':100});
                              setTimeout(function(){ setRollTwo(); }, 1500);
                      });
                  }
                  function setRollTwo() {
                      $('#imageSwapB').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
                              $('#imageSwapA').css({'opacity':0});
                              $('#imageSwapB').css({'z-index':100});
                              setTimeout(function(){ setRollOne(); }, 1500);
                      });
                  }
              });
          </script>
          

          【讨论】:

            猜你喜欢
            • 2019-02-16
            • 1970-01-01
            • 2013-05-24
            • 1970-01-01
            • 2011-10-20
            • 1970-01-01
            • 1970-01-01
            • 2013-12-19
            • 1970-01-01
            相关资源
            最近更新 更多