简体   繁体   English

在div之间切换时删除活动状态

[英]Removing active state when toggling between divs

I have created multiple top down menu items. 我创建了多个自上而下的菜单项。 When the links are clicked a div slides down to show some content. 单击链接后,div会向下滑动以显示一些内容。

What I am trying to do with these links is toggle between them. 我正在尝试使用这些链接进行切换。 When one div is opened an active state is added to the link, when it is closed the active state is removed and the div hidden. 打开一个div时,会将活动状态添加到链接中;当关闭一个div时,将删除活动状态并隐藏div。 When you click between the links I have managed to get them to toggle between each other and the active state is added to the div that is opened. 当您在链接之间单击时,我设法使它们彼此切换,并且将活动状态添加到打开的div中。

What I cannot achieve is removing the active state and resetting some css. 我无法实现的是删除活动状态并重置一些CSS。

Here is my Javascript: 这是我的Javascript:

//menu toggle
$(".trigger").click(function() {

   var divToToggle = $( $(this).data('target') );
   $(".toggle:visible").not(divToToggle).hide();
   $(".trigger").not(divToToggle).removeClass('active');
   $('.top-nav').css('margin-top', '20px');

   divToToggle.slideToggle("fast", "linear");

   $(this).toggleClass('active');
   $('.top-nav').css('margin-top', '0px');

   return false;

});

The .toggle class is on all the divs that are toggled: .toggle类位于所有已切换的div上:

    <div class="account-container toggle hide"></div>
    <div class="collections-container toggle hide"></div>
    <div class="search-container toggle hide"></div>

The .trigger class is on all my links: .trigger类在我的所有链接上:

    <ul class="top-nav">
        <li><a class="hidden-tablet" href="">home </a></li>
        <li><a class="hidden-tablet" href="">about us </a></li>
        <li><a class="hidden-tablet" href="">where to buy </a></li>
        <li><a class="hidden-tablet" href="">contact us </a></li>
        <li class="tablet-menu visible-tablet"><a class="tablet-menu trigger" href="" data-target=".tablet-menu-container">tablet menu</a></li>
        <li class="account"><a class="account trigger" href="" data-target=".account-container">account</a></li>
        <li class="collection"><a class="collection trigger" href="" data-target=".collections-container">collections</a></li>
        <li class="search"><a class="search trigger" href="" data-target=".search-container">search</a></li>
        <li class="basket"><a class="basket trigger" href="" data-target=".home-basket-container">basket</a></li>
            </ul>

It's hard to say where exactly your code is going wrong, but I've re-written it so it works slightly differently. 很难说您的代码到底出了什么问题,但是我已经重写了它,因此它的工作方式略有不同。 Your click handler has to handle two possibilities: either we're clicking to hide an existing section, or we're clicking to switch to a new section. 您的点击处理程序必须处理两种可能性:要么单击以隐藏现有部分,要么单击以切换到新部分。 But we can also think of this as being two steps, with the second one optional: either we hide an existing section, or we hide an existing section and show a new one. 但是我们也可以将其视为两个步骤,第二个步骤是可选的:要么隐藏现有的部分,要么隐藏现有的部分并显示一个新的部分。

I've also switched to using ev.preventDefault() to stop the link from firing, named the jQuery variables so they start with $ (which makes them easier to differentiate). 我还切换到使用ev.preventDefault()来阻止链接的触发,该链接名为jQuery变量,因此它们以$开头(这使它们更易于区分)。 You can try it out on jsfiddle here . 您可以在jsfiddle上尝试一下

$(document).ready(function () {
    $(".trigger").click(function (ev) {
        ev.preventDefault();
        var $clickedLink = $(this);
        var $divToToggle = $($(this).data('target'));
        var isHideOnly = $clickedLink.hasClass('active');

        // Hide the existing div and remove 'active' class.
        $(".toggle:visible").hide();
        $(".trigger").removeClass('active');
        $('.top-nav').css('margin-top', '20px');

        // If we're showing a new one, reveal it and set the active class on the link.
        if (!isHideOnly) { 
            $divToToggle.slideToggle("fast", "linear");
            $('.top-nav').css('margin-top', '0');
            $clickedLink.addClass('active');
        }
    });
});

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

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