我稍微修改了您的代码以collapse
用作类而不是仅hover
. 您需要这两个类,因为您希望有两种不同的状态(折叠和未折叠),每个状态都有自己的悬停。您的第一次尝试失败了,因为background-position
无论您想要什么状态,您都在悬停时将其重置为折叠位置。
演示
jQuery
jQuery(document).ready(function ($) {
$('body.home .entry-content h1').append("<div id='hide-text'></div>");
$('body.home .entry-content h1').hover(function () {
$('#hide-text').addClass("hover");
}, function () {
$('#hide-text').removeClass("hover");
});
$('#hide-text').toggle(function () {
$(this).addClass('collapse');
$('body.home .entry-content h2').fadeIn(500);
}, function () {
$(this).removeClass('collapse');
$('body.home .entry-content h2').fadeOut(500);
});
});
CSS
body.home .entry-content h2 {
display: none;
}
#hide-text {
display: inline-block;
position: relative;
top: 2px;
height: 22px;
width: 26px;
margin-left: 15px;
background: url('http://www.mgrear.com/clients/gls/wp-content/themes/gravity-lab-studios/images/home-button.png') 0px 0px no-repeat;
}
#hide-text:hover, #hide-text.hover {
background: url('http://www.mgrear.com/clients/gls/wp-content/themes/gravity-lab-studios/images/home-button.png') -26px 0px no-repeat;
cursor: pointer;
}
#hide-text.collapse {
background: url("http://www.mgrear.com/clients/gls/wp-content/themes/gravity-lab-studios/images/home-button.png") -52px 0px no-repeat;
}
#hide-text.collapse:hover, #hide-text.collapse.hover {
background: url("http://www.mgrear.com/clients/gls/wp-content/themes/gravity-lab-studios/images/home-button.png") -78px 0px no-repeat;
}