如何将背景图像设置为选中状态?

时间:2012-02-02 03:38:01

标签: javascript jquery

我使用以下脚本在我的菜单项中悬停时更改标签背景图像的位置。但我无法想出一种方法来设置所选状态的背景位置。能帮帮我吗。

这是我用过的脚本。

$(document).ready(function () {
    $('#nav_main li a').hover(function () {
       height = $(this).height() * (-1);
       $(this).stop().animate({'backgroundPosition':'(0 ' + height + ')'}, {duration:300}); 
}, function () {  
    $(this).stop().animate({'backgroundPosition':'(0 0)'}, {duration:300}); 
    }         
);

2 个答案:

答案 0 :(得分:0)

您需要拥有选定的页面信息,例如,如果您在文件夹/home/index.php中,则可以在php中获取变量$folder = 'home';。 然后你设置所有菜单项特殊类,如 myMenuItem-home 当你到达主文件夹时,你有一个像这样的jquery:

$('.myMenuItem-'+folder).stop().animate(
    {'backgroundPosition':'(0 '+($('.myMenuItem-'+folder).height() * (-1))+')'}, {duration:300});

答案 1 :(得分:0)

请参阅hscripts css教程页面,了解如何为列表中的选定项目设置背景图像。 http://www.hscripts.com/tutorials/css/backp3.php