简体   繁体   English

jQuery动画div向下点击

[英]jQuery animate div up down on click

So I have this HTML 所以我有这个HTML

<div id="one">One</div>
<div id="two">Two   <br/> <br/>
   Two
</div>
<div id="three">Three</div>
<div id="four">Four   <br/> <br/>
   Four   <br/> <br/>
   Four
</div>

jQuery: jQuery的:

$(document).ready(function () {
    $("#two").hide();
    $("#four").hide();
    $("#one").click(function () {

        $("#four").slideUp("slow", function () {

        });
        $("#three").slideToggle("slow,", function () {

        });

        $("#two").slideToggle("slow", function () {

        });
    });

    $("#three").click(function () {
        $("#four").slideToggle("slow,", function () {});
    });
});


What I'm trying to do is show div one and three one under another. 我想做的是显示div一和三一。

When I click div one, I want div two to show (animate), and div three (and div four, if div three is clicked too) to move position under div two (with same animation). 当我单击div 1时,我要显示div(动画),并要使div 3(和div 4,如果也单击div 3,则要显示div)以在div 2下移动位置(具有相同的动画)。

When I click div one again, I want div two to hide (animate) and div three (and div four, if div three clicked) to go back to the position they had before. 当我再次单击div时,我要使div 2隐藏(动画)和div 3(如果单击div 3,则要划分div 4)以返回到以前的位置。


Here's JSFiddle: http://jsfiddle.net/6ynnR/ 这是JSFiddle: http : //jsfiddle.net/6ynnR/

It seems like you want something like this: EXAMPLE HERE 您似乎想要这样的东西: 此处示例

Updated jQuery: 更新的jQuery:

$("#two, #four").hide();

$("#one").click(function () {
    $("#two").slideToggle(function () {
        $("#four").slideUp();
    });
});

$("#three").click(function () {
    $("#four").slideToggle(function () {
        $("#two").slideUp()
    });
});

Alternatively, here is another approach: EXAMPLE HERE 或者,这是另一种方法:在此示例

Just remove the following code and you're good to go :D 只需删除以下代码,您就可以开始:D

    $("#four").slideUp("slow", function () {

    });
    $("#three").slideToggle("slow,", function () {

    });

I hope this will work for you, 希望这对您有用,

I added conditional statement: 我添加了条件语句:

if(($("#two").is(":visible"))&&($("#four").is(":visible"))){

      $("#four").slideToggle("slow,", function () {});
      $("#two").slideToggle("slow,", function () {});

    }

and removed two functions : 并删除了两个功能:

$("#four").slideUp("slow", function () {
    });

$("#three").slideToggle("slow,", function () {
    });

http://jsfiddle.net/uY73z/19/ http://jsfiddle.net/uY73z/19/

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

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