【发布时间】:2013-04-01 14:39:45
【问题描述】:
基本上这是一个菜单,悬停时,一个单独的图像将出现在链接的两侧。我已经做到了这一点,但是我似乎无法摆脱链接上的白色间距。悬停时链接上方不应有空白。似乎无法弄清楚我错过了什么!?
<li><a href="#" class="button">Button 1</a></li>
<li><a href="#" class="button">Button 2</a></li>
<li><a href="#" class="button">Button 3</a></li>
$(document).ready(function(){
$(".button").hover(function() {
$(this).before('<div class="buttonimgleft"></div>');
$(this).after('<div class="buttonimgright"></div>');
}, function() {
$(".buttonimgleft, .buttonimgright").remove();
});
});
body {
margin: 0;
}
li {
list-style: none;
display: inline-block;
margin-right: 15px;
}
.buttonimgright {
display: inline-block;
background: #CC0000 url('insert-right-image.png');
width: 15px;
height: 20px;
}
.buttonimgleft {
display: inline-block;
background: #CC0000 url('insert-left-image.png');
width: 15px;
height: 20px;
}
a {
height: 20px;
color: #000;
background: #CC0000;
font-size: 16px;
}
【问题讨论】:
-
一点不请自来的建议:CSS 的想法是共享规则并仅修改异常。您有显示、宽度和高度相同的元素;它们可以共享相同的规则,而不是重新声明。 :)
-
尽管它们不共享相同的背景图像。有什么建议吗?
-
不是说这是完美的,但想法是这样的:jsfiddle.net/vEXG3/8 字符少了一点,但更重要的是,您可以在一个规则中进行更改,而不是三个。