jquery - 使用 jquery 丢失 css 悬停

标签 jquery css hover

CSS 悬停正常工作,直到单击菜单项之一。 我相信问题是从我添加这一行开始的

$("#buttons li a:not(a." + target + ")").css("background-position","0 0");

CSS

#buttons {float:left;}
#buttons ul {list-style-type:none;cursor:pointer;overflow:hidden;}
#buttons ul  li {height:195px;width:40px;float:left}
#buttons a {display:block;height:195px;width:40px;border:none;cursor:pointer;}
#buttons a.settings:hover, #buttons a.duels:hover, #buttons a.messages:hover {background-position: -40px 0;}
#buttons a.settings {background:url(accountsettings.png)}
#buttons a.something {background:url(accountsomething.png)}
#buttons a.messages {background:url(accountmessages.png)}

#text{width:600px;height:199px;overflow:hidden;float:left;}
div#text div {width:600px;height:199px;float:left; margin-left:20px;}

HTML

<div id="buttons">
        <ul>
            <li><a class="settings"></a></li>
            <li><a class="something"></a></li>
            <li><a class="messages"></a></li>
        </ul>
    </div>

    <div id="text">
       <div id="settings">
          <h2>Account Settings</h2>
          <p>Lorem ipsum dolor sit amet, consectetuer adipiscing</p>
       </div>
       <div id="something">
          <h2>Something</h2>
          <p>Lorem ipsum dolor sit amet, adipiscing elit. Nulla quam.</p>
       </div>
       <div id="messages">
          <h2>Messages</h2>
          <p>Lorem ipsum dolor sit amet, adipiscing elit. Nulla quam.</p>
       </div>      
    </div>

脚本

 <script language="javascript" type="text/javascript">

    $("div#text div:not(#settings)").hide();
    $("#buttons li a.settings").css("background-position", "-40px 0");

    $("#buttons li a").click(function(){
        var target = $(this).attr("class");
        $("#buttons li a:not(a." + target + ")").css("background-position","0 0");
        $(this).css("background-position", "-40px 0");
        $("#"+target).show("slide", { direction: "left" }, 500);
        $("#text div").not("#"+target).hide("slide", { direction: "right" }, 500);
    });

 </script>

最佳答案

这就是你想要的吗 - Working Demo

$(function() {

    $("div#text div:not(#settings)").hide();
    $("#buttons li a.settings").css("background-position", "-40px 0");

    $("#buttons li a").click(function(){
        var target = $(this).attr("class");
        $("#buttons li a:not(a." + target + ")").css("background-position","0 0");

        $(this).css("background-position", "-40px 0");
        $("#"+target).show("slow");
        $("#text div").not("#"+target).hide("slow");
        return false;
    });
});

show()hide() jQuery 命令仅采用两个参数:速度和回调。您有一个字符串动画名称、一个对象和一个数字作为参数。

关于jquery - 使用 jquery 丢失 css 悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1053145/

相关文章:

javascript - 如何让段落元素显示在悬停在输入元素上

javascript-如何检测 iframe 中的滚动事件?

javascript - TypeError : $(. ..).datetimepicker 不是函数

为大屏幕隐藏 CSS 溢出

css - 使用类前缀创建 CSS 规则

导航中的 CSS 背景图像(稍后悬停)

css - 鼠标移开时下拉菜单的消失延迟 1 秒,除非返回

jquery - 有谁知道一个好的 jQuery 内容 slider 可以根据内容自动调整包含的 div 的高度?

jquery - 容器div中的图像拟合

javascript - 没有 p 标签的 Div 文本选择器