【问题标题】:jQuery Hover image change animationjQuery Hover 图像变化动画
【发布时间】:2012-04-19 19:36:35
【问题描述】:

我有一个带有灰度图像的 IMG 标签。我连接了一个 Hover() 事件,以便在悬停时将“src”更改为彩色图像,并在鼠标移出时返回灰度。

这很好用,但是变化是突然的。我想在效果中添加一点淡入淡出(),以便颜色看起来淡入淡出。我写了以下我认为可行的内容,但没有。 (图像发生变化,但效果没有褪色或延迟)。我做错了什么?

            $("#showForm").hover(
              function () {
                $(this).fadeIn('slow', function(){
                    $(this).attr("src", 'images/AddButtonSmall.gif');
                });
              }, 
              function () {
                $(this).fadeIn('slow', function(){
                    $(this).attr("src", 'images/AddButtonSmallGray.gif');
                });
              }
            );

【问题讨论】:

  • 您不能淡入 .src 更改。要在旧图像淡出时淡入新图像,您通常需要使用两个重叠图像并淡入和淡出。

标签: jquery


【解决方案1】:

试试这个

         $(".image_hover").mouseover(function(){
        var old_src=$(this).attr("src");
        var new_src=$(this).attr("data-alt-src");
        $(this).attr('src', new_src);
        $(this).attr('data-alt-src', old_src);
      });
     $(".image_hover").mouseout(function(){
        var old_src=$(this).attr("src");
        var new_src=$(this).attr("data-alt-src");
        $(this).attr('src', new_src);
        $(this).attr('data-alt-src', old_src);
      });

【讨论】:

    【解决方案2】:

    您也可以使用过渡使用 css3 进行交叉淡入淡出。 不适用于所有浏览器,但仍然... http://www.w3schools.com/css3/css3_transitions.asp

    类似于CSS: image link, change on hover

    例如:

    #showForm
    {
        background: transparent url('images/AddButtonSmallGray.gif') center top no-repeat;
    
        -webkit-transition:all 0.3s ease-in-out;
        -moz-transition:all 0.3s ease-in-out;
        transition:all 0.3s ease-in-out;
    }
    
    #showForm:hover {
        background-image: url('images/AddButtonSmall.gif');
    }
    

    【讨论】:

      【解决方案3】:

      这里有几个解决方案。您的代码不起作用,因为您正在设置立即更改图像的源。加载两个图像可能更容易,用 CSS 覆盖它们,然后在父容器悬停时淡出颜色。或者你可以交叉淡化它们

      css

      .fader { display: inline-block; }
      .fader img:last-child {
          position: absolute;
          top: 0; 
          left: 0;
          display: none;
      }​
      

      html

      <div class="fader">
          <img src="http://placehold.it/300x300/000fff" />
          <img src="http://placehold.it/300x300/fff000" />
      </div>​
      

      鼠标悬停时淡入

      http://jsfiddle.net/Xm2Be/3/

      $('.fader').hover(function() {
          $(this).find("img:last").fadeToggle();
      });​
      

      交叉淡入淡出

      http://jsfiddle.net/Xm2Be/2/

      $('.fader').hover(function() {
          $(this).find("img").fadeToggle();
      });​
      

      【讨论】:

      【解决方案4】:

      更好的解决方案:

      忘记 javascript 来完成这项工作。请改用 CSS 的 sprites。如果您坚持使用淡入、淡出,请使用过渡。

      更新:针对下面的 cmets,我无法编写一些代码,因为我无法预测 Todd 精灵中的尺寸和位置。我还想澄清一下,我的建议是使用精灵,然后使用过渡来增强“功能”,而不仅仅是过渡,因为我认为他需要它才能在 IE8 和 IE9 中工作。

      【讨论】:

      • 一个谈论精灵而不是关于如何进行图像转换的网站并不是很有帮助。这根本没有回答他的问题。
      • 同意 sprites 评论并不真正相关;但是你可以通过 CSS3 过渡来做到这一点;我只是想看看代码。
      • 感谢您的建议。我对使用精灵并不是很熟悉,我想阅读它,所以这是一个很好的“刺激”。 :)
      【解决方案5】:
       $(this).fadeIn('slow', 
      

      实际上是在尝试淡入这个元素,慢慢地,'this'指的是

      $("#showForm")
      

      如果您愿意,可以将彩色图像直接放在当前灰度图像上,并将彩色图像标记为隐藏,然后让它慢慢淡入。假设它们相互重叠,您可以这样做:

      $("#showForm").hover(
          function () {
             $("#colorImageID").fadeIn();
          }, 
          function () {
             $("#colorImageID").fadeOut();
          });
        }
      

      );

      假设 html 类似于(其中 sameAbsolutePosition 是 css 以将它们放在同一个确切位置,所以可能类似于 position:absolute; left:20; top:20px;):

      <img src='images/AddButtonSmallGray.gif' class="sameAbsolutePosition">
      <img src='images/AddButtonSmall.gif' style="display:none;" class="sameAbsolutePosition">
      

      重申一下,您将在另一张图片上淡入一张新图片。您也可以同时淡出灰度图像。

      【讨论】:

        【解决方案6】:

        试试这个

         $("#showForm").hover(
                  function () {
                      $(this).fadeOut('fast',function(){
                         $(this).attr("src", 'images/AddButtonSmall.gif');
                         $("#img_src").html("images/AddButtonSmall.gif"); 
                      });
                    $(this).fadeIn('fast');
                  }, 
                  function () {
                    $(this).fadeOut('fast',function(){
                         $(this).attr("src", 'images/AddButtonSmallGray.gif');
                         $("#img_src").html("images/AddButtonSmallGray.gif"); 
                      });
                    $(this).fadeIn('fast');
                  }
                );​
        

        这里是Fiddle

        【讨论】:

          【解决方案7】:

          你在哪里淡出? 在不透明度从 0 变为 100 的情况下淡入 display:blockdisplay:none 淡出,不透明度从 100 变为 0。

          一旦你淡入,图像是display:block,不透明度是100。所以,你看不到动画。 所以,要么在 fadeIn 之前执行 display:none 或 fadeOut。

          给出的是解释事情的例子。你应该根据你的要求改变它。

          $("#showForm").hover(
                        function () {
                          $(this).fadeOut('fast').fadeIn('slow', function(){
                              $(this).attr("src", 'images/AddButtonSmall.gif');
                          });
                        }, 
                        function () {
                          $(this).fadeOut('fast').fadeIn('slow', function(){
                              $(this).attr("src", 'images/AddButtonSmallGray.gif');
                          });
                        }
                      );
          

          【讨论】:

            猜你喜欢
            • 2013-02-10
            • 1970-01-01
            • 2013-08-23
            • 1970-01-01
            • 2018-12-14
            • 1970-01-01
            • 1970-01-01
            • 2015-01-09
            • 1970-01-01
            相关资源
            最近更新 更多