【问题标题】:SlideUp SlideDown sequencing for multiple divs多个 div 的 SlideUp SlideDown 排序
【发布时间】:2013-06-01 01:34:33
【问题描述】:

我想像在这个小提琴中那样滑动切换多个 div:

http://jsfiddle.net/jakecigar/XwN2L/2154/

这个脚本的功能不错,但是我需要隐藏的内容在之前依次往下滑动。

此外,当您在打开时单击活动 div 链接时,它会导致它向上滑动并隐藏,因为这是用于站点菜单。

这是 HTML:

  <style>.targetDiv {display: none}</style>

<a  class="showSingle" target="1">Div 1</a>
<a  class="showSingle" target="2">Div 2</a>
<a  class="showSingle" target="3">Div 3</a>
<a  class="showSingle" target="4">Div 4</a>

<div id="div1" class="targetDiv">Lorum Ipsum1</div>
<div id="div2" class="targetDiv">Lorum Ipsum2</div>
<div id="div3" class="targetDiv">Lorum Ipsum3</div>
<div id="div4" class="targetDiv">Lorum Ipsum4</div>

这是脚本:

jQuery(function(){
   jQuery('.showSingle').click(function(){
      jQuery('.targetDiv').slideUp();
      jQuery('.targetDiv').hide();
      jQuery('#div'+$(this).attr('target')).slideToggle();
   });
});

【问题讨论】:

    标签: jquery menu


    【解决方案1】:

    这是一个解决方案,它使用active 类在没有显示任何内容或单击与当前显示的元素关联的 div 时创建适当的功能。

    jQuery(function($){
    $('.showSingle').click(function(){
        var itemid = '#div'+$(this).attr('target'); //id of the element to show/hide.
    
        //Show the element if nothing is shown.
        if($('.active').length === 0){
            $(itemid).slideDown();
            $(itemid).addClass('active');
    
        //Hide the element if it is shown.
        } else if (itemid == "#"+$('.active').attr('id')) {
            $('.active').slideUp();
            $(itemid).removeClass('active');
    
        //Otherwise, switch out the current element for the next one sequentially.
        }else {
            $('.active').slideUp(function(){
                $(this).removeClass('active');
                if ($(".targetDiv:animated").length === 0){
                    $(itemid).slideDown();
                    $(itemid).addClass('active');
                }
            });
        }
    });
    });
    

    http://jsfiddle.net/m6aRW/1/

    编辑
    如果您页面上的其他内容已经在使用 active 类,这将中断。使用不同的类名或更精确的选择器。

    【讨论】:

    • 感谢 Fallexe,由于某种原因,jsfiddle 对我不起作用
    • 最后一件事,你知道是否可以为过渡添加缓动效果吗?
    • 好吧,太好了……抱歉,这是我要问的最后一件事。您知道如何将课程添加到活动链接(打开的链接)吗?说真的……最后……我保证的问题
    • 尝试将 $('.showSingle').addClass('classname') 放置在应该添加类的位置(第一个和第三个 if/else 块的开头)和 $(this).removeClass('classname') 就在点击回调的开头。
    • 对不起,我的评论有一些(更正)错误,但请查看 jsfiddle.net/m6aRW/2 应该做你想做的事。
    【解决方案2】:

    您需要从早期动画的完成功能触发连续的动画。这会让你开始一个,当它完成时,开始下一个,当它完成时,开始下一个,依此类推。

    我不清楚您希望该行为如何工作。如果你能解释得更好,我可以提供一个代码示例。

    猜测你想要什么,这里有一个例子:

    jQuery(function(){
       jQuery('.showSingle').click(function(){
           // get visible targetDivs
           var vis = jQuery('.targetDiv:visible');
    
           // get the item we're supposed to show from an attribute on what was clicked
           var targetItem = $(this).attr('target');
    
           // make jQuery object for target
           var target = jQuery('#div' + targetItem);
    
           // assume we want to slideDown
           var fn = function() {
               target.slideDown();
           };
           // if there are some visible,then we will get a completion function
           // and should hide visible items
           if (vis.length) {
               if (vis[0].id == "div" + targetItem) {
                   // if clicking on the one that's already shown, 
                   //    nothing to show on completion
                   fn = function() {};
               }
               vis.slideUp(fn);
           } else {
               // otherwise, just show the selected one (none to hide)
              target.slideDown();
           }
       });
    });
    

    工作演示:http://jsfiddle.net/jfriend00/fd4Nn/

    【讨论】:

    • 如果快速点击 div,会同时显示多个元素。这可能是也可能不是问题,具体取决于动画速度。
    • 是的.. 我没有注意到这一点。你知道怎么解决吗?
    • @user2441150 - 这是一个试图防止快速多次点击的版本:jsfiddle.net/jfriend00/fd4Nn/17
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多