简体   繁体   English

JavaScript悬停图片替换

[英]Javascript hover image replacement

I've got some social media icons that I'd like to change color when they're hovered over. 我有一些社交媒体图标,当它们悬停在上面时,我想更改颜色。 I figured the easiest way to do this is by replacing the image with a new one (they're small png files). 我认为最简单的方法是将图像替换为新图像(它们是小的png文件)。 What do you suggest? 你有什么建议? A dynamic function would be ideal.. there are four icons, each of which has a filename of demo.png, demo_hover.png. 动态函数将是理想的。.有四个图标,每个图标都有一个文件名demo.png,demo_hover.png。 I'm using the jQuery library. 我正在使用jQuery库。

Thank you! 谢谢!

HTML 的HTML

  <a class="a" id="interscope_icon" href="http://www.interscope.com/" alt="Interscope Records" target="_blank"><img class="icon" id="interscope" src="../Images/icons/interscope.png" alt="Interscope Records" /></a>
  <a class="a" href="http://www.twitter.com/FernandoGaribay" alt="Twitter" target="_blank"><img class="icon" id="twitter" src="../Images/icons/twitter.png" alt="Fernando Garibay's Twitter" /></a>
</p>
<p>
  <a class="a" href="http://www.facebook.com/f2inc" alt="Facebook" target="_blank"><img class="icon" id="facebook" src="../Images/icons/facebook.png" alt="Fernando Garibay's Facebook" /></a>
  <a class="a" href="http://www.myspace.com/f2inc" alt="Myspace" target="_blank"><img class="icon" id="myspace" src="../Images/icons/myspace.png" alt="Fernando Garibay's Myspace" /></a>
</p>

jQuery jQuery的

$('#interscope_icon').hover(function(){
  $('#interscope').attr('src', 'Images/icons/interscope.png');
});

You're probably better off using the 'CSS sprite' method rather than loading a new image... 您最好使用'CSS sprite'方法,而不是加载新图像...

http://css-tricks.com/css-sprites/ http://css-tricks.com/css-sprites/

The easy way is to do it the CSS way: 最简单的方法是使用CSS方法:

#interscope_icon { background-image: url(...) }
#interscope_icon:hover { background-image: url(...) }

I just had this dilemma as well so came up with my own method. 我也有这个难题,所以想出了自己的方法。 Super easy, works great and no need for sprites, just an transparent PNG: 超级简单,效果很好,不需要精灵,只需透明的PNG:

HTML: HTML:

<div class="facebookicon">
    <a href="#!"><img src="http://example.com/some.png"></a>
</div>

CSS: CSS:

.facebookicon img {
    background: #fff;
    transition:  background 400ms ease-out;
}

.facebookicon img:hover {
    background: #3CC;
    transition:  background 400ms ease-in;
}
/* you need to add various browser prefixes to transition */
/* stripped for brevity */

http://jsfiddle.net/mattmagi/MpxBd/ http://jsfiddle.net/mattmagi/MpxBd/

Let me know what you think. 让我知道你的想法。

I would give the social icons as the background-image on the anchors (with display: inline-block; ) instead of as <img /> elements inside the anchors. 我将社交图标作为锚点上的background-image (带有display: inline-block; )而不是锚点内的<img />元素。

Then simply define an a:hover state that changes the CSS to a different background. 然后只需定义一个a:hover状态即可将CSS更改为其他背景。 Perhaps even sprite them. 甚至还可以sp他们。

Something like this: 像这样:

#some_id { background-image: url(...); }
#some_id:hover { background-image: url(...); }

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

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