我有大约 10 个按钮,每个按钮都有图像文本和悬停状态。我想要做的是使用背景位置、宽度和高度来仅显示背景图像精灵的一部分,并使用悬停背景位置来显示悬停样式。我还将在元素上使用图像替换类,以便它保持可访问性和可索引性。
所以(测量是随机的):
[CSS]
.menu{background-image:url(path/to/sprite.png);}
.button-1{width:200px;height:30px;background-position:0 0;}
.button-1:hover{background-position:0 -30px;}
.button-2{width:250px;height:30px;background-position:100px 0;}
.button-2:hover{background-position:100px -30px;}
/* Image Replacement Class (H5BP, @necolas && BEM) */
.ir{border:0;font:0/0 a;text-shadow:none;color:transparent;background-color:transparent;}
[HTML]
<a href="someLink.html" class="menu button-1 ir">Button 1</a>
<a href="someOtherLink.html" class="menu button-2 ir">Button 2</a>
我想知道的是,这是否是这样做的好方法,还是应该以不同的方式完成,例如:
<a href="someLink.html"><img src="image.png" width="200" height="30" alt="Button 1"/></a>
然后使用 JavaScript 在悬停时交换图像。
两者在可访问性和机器人方面有什么区别吗?
谢谢你。