【问题标题】:Unfold Submenu conditions (jQuery)展开子菜单条件 (jQuery)
【发布时间】:2016-09-19 22:27:53
【问题描述】:

我遇到了一个无法解决的 jQuery 问题。 我创建了一个带有子菜单元素的菜单。我想通过单击菜单项来切换内容的高度。问题是当我单击其他项目时,内容会崩溃。有点难以解释,我已经让两个网站完成这项工作 http://www.polerstuff.com/ -> 当您点击“商店”然后点击“信息”时,子菜单保持打开状态。在这里看到了同样的技巧http://topodesigns.com/

我猜这两个网站都在使用 Shopify。

$(document).ready(function() {
    $(".button").on("click", function() {
        if($(".content").height() == 0) {
            $(".content").animate({height: "300px"});
        }
        else if($(".content").height() == 300) {
            $(".content").animate({height: "0px"});
        }
     });
});

这是我的jsfiddle -> 提前非常感谢。

【问题讨论】:

    标签: javascript jquery html shopify


    【解决方案1】:

    这是您的小提琴版本,它使用 data 属性来定位具有所需内容的 div,以及另一个包含所需高度的数据标签以进行动画处理(但还有许多其他方式)。 单击同一按钮将其关闭,这是通过添加指示性类来实现的。 “隐藏”的 div 可能包含更多的 div,其中包含所需的类和布局。

    $(document).ready(function (){
        $(".b").on("click", function (){ 
        
          var $this = $(this),
        	  target = $this.data('target'),
              tall = $this.data('tall'),
              content = $(".content");
              
            target = $('.'+target).html(); // get the html content of target divs
            content.html(target); // insert said content
              
          	if (!$this.hasClass('on')) {   // if it hasn't been clicked yet..
               $(".b").removeClass('on');  // say that none have been clicked
    		   $this.addClass('on');     // say that just this one has been clicked
               content.animate({height: tall}, 200); // animate to the height specified in this buttons data attribute
            } else {
               content.animate({height: "0px"});
    		   $this.removeClass('on');  
            }
        });
    });
    .content {
        background: coral;
        width: 100%;
        height: 0px;
        overflow:hidden;  
    }
    
    .hiding{
        display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    
    <button class="b" data-target="alpha" data-tall="4em">Button</button>
    <button class="b" data-target="bravo" data-tall="7em">Button</button>
    <button class="b" data-target="charlie" data-tall="5em">Button</button>
    
    <div class="content">Le contenu</div>
    
    <div class="hiding alpha"> some stuff </div>
    <div class="hiding bravo"> other things </div>
    <div class="hiding charlie"> bits and pieces </div>

    【讨论】:

    • 太棒了,它就像一个魅力。我正在寻找切换的一侧,这不是一个好主意。我只需要为 html 内容添加淡入淡出效果,但你让我很开心。非常感谢
    • 没问题@BrunoLandowski 在这里额外的小提琴,高度数据重新定位到内容 div(为了更好的 html 结构)并淡入淡出:jsfiddle.net/wrx0uf3m
    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多