我想使用hover函数改变按钮的背景图片y轴位置。是否有简单的方法来保留xpos,或者我需要先获取位置,再拆分并再次使用$.css()。
如果有人悬停在其中任何一个上面,我应该更改所有3个span的背景位置。因此,bt_first:hover似乎无法使用。
这是我的用法。我在“#should stay same#”的位置写了这句话,表示我不想更改xpos的值:
$('.bt_first,.bt_sec,.bt_third').hover(function(){
$('.bt_first,.bt_sec,.bt_third').css({'background-position':'#should stay same# -150px'})
},function(){
$('.bt_first,.bt_sec,.bt_third').css({'background-position':'#should stay same# -110px'});
});
以下是我的HTML代码:
<div><a id="add_comment_btn"><span class="bt_first comments_t"><span> </span></span><span class="bt_sec"> </span><span class="bt_third">Comments</span></a></div>
以及 CSS:
.bt_first,.bt_sec,.bt_third,.logout_t,.comments_t span {
background: url('img/toolbar_bckrnd.png') no-repeat;
}
.bt_first {
background-position: left -110px;
display: inline-block;
height: 24px;
width: 15px;
}
.bt_sec {
background-position: -149px -110px;
display: inline-block;
height: 24px;
width: 2px;
}
.bt_third {
background-position: right -110px;
display: inline-block;
height: 24px;
padding: 0 10px;
}
background-position
,将两个值分离出来,更改其中一个值,然后重新设置回去。这正是你想要做的。 - thirtydot