简体   繁体   English

功能仅在第二次单击时有效,然后可以正常工作吗?

[英]Function only works on second click, then works fine?

I am having trouble with a navigation menu I have created. 我创建的导航菜单遇到问题。 Basically, the navigation consists of several tabs that is collapsible upon click a button and then expandable when you click that button again. 基本上,导航由几个选项卡组成,这些选项卡在单击按钮时可折叠,然后在再次单击该按钮时可展开。 When collapsed, the ends of the tabs are still visible, and when you hover over the tabs will go back out, and go back in when hovering out. 折叠时,选项卡的末端仍然可见,并且当您将鼠标悬停在这些选项卡上时,它们将退出,而当鼠标悬停时,它们将返回。

So I have the following navigation laid out: 因此,我布置了以下导航:

<ul id="navigation">
<li><a href="../name1/">Name1</a></li>
<li><a href="../name2/">Name2</a></li>
<li><a href="../name3/">Name3</a></li>
<li id = "collapser"><a href = "javascript:void(0)">Collapse All</a></li>
</ul>

And I have the following jQuery/javascript laid out to do this: 我设计了以下jQuery / javascript:

           $("#collapser").click(function(){
            if($("#collapser").hasClass("clicked")){
                $(function() {
                  $('#navigation a').stop().animate({'marginLeft':'-118px'},1000);

                  $('#navigation > li').hover(
                        function () {
                            $('a',$(this)).stop().animate({'marginLeft':'0px'},400);
                        },
                        function () {
                            $('a',$(this)).stop().animate({'marginLeft':'-118px'},400);
                        }
                    );
                 });
                 $("#collapser").removeClass("clicked");
                 $("#collapser").addClass("unclicked");
                 $("#collapser").html('<a href = "javascript:void(0)">Expand All</a>')
             }
             else{
                $(function() {
                  $('#navigation a').stop().animate({'marginLeft':'0px'},1000);

                  $('#navigation > li').hover(
                        function () {
                            $('a',$(this)).stop().animate({'marginLeft':'0px'},400);
                        },
                        function () {
                            $('a',$(this)).stop().animate({'marginLeft':'0px'},400);
                        }
                    );
                 });
                 $("#collapser").removeClass("unclicked");
                 $("#collapser").addClass("clicked");
                 $("#collapser").html('<a href = "javascript:void(0)">Collapse All</a>')
             }
         })

The code works, but only when you click the link a second time. 该代码有效,但仅当您再次单击该链接时。 I can't seem to figure out what I did to make it work only after clicking it once already. 仅在单击一次之后,我似乎无法弄清楚如何使它起作用。

Any help would be appreciated, thank you very much!! 任何帮助将不胜感激,非常感谢! -Quintin -Quintin

EDIT: Here is the CSS for the navigation: 编辑:这是导航的CSS:

ul#navigation {
position: fixed;
margin: 0px;
padding: 0px;
font-size:14px;
top: 14px;
left: 0px;
list-style: none;
z-index:9999;}

ul#navigation li {
width: 100px;
}

ul#navigation li a {
display:block;
margin-bottom:3px;
width:126px;
padding:3px;
padding-left:15px;
background-color:#ffffff;
background-repeat:no-repeat;
background-position:center center;
opacity:0.7;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60);
-moz-box-shadow: 0px 1px 3px #000;
-webkit-box-shadow: 0px 1px 3px #000;
}

ul#navigation .checked a{
color:#ffffff;
background-color:#000000;
}

You are verifying if your collapser div has or not the class clicked or unclicked, but you not initialize the div with some class. 您正在验证您的折叠器div是否单击或未单击该类,但是没有使用某个类初始化div。 Adding the class clicked should adjust your effect: 添加单击的类应调整效果:

<li id = "collapser" class='clicked'><a href = "javascript:void(0)">Collapse All</a></li>

You can see it with this jsfiddle 您可以通过此jsfiddle看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM