【发布时间】:2011-02-02 05:02:57
【问题描述】:
我是新手。 我正在用 html 编写,我很好奇当链接悬停时是否可以切换到另一个图像。
【问题讨论】:
我是新手。 我正在用 html 编写,我很好奇当链接悬停时是否可以切换到另一个图像。
【问题讨论】:
是的,您可以,here 是一个使用 javascript 的示例。
【讨论】:
当然。如果您的代码如下所示:
<div class="menu">
<a href="1.html">First</a>
<a href="2.html">Second</a>
<a href="3.html">Third</a>
</div>
只需在你的头脑中添加一些样式:
<style type="text/css">
/* These are comments, and are ignored by the browser
. {dot} is the CSS selector for a class
We are selecting all of the "a" elements inside of
the element with a class of "menu"
*/
.menu a {
background-image: url('link.gif');
width: 100px;
}
/* :hover, :active, and :focus are all pseudo-selectors -- they select
elements based on their state rather than based on their attributes
*/
.menu a:hover, .menu a:active, menu a:focus {
background-image: url('link_hover.gif');
}
</style>
开始学习 HTML 和 CSS 的最佳地点之一是W3Schools。阅读他们的示例将使您牢牢掌握基础知识,然后浏览此处的HTML 和CSS 问题将使您对事物的工作方式有更深入的了解。祝你好运!
【讨论】:
我强烈推荐使用 CSS 精灵:
http://www.alistapart.com/articles/sprites
这个概念一开始可能有点难以理解,但您基本上是将原始图像及其悬停状态(即,当链接悬停时您正在交换的图像)组合成一个图像,然后一次只显示该图像的适当部分作为锚元素上的定位背景图像。
这种方法有几个显着的好处:
当然,这假定您要切换的图像是链接图像本身,而不是当您将鼠标悬停在链接上时会发生变化的页面上的其他元素。这当然是可能的,但需要 JavaScript。
【讨论】: