0

我目前有代码来翻转图像链接,如下所示:

<a id="show" href="#"><img src="images/show-me.png" border=0 onmouseover="this.src='images/show-me_over.png';" onmouseout="this.src='images/show-me.png'"></a>

单击它会显示一个 div

jQuery(document).ready(function(){
    jQuery("#show").toggle(
        function(){
            jQuery('#home-hidden-extra').animate({'height': '480px'} ,1000);},
        function(){
            jQuery('#home-hidden-extra').animate({'height': '0px'} ,1000);}
    );
});

我想做但我找不到/弄清楚的是在隐藏div时使用show_me.png和show_me-over.png,然后在显示div时使用hide_me.png和hide_me-over.png。

实现这一目标的最简单方法是什么?

再次感谢!

4

2 回答 2

1

您应该将静态悬停样式添加到 CSS。定义

.linkShow {
    background-image: url("images/show_me.png");
}

.linkShow:hover {
    background-image: url("images/show_me_hover.png"); 
}

.linkHide {
    background-image: url("images/hide_me.png");
}

.linkHide:hover {
    background-image: url("images/hide_me_hover.png"); 
}

然后将这些类添加到带有 jquery 的链接中。

$("#show").removeClass().addClass("linkShow");
于 2012-07-19T20:12:59.703 回答
0

这应该有效:

HTML

    <a id="show" href="#">
      <img id="the-image" src="images/show-me.png" />
    </a>

JS

  $(document).ready(function(){
    setupHover(); /* Defined below */
    $('#show').toggle(function(){
        $('#home-hidden-extra').animate({'height': '480px'} ,1000);
        setupHover();
    },function(){
        $('#home-hidden-extra').animate({'height': '0px'} ,1000);
        setupHover();
    });
  });

  function setupHover(){
      /* Unbind existing hover handlers */
      $('#show').unbind('mouseenter mouseleave');

      /* Use the correct image filenames depending on the situation */

      if($('#home-hidden-extra').height() > 0){
        images = ['show-me.png','show-me_over.png'];
      }
      else {
        images = ['hide_me.png','hide_me-over.png'];
      }
      $('#show').hover(function(){
          $('#the-image').attr('src','images/' + images[1]);
        },function(){
          $('#the-image').attr('src','images/' + images[0]);
        });
  }
于 2012-07-19T20:18:30.950 回答