简体   繁体   English

与其他元素同时动画的slidetoggle

[英]slidetoggle with simultaneous animation on other element

I have a webpage with basically 2 sections: a header area and a content area. 我有一个基本上包含两个部分的网页:标头区域和内容区域。 Header is fixed ie position:fixed and is always at the top. 标头是固定的,即position:fixed ,始终位于顶部。 There are a few elements in the header section and I want to use slide functionality on that. 标头部分中有一些元素,我想在其上使用幻灯片功能。

I've set appropriate padding-top for the content area. 我为内容区域设置了适当的padding-top But I need to change it when the sliding of the header elements executes. 但是我需要在执行标题元素的滑动时更改它。 Its working with a simple .CSS method. 它使用简单的.CSS方法工作。

Sample code: http://jsfiddle.net/kK7nJ/ 示例代码: http : //jsfiddle.net/kK7nJ/

I would like it to have simultaneous animation: ie when the menu is being displayed the content should be pushed down at the same time. 我希望它具有同步动画:即,在显示菜单时,应同时按下内容。 Right now it is happening one after the other. 现在它正在接连发生。

Please help. 请帮忙。

The problem you are having stems from the fact that your .slideToggle call is making use of the version which sets duration and then a function call to animate when complete, specifically ( as documented ): 您遇到的问题是由于您的.slideToggle调用使用了设置持续时间的版本,然后使用了一个函数调用来完成动画,特别是( 如所记录 ):

.slideToggle( [duration ] [, complete ] ) .slideToggle([持续时间] [,完成])

  • duration (default: 400) 持续时间 (默认值:400)
    Type: Number or String A string or number determining how long the animation will run. 类型:数字或字符串一个字符串或数字,确定动画将运行多长时间。
  • complete 完成
    Type: Function() 类型:Function()
    A function to call once the animation is complete. 动画完成后调用的函数。

While your anonymous function has the right idea, it (as you noticed) will only call after the animation finishes. 尽管您的匿名函数具有正确的概念,但它(如您所注意到的)仅在动画结束后才调用。

If you scroll down the documentation farther, you will find the following call format: 如果您进一步向下滚动文档,则会发现以下调用格式:

.slideToggle( options ) .slideToggle(options)

So, you pass an object to the .slideToggle method this way rather than just the two parameters as before, allowing you greater flexibility. 因此,您可以通过这种方式将对象传递给.slideToggle方法,而不是像以前那样仅将两个参数传递给它,从而可以提供更大的灵活性。

What you should take note of is this: 您应该注意的是:

  • step
    Type: Function( Number now, PlainObject fx ) 类型:函数(现在为Number,PlainObject fx)
    A function to be called after each step of the animation. 在动画的每个步骤之后要调用的函数。
  • So the basic way we would construct the options object to follow what you already linked would be to simply put duration and step in it, with step making the call to change the padding of the content element each animation frame (which is essentially how everything is animated by jquery to begin with, making this a nice and tidy solution): 因此,构造选项对象以遵循您已链接的对象的基本方法是简单地放置持续时间并逐步加入其中,并通过逐步调用来更改每个动画帧的内容元素的填充(本质上是指一切首先由jquery进行动画处理,这使它成为一个很好而整洁的解决方案):

    {
      duration: 'slow', step: function(){
        $(".content").css({'padding-top':$(".header").outerHeight()});
      }
    }

    Here is the jsfiddle implementing this , which seems to work just fine in chrome at least, even with the content frame scrolled down/etc. 这是实现此功能的jsfiddle ,即使内容框架向下滚动/等,它似乎至少在chrome中也能正常工作。

    Rather than manipulating padding, why not put a hidden div at the top of the page and toggle it opposite the header? 而不是操纵填充,为什么不将隐藏的div放在页面顶部并在页眉对面切换呢? I suspect that the disparate transition types are the root of your problem. 我怀疑不同的过渡类型是问题的根源。

    Here's an example showing this: 这是显示此的示例:

    <div class="header">
       <ul class="menu"><li>One</li><li>Two</li><li>Three</li><li>Four</li></ul>
       <a href="javascript:void(0)" id="pull">pull</a>
     </div>
    
    <div class="menu-pad"></div>
    
    $("#pull").on("click",function(){
      $(".menu, .menu-pad").slideToggle('slow');
    });
    

    http://jsfiddle.net/MV5kB/1/ http://jsfiddle.net/MV5kB/1/

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

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