change image on mouseover

情到浓时终转凉″ 提交于 2019-11-28 14:43:39

You can use CSS to change the background image of an element when you hover over it. The hover pseudo-class is supported on most elements in recent browsers, but to be backwards compatible (Internet Explorer 6), you need to use an anchor tag for hover to work.

The onclick event in the link is just so that nothing happens if you click it, it has nothing with the hovering effect to do. You can of course do something when the image is clicked, if you like.

HTML:

<a href="#" id="picture" onclick="return false;"></a>

CSS:

#picture {
   display: block;
   width: 200px;
   height: 200px;
   background-image: url(firstimage.gif);
   border: none;
   text-decoration: none;
}
#picture:hover {
   background-image: url(hoverimage.gif);
}

This seems fairly concise and well written (note: CSS rollover solutions will not work on IE6.)

<img src="https://pbs.twimg.com/profile_images/677544618326556672/WID2m1_a.png" onmouseout="this.src='https://pbs.twimg.com/profile_images/677544618326556672/WID2m1_a.png'" onmouseover="this.src='https://pbs.twimg.com/profile_images/638751551457103872/KN-NzuRl.png'" alt="image" width="50px" height="50px">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!