jquery悬停效果

时间:2010-01-10 01:02:43

标签: javascript jquery

我怎样才能获得这样的效果

http://bavotasan.com/demos/fadehover/

但是对于不是用于图像的锚标签,假设我有锚背景蓝色并且我希望它变为红色但是有了这个效果,我该怎么做?谢谢

3 个答案:

答案 0 :(得分:4)

使用hoveranimate。请注意,这需要jQuery颜色动画插件。

<html>
<head>
  <title>color change</title>
  <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
  <script src="http://plugins.jquery.com/files/jquery.color.js.txt" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript" charset="utf-8">
    $(function() {
      $('a').hover(function() {
        $(this).animate({
          color: "#f00"
        }, "fast")
      }, function() {
        $(this).animate({
          color: "#00f"
        }, "fast")
      });
    });
  </script>
  <style>
    a { color: #00f; }
  </style>
</head>
<body>
  <a href="#">This changes color on hover.</a>
</body>
</html>

在更改a元素上的颜色的示例中,没有理由使用您提供的链接中使用的交叉渐变效果。

答案 1 :(得分:3)

将两个图像放在彼此之上(通过CSS:位置 z-index )。一个黑色和白色,和一个颜色:

/* Assumes width and height are the same between all three elements */
.viewBox  { position:relative; width:125px; height:125px; display:block; }
img.color { position:absolute; top:0; left:0; z-index:10; }
img.bandw { position:absolute; top:0; left:0; }

<a class="viewBox" href="http://google.com">
  <img src="color.jpg" class="color" />
  <img src="bandw.jpg" class="bandw" />
</a>

$(".viewBox").hover(
  function() {
    $("img.color").fadeIn();
  },
  function() {
    $("img.color").fadeOut();
  }
);

-

或者,您可以在没有jQuery的情况下使用纯css完成此操作:

span.hov span            { display:none; }
.rollover                { display:block; background-image:url('bandw.jpg'); 
                           width:125px; height:125px; }
.rollover span.hov       { display:none;  background-image:url('color.jpg');
                           width:125px; height:125px; }
.rollover:hover span.hov { display:block; }

<a class="rollover">
  <span class="hov">
    <span>Invisible Link Text</span>
  </span>
</a>

答案 2 :(得分:0)

您可以在网页上找到HTML,CSS和jQuery。

在这里,创作者已经将2张图像叠加在一起,然后使用jQuery来改变不透明度。

<div class="fadehover">
  <img class="a" alt="" src="cbavota-bw.jpg" style="opacity: 1;"/>
  <img class="b" alt="" src="cbavota.jpg"/>
</div>

$(document).ready(function(){
  $(".a").hover(function() {
    $(this).animate({"opacity": "0"}, "slow");
  },
  function() {
    $(this).animate({"opacity": "1"}, "slow");
  });
}); 
相关问题