繁体   English   中英

使用onclick切换将Javascript图像翻转

[英]Javascript Image roll over with onclick toggle

我目前有代码可以将图像链接如下所示:

<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>

在单击此它显示一个股利

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。

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

再次感谢!

您应该将静态悬停样式添加到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");

这应该起作用:

的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]);
        });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM