我正在寻找一种方法来改变切换时的背景图像,如下所示:
jQuery - Toggle Image Expand/Close
我的问题是我有这个HTML:
<button id="close-menu"></button>
<div id="menu-bar">
<div class="wrapper">
<a href="http://www.website.com" target="_blank">
<img src="image.png" alt="">
</a>
<div class="options">
<h2>eBlast Tools</h2>
<ul>
<li class="toggle-images">Images are <span>enabled</span>. Click to disable.</li>
<li class="download-zip"><a href="download.zip">Download ZIP File</a></li>
</ul>
</div>
</div>
</div>
这是我目前的jquery:
$('#close-menu').click(function() {
$('#menu-bar').slideToggle(400);
return false;
});
这是css:
#close-menu {
background: url(../img/minus-button.png);
background-position: top left;
width: 25px;
height: 25px;
position: absolute;
top: 10px;
right: 20px;
z-index: 100;
border: none;
}
#close-menu:hover {
background: url(../img/minus-button.png);
background-position: bottom left;
}
答案 0 :(得分:7)
假设您想要更改close-menu
的背景 - 尝试这样的事情:
$('#close-menu').click(function() {
$('#menu-bar').slideToggle(400, function(){
if($(this).is(':visible')){
$('#close-menu').css('background-position','bottom left');
} else {
$('#close-menu').css('background-position','top left');
}
});
return false;
});
回复您的评论 - 您最好创建一些像这样的CSS:
.minus { background: url(../img/minus-button.png) top left no-repeat; }
.minus:hover { background-position: bottom left; }
.plus { background: url(../img/plus-button.png) top left no-repeat; }
.plus:hover { background-position: bottom left; }
将minus
的课程添加到close-menu
,然后执行:
$('#close-menu').click(function() {
$('#menu-bar').slideToggle(400, function(){
$('#close-menu').toggleClass('minus');
$('#close-menu').toggleClass('plus');
});
return false;
});
答案 1 :(得分:1)
试试这个,
if($('#divname:visible'))
{
$('#close-menu').css('background-image','img.jpg');
}
else
{
$('#close-menu').css('background-image','img.jpg');
}