简体   繁体   English

有关如何为多个DOM元素设置动画的建议

[英]Advice on how to animate multiple DOM elements

I have multiple <li> elements which contain multiple <div> elements. 我有多个<li>元素,其中包含多个<div>元素。 Some of them have a starting display property of none . 其中一些的起始display属性为none
Based on mouse interaction, I want to show or hide some of the <div> elements. 基于鼠标的交互,我想显示或隐藏一些<div>元素。

CSS transitions can't transition display changes. CSS过渡无法过渡显示更改。 And setting height and opacity to 0 isn't a good idea, some of the elements have inputs or buttons. 将高度和不透明度设置为0并不是一个好主意,某些元素具有输入或按钮。

So the next step was to use JavaScript for animations. 因此,下一步是将JavaScript用于动画。 I'm using jQuery. 我正在使用jQuery。

My problem is that elements that need to be hidden or shown in parallel. 我的问题是需要隐藏或并行显示的元素。 Because of the DOM structure I can't just hide or show the parent because some of the elements will be hidden while others will be shown. 由于DOM结构,我不能只隐藏或显示父对象,因为某些元素将被隐藏,而另一些元素将被显示。

So my question is, how would one efficiently handle multiple parallel animations (fades and slides mostly) on multiple elements while keeping code clean and maintainable and performance as good as possible. 所以我的问题是,如何在保持代码整洁,可维护和性能最佳的同时,有效地处理多个元素上的多个并行动画(大部分是淡入淡出和滑动)。

An example of markup, where the div elements need to be show or hidden: 标记的示例,其中div元素需要显示或隐藏:

<ul>
    <li>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </li>
    <li>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </li>
    <li>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </li>
</ul>

Thank you. 谢谢。

You can assign the same class to each set of elements that you want to display and hide together. 您可以为要显示和隐藏在一起的每组元素分配相同的类。 If this doesn't sound familiar you're probably going to need to take some time to familiarize yourself with that concept. 如果听起来不太熟悉,您可能需要花费一些时间来熟悉该概念。 This should get you started: http://api.jquery.com/hide/ 这应该可以帮助您入门: http : //api.jquery.com/hide/

<div class="first-class"></div>
<div class="second-class"></div>
<div class="third-class"></div>
<div class="first-class"></div>
<div class="third-class"></div>
<div class="second-class"></div>

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

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